This question already has answers here:
Float:right reverses order of spans
(16 answers)
Closed 6 years ago.
So I've run into this problem a few times, how come my navigation menu is reversed?
.navigation li a {
display: inline-block;
text-decoration: none;
float: right;
margin-right: 10px;
}
ul {
list-style-type: none;
}
<div class="container-fluid">
<nav class="navigation">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>
</div>
When put into action, the menu order is reversed, why is this happening?
You let the elements float to the right. Try to picture that..
The first element enters the document and floats all the way to the right, bumping into the side of the screen. The second enters and floats to the right as well, but bumps into the left side of the first, and stays there. And so on, and so on.
A better solution would be to use display: inline-block for the elements, and float-right for the parent (the ul).
But personally I'm not a big fan of floating at all, so I would use text-align on the ul. text-align keeps it a normal line of text, which can also include images or other inline and inline-block elements. By right-aligning the text, the order of the words is not changed, but any remaining white space is just added to the left instead of to the right of the line:
.navigation li a {
display: inline-block;
text-decoration: none;
float: right;
margin-right: 10px;
}
.navigation li {
display: inline-block;
}
.navigation ul {
text-align: right;
}
<div class="container-fluid">
<nav class="navigation">
<ul>
<li>About
</li>
<li>Portfolio
</li>
<li>Contact
</li>
</ul>
</nav>
This is happening because that's how float was designed. It will "float" the elements in the direction specified, in the order specified. If order is important, you can fix this in three ways:
Use the built-in Bootstrap alignment tools.
Apply float: right to the parent <ul> rather than each link.
Reverse the order of the elements.
In this case, you are floating elements to the right, in the order About > Portfolio > Contact. This is what happens:
About is floated right with no previous elements, so it sits against the farthest right wall of the parent container, <ul>.
Portfolio is floated right, but About is there already, so it gets as far right as it can, which puts it to the left of the previous element.
Contact is last to the party, so it ends up at the end of the line, farthest left.
Related
I'm slowly getting to grips with Flex but it has got me confused how I can make all li elements within a ul.list float right. I've read that in order to get the item floating right you simply use margin-left: auto however this only seems to work correctly on the last item, because when I resize the viewpoint to above 1920px or above the gap between the email and telephone number increases... while the left side of the top menu but remains the same distance between each li element.
Here is my HTML:
<header class="ProMenu">
<nav class="row align-middle expanded">
<div class="small-12 medium-4 columns Links">
<ul class="menu">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
<div class="medium-4 columns Logo">
<img src="https://placehold.it/64x42" alt="">
</div>
<div class="medium-4 columns Contact">
<ul class="menu">
<li>Phone: 0777123456</li>
<li>Email: jog#blogs.com</li>
</ul>
</div>
</nav>
</header>
And my CSS:
.ProMenu {
background: #0071c5;
width: 100%;
}
.ProMenu .row {
min-height: 60px;
max-width: 100%;
}
.ProMenu ul li a {
color: #FFF;
}
.ProMenu .Logo {
text-align: center;
}
.Contact ul li{
margin-left: auto;
}
JSFiddle:
And to make things easier I've uploaded the code to a online JSfiddle, to replicate make the window big and monitor the distance between both the mobile number and email increases... something that I want to stop.
You want to utilize the foundation .align-right class, which will apply justify-content: flex-end;
https://jsfiddle.net/28btx50v/2/
Remove the margin-left: auto from the last child. Put it only on the second to last child. That will force both elements to the right.
When a flex item has margin-left: auto, it pushes itself away from everything on its left. In your case, that makes sense for the second to last child. But it doesn't make sense for the last child.
That's why when you widen the screen you get a wider separation between the two.
By using margin-left: auto only on the second to last item, you pack both items together and everything shifts right. You can then use regular (numerical) horizontal margins, if necessary, to create space between them.
I'm trying to work with grids which are whole numbers, that perfectly match up, e.g 20% for 5 menu items would fill 100%.
I have the following code:
#menu-primary-navigation li{display:inline-block;font-size:0;width:20%;}
#menu-primary-navigation li a{font-size:22px;}
I understand the issue of whitespace between ul li elements that means 20% elements will not fit into 100% due to the white space in between each element. However I thought I had got around this issue by making font-size:0; where the elements sit and then only setting the font size inside the element.
Yet you can see in my JSFiddle that the five menu items still do not fit perfectly to 100% of the container.
In my example, the width is 600px and each element is 20%. I can see from inspecting the elements that they are all equal to 120px. Well 120 x 5 = 600, so why does the last element always fall to the next line?
<div class="wrapper">
<ul id="menu-primary-navigation" class="menu">
<li class="menu-item">
Home
</li>
<li class="menu-item">
Tutorials
</li>
<li class="menu-item">
<a>Logo</a>
</li>
<li class="menu-item">
Projects
</li>
<li class="menu-item">
Articles
</li>
</ul>
</div>
http://jsfiddle.net/franhaselden/kq9o4t0v/
Using display: inline-block; is notoriously problematic in this situation. You could consider using display: table-cell; achieve the same result with less hassle.
HTML can remain the same, but try this CSS:
.wrapper{width:600px;}
ul,li{padding:0;margin:0;}
/* needed for use with display: table-cell; */
.menu{ display: table; width: 100%; }
/* changed to table-cell */
#menu-primary-navigation li{display:table-cell; text-align: center;}
#menu-primary-navigation li a{font-size:22px;}
However as I am supposed to be answering the question the part you went wrong is you need to set the font size on the parent i.e the ul in order to effect the white-space. Do note however, I think certain versions of IE will not like font-size: 0;
CSS:
.wrapper{width:600px;}
/* you need it set on the parent */
ul,li{padding:0;margin:0; font-size: 0; }
#menu-primary-navigation li{display: inline-block; width: 20%; text-align: center;}
#menu-primary-navigation li a{font-size:22px;}
You are setting font-size:0 on the li tag to remove whitespace. However the li's are contained in the ul, so you need to set the font-size there.
In your jsfiddle the issue is resolved by setting
.wrapper{width:600px;}
ul,li{padding:0;margin:0;font-size:0}
I'm creating a pure-CSS hover dropdown menu, based on a very basic idea idea.
The HTML:
<ul id="top">
<li>
Menu item 1</li>
<li>
This one has submenu
<div class="submenu">
<ul>...</ul>
<div>
</li>
</ul>
The CSS:
div.submenu {
display: none;
position: absolute;
}
ul#top > li:hover div.submenu { display:block; }
As far as I know, this is the bare minimum to get the idea working.
The problem is I want the submenu to be multi-column, without actually using CSS3 multiple columns.
So I decided to break my submenu into multiple lists and have them float: left like this:
<ul id="top">
<li>
Menu item 1</li>
<li>
This one has submenu
<div class="submenu">
<ul>...</ul>
<ul>...</ul>
<ul>...</ul>
<div>
</li>
</ul>
...and the CSS:
div.submenu ul { float:left; }
This worked well until the point when I got a pretty big submenu in the last main menu item, producing a result like this:
The problem is it is unacceptable to have the submenu fall outside the container. I decided to mark the second half of the main menu items as class="right", and align the submenu's right border to the parent item's right border.
li.right div.submenu { right: 0; }
/* this placed the submenu to the right of the entire page;
it needs a positioning context: */
ul#top li { position:relative; }
That last line causes the <ul>'s to stop floating and just get stacked on top of each other.
Is there a way to keep them floating without setting the <div class="submenu"> to a fixed width?
Interactive demo: http://codepen.io/oli-g-sk/pen/ociet
Edit: if this helps somehow, it is allowed to set the submenu list items .submenu > ul > li to a fixed width. In fact I'm already doing it in the demo.
Try removing float:left from div.submenu ul and add these two rules:
div.submenu {
white-space: nowrap;
}
div.submenu ul {
/* float:left; <-- remove this */
display: inline-block;
}
demo: http://codepen.io/anon/pen/ApxFd
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Have an image floated to the left and two unordered lists floated to the right. For some reason the lists float side by side and not on top of each other. I can't get list-2 to float underneath float 1.
Would anybody have any ideas?
<div class="container">
<img src="yogapic1.png"/>
<ul class="list-1">
<li>Home</li>
<li>Influences</li>
<li>About Me</li>
<li>Classes</li>
<li>Andrews Video Blog</li>
<li>Photography</li>
</ul>
<ul class-"list-2">
<li>Find Us</li>
<li>Contact</li>
<li>Facebook</li>
</ul>
</div><!--container-->
.container {
max-width: 1075px;
margin: auto;
}
.container img {
float: left;
}
.list-1 {
float: right;
}
.list-2 {
float: right;
}
.list-2 {
float: right;
clear: right;
}
float: right will, if there's enough room, put the element to the left of any other elements which have been floated to the right. To override that behaviour and guarantee that it will fall below the most recent float:right element you need to use clear:right.
You've got a typo on list 2 (- instead of =). If you also add clear:right; to the lists it should do what you want:
Example: http://jsfiddle.net/nkYun/
Wrap the two lists in a single div element and float that to the right instead.
By floating the two lists individually, you are taking them out of the normal flow of the document, so they no longer force elements to appear below them.
To float two lists on top of each other you simple need 3 steps:
The container should have position as relative position:relative
One of the 2 lists should be float for example to the right float:right
The second list should have position absolute position:absolute and the right of it is 0px so that it start from the right and be on the first list
Here is an alternative approach. Sometimes trying to figure out what should be floated left, or what should be floated right can be confusing in code. But if you have a general idea of what elements you want to be floated, (and perhaps later on you will decide to add more to that section), then contain the items within a parent container (in our case a div element with a css class called lists) and float the entire parent where you want to be. Here's an example...
The CSS:
.container {
max-width: 1075px;
margin: auto;
}
.container img {
float: left;
}
.lists {
float: right;
border:solid 1px pink;
}
The HTML:
<div class="container">
<img src="yogapic1.png"/>
<div class="lists">
<ul class="list-1">
<li>Home</li>
<li>Influences</li>
<li>About Me</li>
<li>Classes</li>
<li>Andrews Video Blog</li>
<li>Photography</li>
</ul>
<ul class-"list-2">
<li>Find Us</li>
<li>Contact</li>
<li>Facebook</li>
</ul>
</div>
</div><!--container-->
And my fiddle
Notice too with this approach, you are no longer leaving the headache of ordering your lists with floats, but now, within this parent container, you can simply go into your HTML code and sore the lists, in any order you want them to be.
Enjoy!
Try to assign width to your list:
.list-1 {
float: right;
width:100%;
}
.list-2 {
float: right;
width:100%;
}
Edit
If you are giving same CSS to both lists, try something like this
.container ul{
float:left;
width:100%;
}
If you float list-2 to the right and clear: right too that should work. Like:
list-2 {
float: right;
clear: right;
}
I have a div called NAV and inside of NAV I have an UL with 5 li which I float to the left, the li's that is but when I do that the NAV collapses. I know this because I put a border around NAV to see if it collapses and it does. Here is the example.
collapsed http://img401.imageshack.us/img401/8867/collapsedze4.png
no collapsed http://img71.imageshack.us/img71/879/nocollapsedkx7.png
as you can see in the first image, the links in the NAV div are floated left and that
black border ontop is the actual div called NAV.
in this image you can see how it has top and bottom border and it not collapsed.
here is some of the html and css I used.
alt text http://img301.imageshack.us/img301/5514/codejc8.png
#nav #ulListNavi a {
float: left;
}
Add any overflow value other than visible to your container:
div#nav { overflow:auto; }
Then add width to restore the width
div#nav { width: 100%; overflow:auto; }
One solution is to add a "clear:both" style to an element after the last floated anchor, for instance:
<div id="nav">
<ul id="ulListNavi">
<li>Home</li>
<li>Search</li>
<li>Flowers</li>
<li>My Account</li>
<li>Contact Us</li>
</ul>
<div style="clear:both;"></div>
</div>
This causes the containing element to clear all floating elements before closing the containing box.
A few other options for clearing floats here:
http://www.quirksmode.org/css/clearing.html
http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
As to the best way of doing it, that's almost a holy war, the purists would freak about the extra div, if you are not fussed by a little extra markup, the addition of the cleared div as suggested by Joshua and AJ will work fine, and is a reliable technique, but there are at least 17 other ways of doing it...
add this code after your ul:
<div style="clear: both"></div>
Try floating the containing element to the left too.
Don't bother with clearing elements or overflow. Add this:
#nav {
float: left;
}
When you float the LI's, the #nav no longer "contains" anything so it collapses. But if the #nav is floated also, it contains anything floated inside it, so it expands again.
(Also consider removing the #nav div and just applying the same styles to the UL.)
Your problem is because you are floating the <A> elements, but each of them is inside an <LI> element. LIs display as blocks by default, so each <LI> is forcing it's child <A> to begin on a new line.
If you float the <LI>s, I think you'll solve your problem.
#nav #ulListNavi li {
float: left;
}
The quickest solution would be to add overflow:hidden to clear the float on the parent element:
#nav{overflow:hidden;}
Without changing your HTML:
#nav
{
width: 100%;
overflow: auto;
border: solid 1px red;
}
#ulListNavi
{
margin: 0;
padding: 0;
list-style: none;
}
#nav #ulListNavi li
{
float: left;
}
#nav #ulListNavi li a
{
margin-left: 5px;
}
Works in IE8 and FF 3.5