Reputation: 9319
In the snippet, the two divs should appear below each other, as if seperated thru a line break. Instead, the two divs are kind of next to each other, although the second one is a bit lower than the first. Why is that? How do I get the <hr> to line break?
<!DOCTYPE html>
<head><title>DoodleDebug</title>
<style>hr{padding:2pt;border:0;}span{float:left;width:40px;}.ColorPlugin{}</style>
</head>
<body>
<div><span style="background-color:#1a63cb">   </span>
</div>
<hr>
<div><span style="background-color:#1a63cb">   </span>
</div>
I'm using the latest Firefox.
Upvotes: 3
Views: 5889
Reputation: 107052
The problem is with the float
ing span
s. They are taken outside of the normal flow (sort of), so the div
s end up being 0 height. Well, the second one does, the first one is still a few px high because of the hr
. Anyways, both div
s together are much smaller than the span
s which overflow them. Hence the second span
is much higher than you would expect. And since float
s don't overlap each other, the second span
is placed a little to the right to acommodate that (while still retaining the original vertical position).
A solution then depends on what you want to achieve. I understand that this is a dumbed-down example, so I can't see the bigger picture. My first attempt would be to get rid of the float
entirely. Most of the time they create more headaches than solutions anyway. Options here are to use display: inline-block
or just rearranging your HTML somehow.
The second attempt would be to use clear
, which was suggested below by Brad Christie.
Upvotes: 9
Reputation: 13038
Because there is a float:left property in your code, remove it & you have your desired effect.
<style>hr{padding:2pt;border:0;}span{width:40px;clear:both;}.ColorPlugin{}</style>
</head>
<body>
<div><span style="background-color:#1a63cb">   </span>
<hr>
<div><span style="background-color:#1a63cb">   </span>
Upvotes: 1
Reputation: 75409
Your're floating your span tags instead of your divs, which are separated by a containing div. Add div { float:left }
instead to float the actual span containers..
Upvotes: 2
Reputation: 101614
It's your float that's interrupting how it displays. You're telling the browser to display the contents next to each other when you specify a float. However, if you apply a clear:both
on the <hr>
you get the desired effect:
<!DOCTYPE html>
<html>
<head>
<title>DoodleDebug</title>
<style>hr{padding:2pt;border:0;}span{float:left;width:40px;}.ColorPlugin{}</style>
</head>
<body>
<div><span style="background-color:#1a63cb">   </span></div>
<hr style="clear:both;">
<div><span style="background-color:#1a63cb">   </span></div>
</body>
</html>
Upvotes: 4
Reputation: 3996
hr - is a horizontal line and no line break .. for a line break use
<br>
Upvotes: 1