【问题标题】:CSS Make siblings children same width and height (squares) in 100% width parentCSS使兄弟姐妹的孩子在100%宽度的父母中具有相同的宽度和高度(正方形)
【发布时间】:2020-09-09 14:42:39
【问题描述】:

我有一个宽度为“100%”(不是屏幕宽度)的父视图,我希望每个子视图具有相同的宽度和高度。

我的代码是:

<View style={styles.container}>
  <View style={styles.item} />
  <View style={styles.item} />
  <View style={styles.item} />
  <View style={styles.item} />
</View>

const styles = StyleSheet.create({
  container: {
    width: "100%",
    flexDirection: "row",
    alignItems: "center",
  },
  item: {
    width: 75,
    height: 75,
    marginRight: 10,
    backgroundColor: "red",
  },
});

但这并不是对所有设备都有响应(在小型服务中,一些孩子会离开屏幕)...有没有其他方法可以做到这一点?

谢谢。

【问题讨论】:

    标签: html css reactjs react-native


    【解决方案1】:
      container: {
        flexDirection: "row",
        alignItems: "center",
      },
      item: {
        flex: 1,
        aspectRatio: 1,
        backgroundColor: "red",
      },
    

    适合您的情况的诀窍是 aspectRatio。只需让每个孩子 flex: 1,但纵横比为 1 以使它们成平方。

    【讨论】:

      【解决方案2】:

      百分比宽度

      可能有很多方法可以做到这一点,但首先想到的是将宽度设置为百分比。这使项目成为父项的百分比。

        item: {
          width: '30%',
          height: 75,
          marginRight: 10,
          backgroundColor: "red",
        }
      

      如果您需要更灵活的东西,您可能需要研究响应式设计并为不同的屏幕尺寸设置断点。

      弹性包裹

      如果您希望内容保持固定大小,可以将其包裹在容器中。对于不同的屏幕尺寸,一行中的框可能比另一行多。

      您可能需要更改一些样式,但包装看起来像这样

      <View style={styles.container}>
        <View style={styles.item} />
        <View style={styles.item} />
        <View style={styles.item} />
        <View style={styles.item} />
      </View>
      
      const styles = StyleSheet.create({
        container: {
          width: "100%",
          justifyContent: 'space-between',
          flexDirection: "row",
          alignItems: "center",
          flexWrap: 'wrap',
        },
        item: {
          width: 75,
          height: 75,
          marginRight: 10,
          backgroundColor: "red",
        },
      });
      
      

      网格

      如果您不喜欢每行可变数量的框,您可以制作一个网格。您将有一个容器、行,并且在每一行中都有您的盒子。

      同样,您必须调整一些样式。

      <Container style={styles.container}>
        <Row style={styles.row}>
          <Box />
          <Box />
        <Row />
        <Row style={styles.row}>
          <Box />
          <Box />
        <Row />
      </Container>
      
      const styles = StyleSheet.create({
        container: {
          width: "100%",
        },
        row: {
          width: "100%",
          justifyContent: 'space-between',
          flexDirection: "row",
          alignItems: "center",
        },
        item: {
          width: 75,
          height: 75,
          marginRight: 10,
          backgroundColor: "red",
        },
      });
      

      【讨论】:

      • 问题是孩子们必须是正方形
      • 啊,这有点棘手,但您可以尝试将正方形包裹在容器中。让我看看能不能找到一个好例子。
      • 像这样设置 padding-top:w3schools.com/howto/howto_css_aspect_ratio.asp 然后你可以坚持使用百分比
      • 查看我的解决方案。此外,您可以尝试像@JWilk 所说的那样测量父级,但使用 aspectRatio 是不必要的
      • @Raul 我对 rems 有误解,因为这是本机反应。 Victor 的 aspectRatio 解决方案可能会更好。如果你决定使用测量,我为此做了一个测量钩子:stackoverflow.com/a/63767950/14231183
      猜你喜欢
      • 2018-12-17
      • 2019-05-06
      • 2019-10-03
      • 2012-04-03
      • 2023-03-17
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多