一尘不染

如何使div调整其高度以适合容器?

css

1:如何使导航div向下扩展或使其高度与父div相同?

*   {

    border:0;

    padding:0;

    margin:0;

}

#container {

    margin-left: auto;

    margin-right: auto;

    border: 1px solid black;

    overflow: auto;

    width: 800px;

}

#nav {

    width: 19%;

    border: 1px solid green;

    float:left;

}

#content {

    width: 79%;

    border: 1px solid red;

    float:right;

}


<div id="container">

    <div id="nav">

        <ul>

            <li>Menu</li>

            <li>Menu</li>

            <li>Menu</li>

            <li>Menu</li>

            <li>Menu</li>

        </ul>

    </div>

    <div id="content">

        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam fermentum consequat ligula vitae posuere. Mauris dolor quam, consequat vel condimentum eget, aliquet sit amet sem. Nulla in lectus ac felis ultrices dignissim quis ac orci. Nam non tellus eget metus sollicitudin venenatis sit amet at dui. Quisque malesuada feugiat tellus, at semper eros mollis sed. In luctus tellus in magna condimentum sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur vel dui est. Aliquam vitae condimentum dui. Praesent vel mi at odio blandit pellentesque. Proin felis massa, vestibulum a hendrerit ut, imperdiet in nulla. Sed aliquam, dolor id congue porttitor, mauris turpis congue felis, vel luctus ligula libero in arcu. Pellentesque egestas blandit turpis ac aliquet. Sed sit amet orci non turpis feugiat euismod. In elementum tristique tortor ac semper.</p>

    </div>

</div>

阅读 240

收藏
2020-05-16

共1个答案

一尘不染

简单的方法

您可以使用nav to 和的设置topbottom属性来实现此0目的position: absolute。将容器设置为position: relative

现代方式(Flexbox)

IE11 +和所有现代浏览器都支持flexbox。

.container {
  display: flex;
  flex-direction: column;
}

.child {
  flex-grow: 1;
}
2020-05-16