Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

border cause DIV overflow

Tags:

html

css

I have a root div with width:80% and a child with width:100%, when I add to the child a border a scroll bar appear. I guess it's because 100% width + 1px border overflow. Can I avoid such problem? How to define a child div as wide as the parent including borders?

I need this to run on IE8

<div style="width:80%;overflow:auto;">
    <div style="float:left;width:100%;border-left:1px solid;">
        <div class="title">Title1</div>
        <div style="width:100%;">
            <div class="title">Title2</div>
            <div class="title">Title3</div>
        </div>
        <div style="clear:both;"></div>
    </div>
    <div>text</div>
    <div>text</div>
</div>

CSS

div.title {
    float: left;
    width:33%;
}

Example: http://jsfiddle.net/FN3mv/

like image 327
user3369398 Avatar asked Mar 04 '14 01:03

user3369398


People also ask

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.

Why is padding causing overflow?

By default in CSS, an element's actual width and height is width + padding + border. Meaning, when we give an element a width of maybe 10px, the actual width of that element becomes 10px + padding + border. This is why giving it padding or border when it is already at 100% will cause an overflow.


2 Answers

Add box-sizing: border-box to your 100% width element.

You will also need -moz-box-sizing: border-box.

like image 199
Niet the Dark Absol Avatar answered Oct 13 '22 01:10

Niet the Dark Absol


You have assigned width to 100% and adding borders of 1px will add 2px + 100% if you need borders you need another parent div

<div style="width:50%;overflow:auto;">
    <div style="border:1px solid;">
    <div style="width:100%;">
        <div style="float: left;width:50%;">AAA</div>
        <div style="float: left;width:50%;">BBB</div>
        <div style="clear:both;"></div>
        </div></div>
    <div>A1</div>
    <div>A2</div>
</div>
like image 34
Genus Avatar answered Oct 12 '22 23:10

Genus