I'm trying to fix margins in existing HTML/CSS (fixing with CSS overrides), and I'm having trouble understanding the case I'm working with (boiled down to the code below).
What I want: no padding above the first 'div.floating' (the padding that comes from 'p.par' for some reason - why?). What confuses me: the padding is gone if I remove div#extra (or if '.floating-column' isn't floating). Why? Are there rules for collapsing margins involved that are beyond my understanding?
(Also, changing the HTML is not an option.)
Thanks for help, Manuela
ps: the code:
<!DOCTYPE html>
<html>
<head>
<style>
html, body, div, p {
border:0;
margin:0;
padding:0;
}
.container {
width:960px;
margin:0 auto;
background-color: #ddd;
}
.container:after {
content:"";
display:block;
height:0;
clear:both;
visibility:hidden;
}
.floating-column {
float:left;
border: 1px dashed green;
}
.floating {
width: 100%;
float: left;
border: 1px dotted black;
}
.par {
margin: 10px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="container">
<div class="floating-column">
<div id="extra">
<div class="floating">first float</div>
<div class="floating">another float</div>
<p class="par">Cras nibh erat, tempor non sagittis ac, vulputate ac lectus. Praesent sed fermentum eros. Mauris sodales suscipit diam, a congue dui commodo ac. Sed convallis rutrum ligula, vitae sollicitudin nisl porttitor nec. Mauris tempor fringilla imperdiet. Pellentesque interdum, tellus nec venenatis venenatis, nisi nulla ultricies leo, tincidunt venenatis risus nisl ac dolor. Donec ante leo, elementum et faucibus tincidunt, dignissim lacinia libero. Suspendisse pharetra in augue in sodales. Etiam luctus dui blandit nisi dictum pretium.</p>
</div>
</div>
</div>
</body>
</html>