Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float div's to the right in left-to-right order?

Tags:

html

css

I have multiple div's I want to display in a horizontal row. Normally, the way I'd do this is to simply float them to the right and put them in the markup in reverse order like so:

<div>   <div style="float:right">Right</div>   <div style="float:right">Middle</div>   <div style="float:right">Left</div> </div> 

I'm trying to accomplish a similar thing, float div's to the right, but I can't reverse their order in the markup for SEO reasons. The left div needs to be first in the code.

Is there a simple way to do this without resorting to positioning things absolutely?

like image 726
Jake Wilson Avatar asked Feb 14 '12 16:02

Jake Wilson


People also ask

How do you float elements side by side?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

What is the position of the float?

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).

In what direction does float will work IMG float right?

The elements after the floating element will flow around it. The elements before the floating element will not be affected. If the image floated to the right, the texts flow around it, to the left and if the image floated to the left, the text flows around it, to the right.


2 Answers

You could apply a text-align: right to the container and a display: inline-block in place of the floating:

<div style="text-align: right">   <div style="display:inline-block">Left</div>   <div style="display:inline-block">Middle</div>   <div style="display:inline-block">Right</div> </div> 

DEMO

like image 100
Didier Ghys Avatar answered Sep 29 '22 06:09

Didier Ghys


Using display:inline-block might not work as expected with elements of variable height.

So you might want to use:

<div style="float: right">       <div style="float:left">Left</div>       <div style="float:left">Middle</div>       <div style="float:left">Right</div> </div> 

See: demo of both -- inline and float-float.

like image 39
Nux Avatar answered Sep 29 '22 04:09

Nux