Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent child div from overflowing parent div

Tags:

css

overflow

I have a parent div containing a header section and a body section. The parent div has a maximum height, but no minimum height.

See this example: https://jsfiddle.net/1o79a6nc/2/

#cont {    padding: 5px;    background-color: red;    max-height: 150px;    max-width: 50%;  }  #body {    background-color: blue;    overflow-y: auto;    overflow-x: hidden;  }  #head {    background-color: green;  }
<div id='cont'>    <div id='head'>      <div>Head</div>    </div>    <div id='body'>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>    </div>  </div>

I want the body to only expand so that it fits inside the parent (including the padding), and then apply scroll bars when necessary.

As the example shows, I have tried overflow-y: auto, however this is not working as I have intended.

Edit: I want scroll bars to only appear on the body, so that the head section and the parent bottom padding are always visible.

like image 987
Right Of Zen Avatar asked Jun 28 '16 02:06

Right Of Zen


People also ask

How do you stop a child div from overflowing parent div?

You could simply set the container to flexbox. Another option would be using CSS table, some extra HTML code is needed. And max-height doesn't work with table layout, so use height instead. Show activity on this post.

How do I keep my div from expanding?

You should use display: table; It will shrink to the size of it's contents and can also be centered and positioning without having to assign a given width.

How do I make DIVS always fit in my parent div?

If you want the child divs to fit the parent size, you should put a margin at least of the size of the child borders on the child divs ( child. margin >= child.

Why is my div overflowing?

Overflow happens when there is too much content to fit in a box. CSS provides various tools to manage overflow. As you go further with CSS layout and writing CSS, you will encounter more overflow situations.


2 Answers

You could simply set the container to flexbox.

#cont {   padding: 5px;   background-color: red;   max-height: 150px;   max-width: 50%;   display: flex; /*added*/   flex-direction: column; /*added*/ } 

jsFiddle

#cont {    padding: 5px;    background-color: red;    max-height: 150px;    max-width: 50%;    display: flex; /*added*/    flex-direction: column; /*added*/  }  #body {    background-color: blue;    overflow-y: auto;    overflow-x: hidden;    flex: 1; /* added */  }  #head {    background-color: green;  }
<div id='cont'>    <div id='head'>      <div>Head</div>    </div>    <div id='body'>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>      <div>Body</div>    </div>  </div>

Another option would be using CSS table, some extra HTML code is needed. And max-height doesn't work with table layout, so use height instead.

jsFiddle

#cont {    padding: 5px;    background-color: red;    height: 150px;    width: 50%;    display: table;  }  #head,  #body {    display: table-row;  }  #head > div,  #body > div {    display: table-cell;    position: relative;  }  #body > div {    height: 100%;  }  #body > div > div {    position: absolute;    left: 0;    right: 0;    top: 0;    bottom: 0;    overflow-y: auto;    overflow-x: hidden;  }  #body {    background-color: blue;    overflow-y: auto;    overflow-x: hidden;  }  #head {    background-color: green;  }
<div id='cont'>    <div id='head'>      <div>Head</div>    </div>    <div id='body'>      <div>        <div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>          <div>Body</div>        </div>      </div>    </div>  </div>
like image 55
Stickers Avatar answered Sep 16 '22 20:09

Stickers


This solution worked for me and its display the vertical scrollbar only in child div.

.parent {     height : 300px ; //for example     overflow: hidden; }    .child   {        max-height: 100%;     overflow-y: scroll; } 
like image 44
Maherbs Avatar answered Sep 20 '22 20:09

Maherbs