tags:

views:

73

answers:

5

I hardly use float:right in my css and now I did and came across a annoying problem. I am floating my menu items to the right

my HTMl

    <ul id="extMenu">
        <li>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="#">Feedback</a>
        </li>
        <li>
            <a href="#">Contact</a>
        </li>
    </ul>

my CSS

    #extMenuBlock {
    }
        #extMenuBlock ul#extMenu { 
            list-style:none;
            padding:0;
            margin:0; 
        }
        #extMenuBlock ul#extMenu li { 
            float:right;
            padding:5px; 
        }

Now when the items are float, I receive my menu is this order Contact Feedback Home, but I want them in opposite order ie Home Feedback Contact

+1  A: 

Why did you choose to float to the right? Floating multiple items to the right means the first item will be the most right item and the last floated item will be on the left of your items.

The align your menu to the right and have your items on one line i would use this:

#extMenuBlock {
    text-align: right;
}
#extMenuBlock ul#extMenu {
    list-style:none; padding:0; margin:0;
}
#extMenuBlock ul#extMenu li {
    display: inline-block; padding:5px;
}
codescape
@codescape, Cause I want them on the right side
Starx
Thanks, I was missing this information in your question. I adjusted my answer to provide a solution for your requirement.
codescape
+1, @codescape, works perfect but not ie compatible.
Starx
Thanks for this alternative, But my actual question is still unanwered, How to change the ORDER of the floated element?
Starx
+3  A: 

Try this

ul#extMenu {
    list-style: none;
    padding: 0;
    margin: 0;
    float: right;
}
ul#extMenu li {
    display: inline;
    padding: 5px;
}
dDejan
+1 Good answer - could just use text-align: right on the UL though.
Sohnee
@dDejan, works pretty much but the padding is not working as intended, the top and bottom padding is escaped
Starx
@Starx - perhaps my solution below will help then - it doesn't use any floats and the padding appears to be preserved.
Sohnee
Thanks for this alternative, But my actual question is still unanwered, How to change the ORDER of the floated element?
Starx
@Starx, as far as I know it is not possible to change the order of the floats
dDejan
A: 

What you really want to do is make each li element an inline element, and float the ul as a whole to the right (or use text-align: right, if you prefer).

New CSS:

#extMenuBlock {
    float:right;
}
#extMenuBlock ul#extMenu { 
    list-style:none;
    padding:0;
    margin:0; 
}
#extMenuBlock ul#extMenu li { 
    display:inline;
    padding:5px; 
}
cmrn
Thanks for this alternative, But my actual question is still unanwered, How to change the ORDER of the floated element?
Starx
@Starx this code should create the correct order?
cmrn
@cmm, no vertical padding dude, how to preserve it
Starx
A: 

My variation on the answer - no floats at all.

ul#extMenu {
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: right;
}
ul#extMenu li {
    display: inline;
    padding: 5px;
}
Sohnee
@sohnee, this is definately a better solution than that of @dDejan, avoiding float, but the padding problem still remains. Why is the top and bottom padding being avoided? Got an answer
Starx
@Starx: See my answer, thats why I am using `display:inline-block` and not `display:inline`. You cannot give inline elements any padding.
codescape
Thanks for this alternative, But my actual question is still unanwered, How to change the ORDER of the floated element?
Starx
@Starx - which padding in particular. I'm working in Firefox and if I increase the padding: 5px, I see the padding increase on screen. I should be able to answer if you let me know the exact padding issue. Cheers.
Sohnee
@sohnee, Vertical padding ie top and bottom
Starx
@Sohnee: Here is some example: http://jsfiddle.net/KhJAc/
codescape
You could solve that by adding the same padding to the UL:ul#extMenu { padding: 5px 0; ...
Sohnee
+2  A: 

Using display: inline on <li>'s can cause problems, especially if you're eventually going for dropdown menus. I'd recommend something similar to this (only floats show, you know what other styles you want to add):

#extMenu { float: right; }
#extMenu li { float: left; }

So the menu itself will float to the right, while the menu items will float to the left.

Another solution would be to simply reverse the order of your <li>'s

Ryan Kinal
"Using display: inline on <li>'s can cause problems" - I'd be interested in hearing more detail on this, perhaps reference a source for this.
Sohnee
+1 @Ryan, Nice one. This works and is IE compatible. But I would like to hear something related to changing the order of the floated elements
Starx
Re: Sohnee - http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.3 "Generally, block-level elements may contain inline elements and other block-level elements. Generally, inline elements may contain only data and other inline elements." Since the <li>'s are now inline, they can't contain block elements, like <div>'s and <ul>'s.
Ryan Kinal
Re: Starx - Yeah, I was looking for a way to change the order, but I don't think it's actually possible, as floated elements "stack up" on each other. The only real way to change the order is to switch up the HTML, which isn't always possible.
Ryan Kinal
@Ryan Kinal - the example in the question does not contain any block level elements inside of the list item elements, meaning you are coding to a complexity that does not exist.
Sohnee
@Sohnee True. I was, however, coding in a forward-thinking manner. If, at some point in the future, a dropdown needs to be added, then this code will not break.
Ryan Kinal