Float:right reverses order of spans

Css

Css Problem Overview


I have the HTML:

<div>
    <span class="label"><a href="/index/1">Bookmix Offline</a></span>
    <span class="button"><a href="/settings/">Settings</a></span>
    <span class="button"><a href="/export_all/">Export</a></span>
    <span class="button"><a href="/import/">Import</a></span>
</div>

and CSS:

span.button {
    float:right;
}

span.label {
    margin-left: 30px;
}

In the browser, spans display in the reverse order: Import Export Settings. Can I change the order by changing only the CSS file and leave the HTML as is?

Css Solutions


Solution 1 - Css

The general solution to this problem is either to reverse the order of the right floated elements in the HTML, or wrap them in a containing element and float that to the right instead.

Solution 2 - Css

If you want to make your list items aligned right without reversing the order dont float your list items. Make them inline instead. text-align:right your span

div {
    text-align:right;
}

span {
    display:inline;
}

Solution 3 - Css

Yes, this can be done with your exact markup.

The trick is to use direction: rtl;

Markup

<div>
    <span class="label"><a href="/index/1">Bookmix Offline</a></span>
    <span class="button"><a href="/settings/">Settings</a></span>
    <span class="button"><a href="/export_all/">Export</a></span>
    <span class="button"><a href="/import/">Import</a></span>
</div>

CSS

div
{
    direction: rtl;   
}

span.label {

    float: left;
    margin-left: 30px;
}

FIDDLE

Solution 4 - Css

div.outerclass {
  text-align: right;
}

div.outerclass .label {
  float: left;
   text-align: left;
}

Solution 5 - Css

Update: Two lightweight CSS solutions:

Using flex, flex-flow and order:

Example1: Demo Fiddle

    div{
        display:flex;
        flex-flow: column;
    }
    span:nth-child(1){
        order:4;
    }
    span:nth-child(2){
        order:3;
    }
    span:nth-child(3){
        order:2;
    }
    span:nth-child(4){
        order:1;
    }

Alternatively, reverse the Y scale:

Example2: Demo Fiddle

div {
    -webkit-transform: scaleY(-1);
    transform: scaleY(-1);
}
span {
    -webkit-transform: scaleY(-1);
    transform: scaleY(-1);
    display:inline-block;
    width:100%;
}

Solution 6 - Css

very easy!

div.outerclass {
	float: right;
}

div.innerclass {
	float: left;
}

Solution 7 - Css

Just use display:inline-block and text-align:right instead of float:right:

div { text-align:right }

span { display:inline-block }

Solution 8 - Css

Float the div right instead of the span elements?

Solution 9 - Css

Or you can use the flex display

div {
  display: flex;
  justify-content: flex-end;
}

<div>
  <span class="label"><a href="/index/1">Bookmix Offline</a></span>
  <span class="button"><a href="/settings/">Settings</a></span>
  <span class="button"><a href="/export_all/">Export</a></span>
  <span class="button"><a href="/import/">Import</a></span>
</div>

Solution 10 - Css

Answering your updated question:

path to div {
    text-align: right;
}
path to div span.label {
    float: left;
}

I say "path to div" above because you need to adequately target this one div, which doesn't (according to your quoted code) have a class or ID of its own. That's not a problem if we can do it with structure.

So for instance, if the div is the only div within a container with the id (say) "navigation", that might be:

#navigation div {
    text-align: right;
}
#navigation div span.label {
    float: left;
}

Or if there are other divs in the container, but this is the only one that's a direct child of the navigation container, you can use a child selector instead of a descendant selector:

#navigation > div {
    text-align: right;
}
#navigation > div span.label {
    float: left;
}

Solution 11 - Css

This works without rearranging the html and is very simple. Heres a working example: http://jsfiddle.net/dzk7c/2/

html:

