MUI: Bildmitte in der Flexbox ausrichten und anpassen
P粉550323338
P粉550323338 2023-09-08 18:20:02
0
2
523

Warum ist mein Bild in der Mitte ausgerichtet, aber nicht an den Enden? Ich bin so verwirrt..

<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粉550323338
P粉550323338

Antworte allen(2)
P粉548512637

如果您想使内容居中对齐,则必须使用“justifyContent: 'center'”属性。另外,“Container”元素存在一些问题,您可以使用 。

检查这是否是您想要实现的目标

<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>

Codesandbox 上的演示

P粉680487967

您可以用 Box 包裹 img

<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>

此处查看我的演示

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage