Nice.
Impacts:
(1) div background color vanished.
(2) P element’s text content sits next to the <img> elelement.
Resolution:
added clear:both in <p> css. This restores div background color. and puts down the block element <p> to the new line of <img> elelement.
The div background colour vanished, but only because it was covered up by the p’s background colour. The div was only as big as the p was, because all it saw was the p and encloses it.
Remove the p’s colour to see the div again:
<html>
<head>
<style type="text/css">
div {
background-color: #00f;
}
img
{
float:left;
border:1px solid red;
}
[b]p{
}[/b]
</style>
</head>
<body>
<div>
<img src="logocss.gif" width="95" height="84" alt="css" />
<p>Lots n lotsa text... lots n lotsa text... en nog meer tekst </p>
</div>
</body>
</html>
Now it will be blue exactly where it was yellow. (I moved styles over to the css section… might as well try to keep it clean)
When the p has clear: both; on it, the div is still as large as the p it holds but there is now space between where the top of the div is and the bottom of the P. The div still doesn’t actually “see” the image, but it does “see” the non-floated child P and so still encloses that p.
This is the source of the original float-clearing trick: the clearing div or clearing br.
It’s considered “bad practice” today but it always works:
<div>
<float/>
<float/>
<br class=“clear”>
</div>
br.clear {
clear: both;
}
Today there are other ways of making the div enclose its floats. We try to avoid the clearing-br or clearing-div because it’s adding markup to the HTML page, not for content but to get the CSS to work. So today we use pure-CSS techniques.
But when I do this experiment…
I see a gap between <p> element and end of <div> element . I don’t understand why there is gap at the bottom …between <p> and end of <div>
This one gets trickier: when the div just has a background-color (or background-image), it only has to put that background colour or image on the actual area where it is. This is called the “padding box”.
But when you add a border to the div, the div must enclose all parts of its children, because now it has to show the “border box”. The gap you see is from the natural/default margin of the p. You can see this when you
<html>
<head>
<style type="text/css">
img
{
float:left;
border:1px solid red;
}
p{
clear:both;
[b]margin: 0;[/b]
background-color:#ff0;
}
</style>
</head>
<body>
<div style="background-color:blue;border:2px solid pink;">
<img src="logocss.gif" width="95" height="84" alt="css" />
<p>Lots n lotsa text... lots n lotsa text... en nog meer tekst </p>
</div>
</body>
</html>
Margins are not part of the “padding box” of an element, so when you give that element a background colour or image, the margins don’t get coloured (the padding, if any, does). So you never saw the default margin on the p (most browsers add default top and bottom margins on P elements). But a container with a border, who must show its “border box”, must contain all parts of its children, including their (vertical) margins!
The exception to this is top margins: they may collapse (see Margin collapse) so if we have the p at the top (before the img) and it has a top margin and we give a border to the div, you won’t see a gap above the p (you woul expect to see some of the div’s blue to peek out). Instead, the margin collapses into the div itself and you instead get a gap above the div (the margin moves to outside the div). I think margin-collapse is confusing so I just keep in mind that whenever I have strange gaps and I have top margins on things, I can stop margin collapse by either adding 1px of padding there or 1px of border to the parent (if the p has a border or padding, its margin isn’t allowed to collapse). If I use a border I’ll make it invisible so you don’t see it.
So here’s the p with 20px top-bottom margin:
<html>
<head>
<style type="text/css">
body {
background-color: #f00;
}
div {
background-color: #00f;
}
p{
margin: 20px 0;
background-color: #ff0;
}
img {
float:left;
border:1px solid red;
}
</style>
</head>
<body>
<div>
<p>Lots n lotsa text... lots n lotsa text... en nog meer tekst </p>
<img src="logocss.gif" width="95" height="84" alt="css" />
</div>
</body>
</html>
Add a thin 1px border to the top of the div, and suddenly there’s a whole lot more space there:
<html>
<head>
<style type="text/css">
body {
background-color: #f00;
}
div {
background-color: #00f;
[b] border-top: 1px solid #00f;[/b]
}
p{
margin: 20px 0;
background-color: #ff0;
}
img {
float:left;
border:1px solid red;
}
</style>
</head>
<body>
<div>
<p>Lots n lotsa text... lots n lotsa text... en nog meer tekst </p>
<img src="logocss.gif" width="95" height="84" alt="css" />
</div>
</body>
</html>