Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do elements need to be floated right before left when in the same container with a centred element?

I had a problem with a right floated element not staying on the same horizontal alignment as a left floated element and a centred element. The right floated element was floating right, but sitting below the horizontal line the others were on.

When floating elements (for example one left, one centred and one right) in the same div, the right element needs to stack before the left element.

Example:

<div id=container>
    <div id="float-right"></div>
    <div id="margin-auto"></div>
    <div id="float-left"></div>
</div>

If I have only two elements, one left and one right they sit horizontally as below.

See example: jsfiddle

When I add a central element the right element moves down.

See example: jsfiddle

What is happening here ?

like image 517
Aaron Avatar asked Jul 27 '13 21:07

Aaron


People also ask

What keeps the element float on left side of the container?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

What property makes elements float to the right or left of the screen positioned where they are in the HTML?

One way to position elements on a page is with the float property. The float property is pretty versatile and can be used in a number of different ways. Essentially, the float property allows us to take an element, remove it from the normal flow of a page, and position it to the left or right of its parent element.

What is the purpose of clearing a float?

Purpose of clearing floats in CSS: We clear the float property to control the floating elements by preventing overlapping. On our webpage, if an element fits horizontally next to the floated elements, unless we apply the clear property same as float direction then the elements will be a move below the floated elements.

How do you center an element with a float?

The CSS float property is used to set or return the horizontal alignment of elements. But this property allows an element to float only right or left side of the parent body with rest of the elements wrapped around it. There is no way to float center in CSS layout.


3 Answers

I will provide you with a simple example here, let's assume that you are not assigning the middle div any width so see what it will be actually doing

Demo

<div style="width:100%">
   <div style="width:20px;height:20px;background-color:red;float:left;border: 3px solid #000;"></div>
   <div style="height:20px;background-color:red;margin:0 auto auto;border: 3px solid #000;"></div>   
   <div style="width:20px;height:20px;background-color:red;float:right;border: 3px solid #000;"></div>
</div>

Why this happens?

div is a block-level element, it takes up the entire horizontal space on the page, if you know, when you float any element either left or right it won't take 100% anymore and it will take only the space assigned by using width, or the content it holds, so in this case, left floated div will take 20px width leaving other space unused. Now you have another div which IS NOT FLOATED but it will take the rest of the available horizontal space, making your right floated div element to push down.

So what to do in order to solve this?

You need to float all the div to the left, or it may be enough if you make the middle div float to the left or to the right. Now I am aware that you want to have 2 div, 1 floated to left and other to right, but this is not the right way to do that, if you want, you can wrap the elements inside a container div, or what you can use is position: absolute; to set the elements right.


In order to show you how block level elements work, I will share you another example here..

Assume that you are having a div nested inside a p tag (This is invalid so please never use this in real world, this is just for demonstration purpose), and give some width to the div element and see how it renders your text.

<p>Hello World, I don't want the <div>text to</div> break</p>

div {
    width: 40px;
    background: #f00;
}

Demo 2

Though you provide the width to block level element, it will still break the paragraph.


From w3c

By default, block-level elements are formatted differently than inline elements. Generally, block-level elements begin on new lines, inline elements do not.

like image 110
Mr. Alien Avatar answered Nov 15 '22 05:11

Mr. Alien


This happens because of the way elements are floated according to the HTML spec. Elements are floated horizontally from left to right. Any floated element will appear as far left as it possibly can.

Since div is a block level element, it will push everything down on the right hand side, but stack against anything on the left.

In your example, you can not see it, but the floated, or centre, div extends all the way out to the edge of the page as demonstrated by @Mr. Alien's fiddle.

You can read more about this in the spec:

http://www.w3.org/wiki/Floats_and_clearing

like image 20
Mohamad Avatar answered Nov 15 '22 03:11

Mohamad


div is a block level element, it is making your right div push down as it is not floated, either on left or on the right side, and hence it pushes your other div below.

If you float that too, it will make the rest of the space on other side empty, thus it will allow the right floated div to sit beside the middle div

http://jsfiddle.net/mSXX6/2/

like image 39
Blowfish Avatar answered Nov 15 '22 03:11

Blowfish