【问题标题】:How to make all horizontal child div's to fit to the parent?如何使所有水平子div适合父母?
【发布时间】:2021-11-12 16:40:19
【问题描述】:

这是我的 HTML sn-p,

<div class=bigger>
    <div>A</div>
    <div>B</div>
    <div>C</div>
</div>

如果我有这样的 CSS:

.bigger
{
  height: 300px;
}

在 CSS 中有什么方法可以让所有子 div 的高度与父 div 的高度相同,即都应该有 300px 的高度?

【问题讨论】:

  • 请查看您的问题,然后使用“编辑”链接并使用提供的格式化工具。 编辑:Kos 已经帮你了。
  • 如果你的父母身高是固定的,那么你总是可以计算子div的数量并相应地制作动画
  • 我不禁注意到您使用的是heigh: 300px; 而不是height: 300px;(带有t)。你是复制粘贴的,还是只是问题中的错字?
  • 类型错误?有没有相同的css解决方案?
  • @Manglore 你的实际问题是什么?

标签: html css css-float


【解决方案1】:

使用 Flexbox 的解决方案:

.bigger {
  height: 400px;
  background: #eea;

  display: flex;
  flex-direction: column;
}

.bigger div {
  margin: 4px;
  border: 1px dotted green;
  flex: auto;   
}

演示:http://codepen.io/anon/pen/nAjLt (注意 Codepen 做了供应商前缀)

【讨论】:

  • +1 用于使用flex 的简单解决方案,但请注意,这不适用于旧版浏览器
  • 没错,Flexbox 仍处于试验阶段,我不会在生产中使用它。有关最新的兼容性表,请参阅 caniuse.com/flexbox
【解决方案2】:

如果你想在纯 CSS 中实现这一点,你可以试试这个

DEMO

我已经为 4 个项目设置了它。您可以将其扩展到任意数量的项目。

/* one item */
.bigger > div:first-child:nth-last-child(1) {
    height: 300px;
}

/* two items */
.bigger > div:first-child:nth-last-child(2),
.bigger > div:first-child:nth-last-child(2) ~ div {
    height: 150px;
}

/* three items */
.bigger > div:first-child:nth-last-child(3),
.bigger > div:first-child:nth-last-child(3) ~ div {
    height: 100px;
}

/* four items */
.bigger > div:first-child:nth-last-child(4),
.bigger > div:first-child:nth-last-child(4) ~ div {
    height: 75px;
}

【讨论】:

    【解决方案3】:

    允许元素占用相等的空间是传统上只有使用&lt;table&gt;s 才能实现的。不过你可以use CSS to make your &lt;div&gt;s behave like &lt;table&gt;s instead:

    HTML

    <div class=bigger>
        <div>A</div>
        <div>B</div>
        <div>C</div>
        <div>A</div>
        <div>B</div>
        <div>C</div>
        <!-- add or remove any number of <div>s here -->
    </div>
    

    (相关)CSS

    .bigger {
        display: table;
        height: 300px;
        width: 100%;
    }
    
    .bigger > div {
        display: table-row;
        border-top: 1px solid black;
        border-bottom: 1px solid black;
    }
    

    【讨论】:

    • 如果
      A
      内容超过300px怎么办?我的意思是可以为子div添加溢出:隐藏。我已经尝试过,但没有为我工作
    • 因为它的行为与&lt;table&gt; 完全一样,不幸的是,您无法阻止它调整大小。不过,如果需要,you can wrap that within another &lt;div&gt; which prevents overflow
    • 我的意思是,如果里面有 4 个更大的 div 并且内容可以是任意行数,都应该共享相同的高度?
    • @Manglore 因为.bigger 的显示像一张桌子,所以它的所有孩子都将(尝试)等高。如果您超过了它的高度,您可以确保将其包裹在另一个 &lt;div&gt; 中,以隐藏可能发生的任何溢出。
    【解决方案4】:

    要与孩子和父母一起工作,您需要使用automax

    <div class="bigger">
     <div>A</div>
     <div>B</div>
     <div>C</div>
    </div>
    

    这里可以使用

    .bigger {
      height: auto; // this will do the trick..
      overflow: none;
      min-height: 300px;
    }
    

    使用 max 和 min,只是为了确保每次孩子增加或减少时 height 保持不变,然后使用这个:

    .bigger div {
      height: 100%; // note this..
    }
    

    在这里试试这个小提琴:http://jsfiddle.net/afzaal_ahmad_zeeshan/2bJfW/ 再添加一个 div 并检查它,

    如果你想动态创建 div 高度,那么没有 CSS,你需要 JS 或者说 jQuery。因为您需要计算孩子的数量,然后更改他们的身高百分比,假设从 10030(对于 3)或 22 对于(4)等等,因为文本不会不适合那个尺寸。

    【讨论】:

      【解决方案5】:
      .bigger
      {
        height: auto;
      }
      .bigger div
      { 
      height:50px;
      }
      

      你应该提到每个内部div的高度,然后才能正常工作。

      现在你在更大的 div 中添加另一个内部 div 元素,高度应该相同。

      这里我提到了50px的高度,你给你想要的高度。

      【讨论】:

        【解决方案6】:
        .bigger {
            width:100%;
            height:auto;
            border:1px solid red;
        }
        .bigger div {
            height:100px;
            width:100%;
            border:1px solid blue;
        }
        

        通过将height 设置为auto it will adjust to any no of divs,使用此css。

        另外.bigger div 会将所有 div 设置为 100px 高度。

        Live Demo

        编辑:对于固定容器

        .bigger {
            width:100%;
            height:300px;
            border:1px solid red;
            display:table;
        }
        .bigger div {
            display:table-row;
            height:auto;
            width:100%;
        }
        

        Live Demo2

        所有浏览器都一样:)

        【讨论】:

        • OP 想要在容器上固定高度,而不是元素。
        • 如果子 div 的高度超过 300 像素,则此操作失败。如何添加 oveflow : hidden properties to child?
        猜你喜欢
        • 2014-03-11
        • 2010-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多