Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div 100% width when parent tag has padding?

Tags:

I have an HTML page where the body tag has CSS padding-left and padding-right applied. I'd like the page footer div to be 100% width of the page though, without the body padding applied. Is there a good/easy way of doing this ?

like image 397
Michael Low Avatar asked Mar 30 '11 06:03

Michael Low


People also ask

Does width 100% include padding?

No, element width does not include padding, margin, or border. The basic difference between padding and width is that in padding you define the space around the element and on the other hand in the width you define the space of the element.

How do you make a div full width of a parent?

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 you make a div 100 width?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

Does padding count towards width?

The content-box model states that padding and borders don't count in the width that you set for a box. So they add on to its width .


2 Answers

You could apply a negative margin to the inside container to negate the padding

<body style="padding:0 40px">
<div style="width:100%;margin:0 -40px">&nbsp</div>
</body>
like image 148
David John Smith Avatar answered Sep 26 '22 22:09

David John Smith


If you give the footer a left and right margin that is a negative amount, of equal size to the padding of the body, then it will counter the body's padding.

body {
    padding-left:10px;
    padding-right:10px;
}
.footer {
    margin-left:-10px;
    margin-right:-10px;
}
like image 22
Leon Bambrick Avatar answered Sep 23 '22 22:09

Leon Bambrick