search

Home  >  Q&A  >  body text

MUI: Align and adjust image center in flexbox

Why is my image centered but not aligned? I'm confused..

<Box
      sx={{
        display: 'flex',
        minHeight: '100vh',
        alignItems: 'center',
        flexGrow: 1,
      }}>
      <Container maxWidth="sm">
        <img src="https://static.vecteezy.com/system/resources/previews/001/200/294/original/house-png.png"
          width={200}
          alt="img"
        />
        <LinearProgress sx={{ mt: 4 }} />
      </Container>
    </Box>
P粉550323338P粉550323338500 days ago555

reply all(2)I'll reply

  • P粉548512637

    P粉5485126372023-09-09 12:34:07

    If you want to center-align the content, you must use the "justifyContent: 'center'" attribute. Also, there are some issues with the "Container" element, you can use .

    Check if this is what you want to achieve

    <Box
      sx={{
        display: "flex",
        minHeight: "100vh",
        alignItems: "center",
        justifyContent: "center",
        flexGrow: 1
      }}
    >
      <Box>
        <img
          src="https://static.vecteezy.com/system/resources/previews/001/200/294/original/house-png.png"
          width={200}
          alt="img"
        />
        <LinearProgress sx={{ mt: 4 }} />
      </Box>
    </Box>

    Demo on Codesandbox

    reply
    0
  • P粉680487967

    P粉6804879672023-09-09 09:42:29

    You can wrap img with Box

    <Box
      sx={{
        display: 'flex',
        minHeight: '100vh',
        alignItems: "center",
        flexGrow: 1,
      }}
    >
      <Container maxWidth="sm">
        <Box
          sx={{
            display: 'flex',
            justifyContent: "center"
          }}
        >
          <img
            src="https://static.vecteezy.com/system/resources/previews/001/200/294/original/house-png.png"
            width={200}
            alt="img"
          />
        </Box>
        <LinearProgress sx={{ mt: 4 }} />
      </Container>
    </Box>

    View my demo here

    reply
    0
  • Cancelreply