Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does floating and clearing on the same element work?

I was brushing up on my CSS skills earlier and I came across a tutorial that has me going bald. Here's the link: http://css.maxdesign.com.au/floatutorial/tutorial0304.htm I have spent hours researching for an answer to this but haven't came across anything that helped me.

The author of the tutorial does not give any good details to what is going on with elements. I understand that floating lifts an element off the page and floats it in the given direction (left / right). Clearing resets the floated element and places it back into the flow of the page.

How are those elements still floated to the right if the right side has been cleared? I don't understand the logic behind this. Any clarification on this would be deeply appreciated and maybe I can keep some of my hair.

Edit: Here is CSS code and a screenshot from the tutorial linked above...

.floatright {
    float: right;
    margin: 0 0 10px 10px;
    clear: right;
}

p { margin-top: 0; }

enter image description here

like image 850
W3Geek Avatar asked Oct 08 '22 03:10

W3Geek


1 Answers

You misunderstand clearing. clear: right simply means that the element should vertically clear all previously right-floated elements in the same context, in other words that it should drop below all other right-floated elements. Visualization:

|                          |
|            +-----++-----+|
|            |  A  ||  B  ||
|            |     ||     ||
|            +-----++-----+|
|                   +-----+|
|                   |  C  ||
|                   |     ||
|                   +-----+|
|                          |

All elements are floated right, but C is set to clear: right, so it drops below the previous floated elements.

like image 134
deceze Avatar answered Oct 13 '22 10:10

deceze