Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

margin-bottom property of a div's last element doesn't "extend" the div

Tags:

html

css

margin

I have an element in a div, which has a background image. Below the div I have another div with another background image. Now the problem is that if the last element contained in the first div has margin-bottom applied there will be a gap between the 2 divs like this:

Screenshot http://img40.imageshack.us/img40/5603/littlesnapperh.png

Notice the gray gap caused by the margin-bottom property of the h2 element contained within the first div. I know this can be solved if I switch margin-bottom to padding-bottom but what if I need margin-bottom?

How to fix this?

like image 566
janosrusiczki Avatar asked May 12 '10 10:05

janosrusiczki


1 Answers

This is a feature known as collapsing margins. See: http://www.w3.org/TR/CSS2/box.html#collapsing-margins

The easiest way around (aside of replacing the margin with padding as you suggest) is to add a small (1px) invisible border or padding to the outer element.

like image 129
RoToRa Avatar answered Sep 19 '22 13:09

RoToRa