Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a div to resize its height to fit container?

Tags:

html

css

How can I get the nav div to have expand down or have the height the same as its parent 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>
like image 569
ParoX Avatar asked Aug 04 '09 23:08

ParoX


People also ask

How do I stretch a div to fit a container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do I auto-resize an image to fit a div container?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I fit something in a div?

For width it's easy, simply remove the width: 100% rule. By default, the div will stretch to fit the parent container.


1 Answers

Simple Way

You can achieve this with setting both the top and bottom attributes of the nav to 0 and the position: absolute. Set the container to position: relative.

See how this is done

Modern Way (Flexbox)

IE11+ and all modern browsers support flexbox.

.container {   display: flex;   flex-direction: column; }  .child {   flex-grow: 1; } 
like image 132
kmiyashiro Avatar answered Sep 27 '22 23:09

kmiyashiro