<div>
    <span class="label"><a href="/index/1">Bookmix Offline</a></span>
    <span class="button"><a href="/settings/">Settings</a></span>
    <span class="button"><a href="/export_all/">Export</a></span>
    <span class="button"><a href="/import/">Import</a></span>
</div>

and css:

div {
    float:right;
}

span {
    display: inline-block;
}

Solution 12 - Css

maybe this, for someone...

http://jsfiddle.net/15abbg81/

only horizontal, may be to use: rtl/ltr of tables should work in all browsers that support table-cell css

normal:

<div class="container">
    <section>1</section>
    <article>2</article>
    <aside> <span>א</span><span>.</span> </aside>
</div>
<style>
.container { display: table; 
    direction:ltr; /*ltr here is not required*/
    }
.container>* {
    display: table-cell; /*only works with table-cell*/
    direction:ltr; /*ltr here is not required*/
}
</style>

reverse:

<div class="reverse">
    <div class="container">
        <section>1</section>
        <article>2</article>
        <aside> <span>א</span><span>.</span> </aside>
    </div>
</div>

<style>
    .reverse .container { display: table;  direction:rtl } /*change the outside direction to rtl*/
    .reverse .container>* {
        display: table-cell;
        direction:ltr /*inside switch back to ltr if needed*/
    }
</style>

    

Solution 13 - Css

It might be achieved using flex margin positioning. Please note that this will work only on IE10+

div
{
    display: flex;  
}

span.label {
    margin: auto;
    margin-left: 0;
}

span.button {    
    margin-right: 5px;
}

Solution 14 - Css

I get also stuck with this... so here is my sollution

I had this Before and want this After

HTML looks like this

<ul class="visible-xs restaurant-thumbnail__tags list-inline">
    @foreach($restaurant->categories as $category)
        <li>{{ $category->title }}</li>
    @endforeach
    @foreach($restaurant->labels as $label)
        <li>{{ $label->title }}</li>
    @endforeach
</ul>

CSS looks like this

.restaurant-thumbnail__tags {
    direction: rtl;
    -webkit-transform: scaleY(-1);
    transform: scaleY(-1);
}
.restaurant-thumbnail__tags li {
    -webkit-transform: scaleY(-1);
    transform: scaleY(-1);
}

Solution 15 - Css

html

<div>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>

css

div{ 
    width:300px; 
    border:1px solid #000; 
    overflow:hidden; 
}
ul{
    padding:0;
    margin:0;
    float:right;
}
li{
    border:1px solid #000;
    text-align:center;
    float:left;
    margin:20px 5px;
    padding:5px;
    width:25px;
    list-style:none;
}

Solution 16 - Css

you would need to float the spans left, and float the parent container right.

this will depend on the kind of layout you are trying to achieve however.

it's intended behaviour, for the benefit of screen readers and the like I imagine. the other option is to adjust the mark-up, but this, as you say, may not be desirable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncethView Question on Stackoverflow
Solution 1 - CssMarcus WhybrowView Answer on Stackoverflow
Solution 2 - CssDil AView Answer on Stackoverflow
Solution 3 - CssDanieldView Answer on Stackoverflow
Solution 4 - CssRoToRaView Answer on Stackoverflow
Solution 5 - CssSW4View Answer on Stackoverflow
Solution 6 - CssxxxbenceView Answer on Stackoverflow
Solution 7 - CssMikeView Answer on Stackoverflow
Solution 8 - CssDan GrossmanView Answer on Stackoverflow
Solution 9 - Cssng trong MinhView Answer on Stackoverflow
Solution 10 - CssT.J. CrowderView Answer on Stackoverflow
Solution 11 - CssJ GroverView Answer on Stackoverflow
Solution 12 - CssShimon DoodkinView Answer on Stackoverflow
Solution 13 - CssMachetView Answer on Stackoverflow
Solution 14 - CssPavel BerkaView Answer on Stackoverflow
Solution 15 - CssUuUView Answer on Stackoverflow
Solution 16 - CssRossView Answer on Stackoverflow