Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why clear: right doesn't work as intended

Tags:

css

css-float

I'm always confused by clear: left, clear: right and clear: both in CSS. I know clear: both means it doesn't allow floating elements on both sides of itself.

I did some testing here. I thought the layout would appear like below, because B uses clear: both. But it doesn't. Could someone tell me why?

A
B
CD

Updated (Post the code)

<div class="container">
    <div class="A">a</div>
    <div class="B">b</div>
    <div class="C">c</div>
    <div class="D">d</div>
    <div class="CB"></div>
</div>

.container{
    width:100%;
    border:1px solid red;    
}
.B{
    float:left;
    clear:both;
    width:10%;
    height:30px;
    border:1px solid blue;
}
.A,.C,.D{
    float:left;
    width:10%;
    height:30px;
    border:1px solid blue;
}
.CB{
    clear:both;
}
like image 899
Joe.wang Avatar asked Apr 18 '13 10:04

Joe.wang


People also ask

What are two valid techniques used to clear floats?

Left and Right can be used to only clear the float from one direction respectively. None is the default, which is typically unnecessary unless removing a clear value from a cascade.

Is float still used in CSS?

The float property still exists in CSS as it did when Internet Explorer was a young browser, and still works the same.

Can a div float over another?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


1 Answers

clear on an element only clears the floats before it in document order. It doesn't clear floats after it. The left and right values mean clearance of left floats and right floats preceding an element respectively. They don't mean clearing floats before and after the element.

Since C is being floated, but doesn't have any clearance being applied, it floats next to B. B does not try to clear C because C comes after it in the document structure.

Furthermore, clear: right doesn't have any effect in your test case because none of your elements are being floated to the right anyway.

like image 55
BoltClock Avatar answered Oct 26 '22 06:10

BoltClock