CSS Clear: Left breaks into new line while tryng to split elements into 2 groups
Asked Answered
I

4

5

JSFiddle Link: here

div{
  width:50px;
  height:20px;
  
}
#container{
  border:5px solid black;
  width:400px;
  height:200px;  
}

#a{
  background-color:red;
  float: left;  
}
#b{
  background-color:blue;
  float: left;
  clear: left;
}
#c{
  background-color:green;
  float: right;
}
#d{
  background-color:orange;
  float: right;
  clear: right;
}
#e{
  background-color:black;
  float: right;
  clear: right;
}
<div id="container">

<div id="a">
</div>
<div id="b">
</div >
<div id="c">
</div>
<div id="d">
</div>
<div id="e">
</div>

</div>

Following image is the end result, Where you can see that, the elements on the right are not starting from the top position. There is a 1 brick space at the top. Why is that ? What is the logic ?

enter image description here

Intricacy answered 24/7, 2018 at 18:28 Comment(0)
L
3

If you are using float alone, without specifying positions/margins, you'll have to rely on the browsers way of piling / blocking stuff together.

A work around in your code would be changing the C element order, so it can pile in a proper position (like putting together a bunch of lego blocks):

<div id="a"></div>
<div id="c"></div>
<div id="b"></div>
<div id="d"></div>
<div id="e"></div>
Lowry answered 24/7, 2018 at 18:53 Comment(0)
B
1

Floats do not automatically go to the top of the container. The Red element takes his place on the left. The Blue element floats to the left as well, but it's cleared on the left, so it's pushed to the next line. Then you have the Green element with float right, since Blue is cleared on the left, not on the right, it takes the place on the right on the same line.

In short, it floats where it should on the closest availible spot, not at the top-most one.

I hope I'm being clear enough, but if it is still confusing, please, let me know.

Bascom answered 24/7, 2018 at 18:46 Comment(0)
I
1

Clear plays no direct part in this rule, and is simply affecting the position of the second left floated div, from which the first right floated div takes its position.

For example, the same 1 brick space appears if the second floated left div is on a second line for other reasons, such as if the width of the first two divs exceeds 100%.

div{
  width:30%;
  height:20px;

}
#container{
  border:5px solid black;
  width:400px;
  height:200px;  
}

#a{
  background-color:red;
  float: left;
  width:60%;
}
#b{
  background-color:blue;
  float: left;
  width:45%;
}
#c{
  background-color:green;
  float: right;

}
#d{
  background-color:orange;
  float: right;
  clear: right;
}
#e{
  background-color:black;
  float: right;
  clear: right;
}
<div id="container">    
<div id="a">
</div>
<div id="b">
</div >
<div id="c">
</div>
<div id="d">
</div>
<div id="e">
</div>    
</div>

Step 5 in the CSS2.2 rules for floats says:

The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.

So the highest place that the first right floated div can be is level with the second left floated one.

Inanition answered 24/7, 2018 at 18:54 Comment(0)
S
1

I'd suggest using position: absolute; instead of float.

Also remember that an element with position: absolute; is relative to first parent that has position: relative;

Here is an worknig example:

div {
  width: 50px;
  height: 20px;
}

#container {
  border: 5px solid black;
  width: 400px;
  height: 200px;
  position: relative;
}

.float {
  position: absolute;
  top: 0;
}

.right {
  right: 0;
}

.left {
  left: 0;
}

#a {
  background-color: red;
}

#b {
  background-color: blue;
  top: 20px;
}

#c {
  background-color: green;
}

#d {
  background-color: orange;
  top: 20px;
}

#e {
  background-color: black;
  top: 40px;
}
<div id="container">
  <div id="a" class="float left"></div>
  <div id="b" class="float left"></div>
  <div id="c" class="float right"></div>
  <div id="d" class="float right"></div>
  <div id="e" class="float right"></div>
</div>
Slog answered 25/7, 2018 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.