【问题标题】:CSS div not changing height to fit larger child divCSS div没有改变高度以适应更大的子div
【发布时间】:2014-06-25 21:16:59
【问题描述】:

好的,所以我有一个容器 div,其中包含两个左右浮动的 div。这些子 div 的高度取决于里面的内容(目前只是文本)。

问题是容器 div 的高度没有正确变化以适应孩子们;相反,它只适合右手 div 并且不适合左手。

我已将所有 div 的高度设置为自动。我也尝试将较大的子 div 的大小设置为 100%,但这似乎没有效果。

下面包含三个相关的 CSS(我还将提供网站链接,以便在需要时检查其余的 CSS 和 HTML)。

#container{
/*
The container, contains the page and the footer
*/
    height: auto;   /* this is the problem child...*/
    width: 1000px;
    margin: auto;
    padding-top: 20px;
    background-color: white;
}

#content{
/*
The container, contains the content of the webpage
*/
    height: auto;
    width: 600px;   
    margin: 10px 10px 0px 10px;
    background-color: white;
    float: left;
}

#aside{
/*
The aside to be used for additional information
*/
    height: auto;
    width: 330px;   
    float: right;   
    margin: 10px 10px 0px 10px;
    padding: 5px 15px 5px;
    background-color: #E8E8E8;
    border-radius: 25px;
}

基本上我想要它,以便容器将是两个孩子中较高者的正确高度(但在这种情况下,我认为它始终是右手的)。但是目前它似乎总是匹配较小的。

可以看到的网站链接:http://timbrodrick.com/aboutWebsite.html “©2014 Tim Brodrick, info@timbrodrick.com”是页脚的一部分,它是一个从容器 div 末尾开始的 div。

【问题讨论】:

  • 浮动元素就是这样——它们不会影响父元素的高度。您可以通过搜索关键字“clearfix”找到对抗这种行为的技术。

标签: html css web


【解决方案1】:

在您的#footer 或#container 规则中,添加overflow:auto。这将恢复由浮动孩子创建的行为。

【讨论】:

    【解决方案2】:


    您还可以做另一件事:
    您可以像这样在 side 标记下添加无效图像:

    <img src="nothing.png" height="271px" style="visibility: hidden;">
    

    但我认为第一个答案是最好的。

    【讨论】:

      【解决方案3】:

      这里看起来像是对 clearfix 的调用。尝试向两个内部 div 添加一个类,说“浮动”并使用从两个 div 中删除 float:left , right css。然后在 floaters 类中使用 display:inline-block。

      #container{
      /*
      The container, contains the page and the footer
      */
          height: auto;   /* this is the problem child...*/
          width: 1000px;
          margin: auto;
          padding-top: 20px;
          background-color: white;
      }
      
      #content{
      /*
      The container, contains the content of the webpage
      */
          height: auto;
          width: 600px;   
          margin: 10px 10px 0px 10px;
          background-color: white;
          /*float: left; */
      }
      
      #aside{
      /*
      The aside to be used for additional information
      */
          height: auto;
          width: 330px;   
         /* float: right;  */ 
          margin: 10px 10px 0px 10px;
          padding: 5px 15px 5px;
          background-color: #E8E8E8;
          border-radius: 25px;
      }
      .flaoters{
      display:inline-block;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 2012-02-11
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多