【问题标题】:Using Flexbox and flexGrow with Textfield将 Flexbox 和 flexGrow 与 Textfield 一起使用
【发布时间】:2020-10-13 18:36:02
【问题描述】:

我只是想仅使用 Flexbox 在屏幕上拆分组件,以便它们出现在正确的位置。

我有 3 行文本字段,其特定比例与全宽相关:

  • 第 1 行:2、1、1
  • 第 2 行:1, 1
  • 第 3 行:1、1、2

我尝试了Box组件的flexGrow和width,但没有成功。

预期结果:

我的结果:

使用以下代码:

<Box display="flex" p={1} bgcolor="background.paper">
    <Box m="0.5em" width={"50%"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
    </Box>
    <Box m="0.5em" width={"25%"}  bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
    </Box>
    <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
    </Box>
</Box>

<Box display="flex" p={1} bgcolor="background.paper">
    <Box m="0.5em" width={"50%"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
    </Box>
    <Box m="0.5em" width={"50%"}  bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
    </Box>
</Box>

<Box display="flex" p={1} bgcolor="background.paper">
    <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
    </Box>
    <Box m="0.5em" width={"25%"}  bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
    </Box>
    <Box m="0.5em" width={"50%"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
    </Box>
</Box>

有没有办法正确对齐文本字段。

【问题讨论】:

    标签: css flexbox material-ui


    【解决方案1】:

    我认为您没有考虑每个弹性项目提供的边距。

    假设你有:

    ROW <Box/> container width = 768px
    ROW <Box/> container left+right padding = 16px
    font size = 16px
    margin 0.5em = 8px
    

    对于第 1 行

    number of box elements (i.e., flex items) = 3
    summation of elements left+right margins = 3*(8*2) = 48px
    width left for content = 768px - 48px - 16px = 704px
    element 1 with 50% width = 704*0.5 = 352px
    element 2 with 25% width = 704*0.25 = 176px
    element 3 with 25% width = 704*0.25 = 176px
    summation of width of element 2+3 = 352px
    

    对于第 2 行

    number of box elements (i.e., flex items) = 2
    summation of elements left+right margin = 2*(8*2) = 32px
    width left for content = 768px - 32px - 16px = 720px
    element 1 with 50% width = 720*0.5 = 360px
    element 2 with 50% width = 720*0.5 = 360px
    

    第 1 行元素 1 宽度与第 2 行元素 1 宽度之间的比较 = 352 &lt; 360


    要解决这个问题,您可以为相关的&lt;Box/&gt; 元素分配更多宽度

    <Box display="flex" p={1} bgcolor="background.paper">
      <Box m="0.5em" width={"50%"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
      </Box>
      <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
      </Box>
      <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
      </Box>
    </Box>
    
    <Box display="flex" p={1} bgcolor="background.paper">
      <Box m="0.5em" width={"50%"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
      </Box>
      <Box m="0.5em" width={"calc(50% + 1em)"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
      </Box>
    </Box>
    
    <Box display="flex" p={1} bgcolor="background.paper">
      <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
      </Box>
      <Box m="0.5em" width={"25%"} bgcolor="grey.300">
        <TextField value="Quarter" fullWidth></TextField>
      </Box>
      <Box m="0.5em" width={"calc(50% + 2em)"} bgcolor="grey.300">
        <TextField value="Half" fullWidth></TextField>
      </Box>
    </Box>
    

    虽然这是一个解决方案,但我建议使用Grid。在那里,您已经可以使用 xs、lg 等道具指定在特定屏幕尺寸中需要多少弹性项目,以便您轻松控制响应速度

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 1970-01-01
      • 2021-01-14
      • 2020-04-17
      • 2017-08-27
      • 1970-01-01
      • 2016-02-12
      • 2016-04-10
      • 2017-07-04
      相关资源
      最近更新 更多