Prevent floating UL and LI elements underneath a header - html

Below is a menu inside a header. The ul and li elements are floating and are now floating underneath the header, which I've tried to prevent with clear:both. However, that doesn't seem to work so I wonder... what can be wrong?
html:
<header>
<ul>
<li><a href='#'>Item 1</a></li>
<li><a href='#'>Item 2</a></li>
<li><a href='#'>Item 3</a></li>
<li><a href='#'>Item 4</a></li>
</ul>
<div class='clear'/>
</header>
css:
header {
background: #888;
height: 20px;
padding: 10px;
}
ul{
margin: 18px 0;
padding: 0;
list-style: none;
}
ul li{
margin: 0 10px 0 0;
padding: 0;
display: block;
float: left;
width: 80px;
height: 20px;
border: 1px solid #000;
background: red;
}
ul li a{
display:block;
width: 100%;
height: 100%;
text-decoration: none;
float: left;
text-align: center;
padding-top: 2px;
}
ul li a:hover{
color: #fff;
background-color: #000;
}​
.clear {
clear:both;
}

You can use a clearfix:
.clearfix:after {
clear:both;
content:".";
display:block;
height:0;
line-height:0;
visibility:hidden;
}
and apply that to both the ul and the header.

Better css for your situation would be (Tested in FF, Chrome, IE6+)
header {
display: block; /* Necessary for older browsers to render this html5 element properly */
background: #888;
height: 20px;
padding: 10px;
}
ul {
margin: 18px 0;
padding: 0;
list-style: none;
}
ul li {
margin: 0 10px 0 0; /* at least some margin-right is necessary to make inline-block work in IE */
padding: 0 5px; /* you don't have to set a width any longer, so let padding define spacing */
display: inline-block;
zoom: 1; /* part 1 of the IE hack to make inline-block work */
*display: inline; /* part 2 of the IE hack to make inline-block work */
/* height and line-height not necessary - driven by a element below */
border: 1px solid #000;
background: red;
}
ul li a {
display:block;
height: 20px;
line-height: 20px; /* this will vertically center the text in the li */
text-decoration: none;
text-align: center;
}
ul li a:hover{
color: #fff;
background-color: #000;
}​

Rather than use a cleardiv, perhaps you could try the standard clearfix from html5boilerplate?
Here is the modified markup:
<header class="clearfix">
<ul>
<li><a href='#'>Item 1</a></li>
<li><a href='#'>Item 2</a></li>
<li><a href='#'>Item 3</a></li>
<li><a href='#'>Item 4</a></li>
</ul>
</header>
and here is the css to add:
.clearfix:before,
.clearfix:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
From the docs:
.clearfix
Adding .clearfix to an element will ensure that it always fully contains its floated children. There have been many variants of the clearfix hack over the years, and there are other hacks that can also help you to contain floated children, but the HTML5 Boilerplate currently uses the micro clearfix.

You can't use /> for divs. Change it from <div class='clear'/> to <div class="clear"></div>
Or try adding another li below the current ones and giving that the clear class.
<header>
<ul>
<li><a href='#'>Item 1</a></li>
<li><a href='#'>Item 2</a></li>
<li><a href='#'>Item 3</a></li>
<li><a href='#'>Item 4</a></li>
<li class='clear'></li>
</ul>
</header>

Related

CSS - on hover affect full width of element, without removing padding

I have a series of nested ul and li elements, to create a sidebar nav system. When the user hovers over one of the li elements I want the entire width of the background to change (shown right), not just the inside of the container (shown left).
The problem I'm having is that in order to make the entire width change, I need to remove the ul padding-left which then removes the indentation and 'nested' look of the list elements.
An example below shows the behaviour currently present, where the background-color does not change for the entire width. Preferably I wouldn't have to manually add in indentation for each element, as the nested padding works quite nicely as is, but am open to suggestions :D
* {
box-sizing: border-box;
}
nav {
width: 100px;
background-color: lightgrey;
}
li {
list-style: none;
}
ul {
padding-left: 10px;
}
a {
color: black;
text-decoration: none;
padding: 5px;
display: inline-block;
width: 100%;
}
a:hover {
background-color: grey;
color: white;
}
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Work</a></li>
<ul>
<li><a href='#'>Piece 1</a></li>
<li><a href='#'>Piece 2</a></li>
</ul>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
My questions:
How can I make the background of the entire width of the li update on hover, without removing the indentation for the nested list items.
Why are the width: 100% li elements not overflowing outside the nav bar? Since I thought width: 100% meant their width was the same as their parent's widths, which surely as they are indented would mean they would extend beyond.
Thanks!
Post Answer Edit
The accepted answer is definitely good, but the solution i ended up with was dynamically generating the navbar elements with JavaScript according to an object.
This solution works for any number of nested levels.
The trick is using CSS counter to pad levels.
* {
box-sizing: border-box;
}
nav {
display: inline-block;
background-color: lightgrey;
}
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
li {
counter-increment: section;
counter-reset: section;
}
a:before {
content: counters(section, '');
opacity: 0
}
a {
color: black;
text-decoration: none;
padding: 5px;
display: block;
}
a:hover {
background-color: grey;
color: white;
}
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Work</a>
<ul>
<li><a href='#'>Piece 1</a></li>
<li><a href='#'>Piece 2</a>
<ul>
<li><a href='#'>Piece 2.1</a></li>
<li><a href='#'>Piece 2.2</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
If you want the links to be the width of the list, you can remove the padding of the unordered list (ul). Put the padding on the links instead:
* {
box-sizing: border-box;
}
nav {
width: 100px;
background-color: lightgrey;
}
li {
list-style: none;
width: 100%;
}
ul {
padding-left: 0;
}
a {
color: black;
text-decoration: none;
box-sizing: border-box;
padding: 5px;
width: 100%;
padding-left: 15px;
display: inline-block;
width: 100%;
}
ul ul li a{
padding-left: 25px;
}
a:hover {
background-color: grey;
color: white;
}
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Work</a></li>
<ul>
<li><a href='#'>Piece 1</a></li>
<li><a href='#'>Piece 2</a></li>
</ul>
<li><a href='#'>Contact</a></li>
</ul>
</nav>

How to move this menu bar to the right

I want move objects to the right but don't know how!? Any idea?
. Please help me. Here is the code in HTML and CSS
/*=====================================
= Top Bar
=====================================*/
#top-bar {
height: 42px;
line-height: 40px;
background: #f9f9f9;
color: #888;
font-size: .857em;
border-bottom: 1px solid #f5f5f5;
}
.top-nav ul li {
display: block;
float: left;
}
.top-nav ul {
margin: 0;
padding: 0;
}
.top-nav {
font-family: "Montserrat",Open Sans,Sans-serif;
}
.top-nav ul li a {
color: #111;
text-decoration: none;
display: inline-block;
padding: 0 15px;
line-height: .917em;
color: #888;
text-decoration: none;
text-transform: uppercase;
letter-spacing: .5px;
font-size: .917em;
border-right: 1px solid #eee;
}
.top-notification {
float: right;
}
.top-notification p {
margin: 0;
float: left;
font-size: 13px;
}
.top-notification a {
color: #111;
text-decoration: none;
/* display: inline-block; */
padding: 5px 9px;
line-height: .917em;
color: #888;
text-decoration: none;
text-transform: uppercase;
letter-spacing: .5px;
font-size: .917em;
border: 1px solid #D1D1D1;
margin-left: 22px;
background:#fff;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none!important;
color: #fff;
width:100%;
}
nav ul li a {
color: #fff!important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width:170px;
float:none;
display:list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li > a:after { content: ' +'; }
nav li > a:only-child:after { content: ''; }
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
}
.menu.cloned {
width: 100%!important;
left: 0!important;
}
.menu.cloned nav {
width: 1200px;
margin: auto;
}
Here is HTML codes for my website. I think every thing in is fine here and the problem is in CSS codes.
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
This is menu bar
You should add some margin-left to your css. As you have not provided your full HTML I cannot say which attribute exactly needs the stylling.
I think you should add margin-left to your menus to increase space between them or to the parent div for shifting the whole navbar to the right. Hope this helps!
I'm going to assume that when you ask how to move them to the right that the ultimate goal is to center the navigation elements inside the blue-green field that is <nav>.
You might also be asking to align the navigation items to the right side of the blue-green field, I've addressed that in my second code snippet.
Center Nav Items
A common way to center something is to move it to the left by 50% and then pull it back 50% of it's width with transform. In order for this to work you'll need to make your <ul> not take up 100% of its parent element. You can do this a number of ways, for simplicity I have set it to display: inline-block;.
This solution will allow you to add/remove <li> and still be centered in <nav>.
nav ul {
display: inline-block;
position: relative;
left: 50%;
padding: 0;
margin: 0;
transform: translateX( -50%);
list-style: none;
}
nav ul li {
display: inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none !important;
color: #fff;
}
nav ul li a {
color: #fff !important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover>ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width: 170px;
float: none;
display: list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li>a:after {
content: ' +';
}
nav li>a:only-child:after {
content: '';
}
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
}
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
I noticed you included a little more CSS than what was being used by your markup so I removed it.
Align Nav Items to Right
If you are looking to align the navigation to the right side instead of the left side you can float the <ul> to the right. The only catch here is that you will need to clear the float so you can see the background color of .menu. I used the poor man's clearfix, overflow: hidden; but you could use the more modern option of the Micro Clearfix.
nav ul {
float: right;
display: inline;
position: relative;
padding: 0;
margin: 0;
list-style: none;
}
nav ul li {
display: inline-block;
}
nav ul li a:link {
display: inline-block;
padding: 15px 25px;
text-transform: uppercase;
letter-spacing: 1px;
position: relative;
text-decoration: none !important;
color: #fff;
}
nav ul li a {
color: #fff !important;
}
nav a:hover {
background-color: #2da399;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
z-index: 1000;
background: #494949;
}
/* Display Dropdowns on Hover */
nav ul li:hover>ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
min-width: 170px;
float: none;
display: list-item;
position: relative;
}
nav ul ul a:hover {
background-color: #656565;
}
/* Change this in order to change the Dropdown symbol */
nav li>a:after {
content: ' +';
}
nav li>a:only-child:after {
content: '';
}
.menu {
width: 1200px;
margin: auto;
z-index: 99999;
background: #2da399;
overflow: hidden; /* Clearfix that allows the background of <nav> to be seen with floated child elements. */
}
<!--Main Navigation-->
<div class='menu-wrapper'>
<div class='menu'>
<nav>
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
<li><a href='#'>Nyheter </a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
<li><a href='#'>Världen</a></li>
</ul>
</li>
<li><a href='#'>Vetenskap
</a>
<!-- First Tier Drop Down -->
<ul>
<li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
<li><a href='#'>Fysik </a></li>
<li><a href='#'>Matte</a>
<!-- Second Tier Drop Down -->
<ul>
<li><a href=''>Matte 1</a></li>
<li><a href='#'>Matte 2</a></li>
<li><a href='#'>Matte 3</a>
<!-- Third Tier Drop Down -->
<ul>
<li><a href='#'>Matte 4</a></li>
<li><a href='#'>Hjälpmedel
</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>فناوری </a></li>
<li><a href='#'>اجتمائی </a></li>
<li><a href='#'> سیاست</a></li>
<li><a href='#'>تاریخ </a></li>
<li><a href=''>کلاس ها</a></li>
<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>
</ul>
</nav>
</div>
</div>
Just set your nav ul li width to 100/number of items. and it will spread your menu items equally across the bar, removing the gap on the right as much as possible.
You can remove the space between inline block elements as you like by setting margin-right property accordingly.
Replace old nav ul li with this code:
nav ul li {
display:inline-block;
background-color: #2da399;
width:11%;
margin-right: -4px;
}
Doing the menu this way will allow your menu to wrap into various screen widths fully.

Menu with links floated at left and right but aligned at center in terms of height

Im trying to have a menu where I have some links floated at left and then I have a div with some links that I want floated at right.
I'm trying to do this with the code below and it is working, but the left links and right links are no aligned at the center of the menu in terms of height, do you know why?
And also the :hover effect is not occupying the entire height of the menu.
CODE:
.container {
width: 100%;
background: yellow;
float: left;
}
.content {
height: 50px;
}
.menu-list {
list-style: none;
}
.menu-list li {
float: left;
}
.menu-links {
float: right;
}
.menu-list li a {
color: #aaa;
text-decoration: none;
line-height: 50px;
font-weight: bold;
}
.menu-list li a:hover {
background: red;
}
<div class="container">
<div class="content">
<ul class="menu-list">
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
<li><a title="" href="">Home</a>
</li>
</ul>
<div class="menu-links">
Link 1
Link 2
</div>
</div>
</div>
JsFiddle
Check this fiddle here
It happens because, you have used menu-list with ul and menu-links with div.
In basic HTML, ul has predefined padding and margin. So, first of all clear out that padding and margin then style menu-list and menu-links accordingly.
Instead of adding extra space or element use following html,
<div class="container">
<div class="content">
<ul class="menu-list">
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
</ul>
<ul class="menu-links">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</div>
</div>
And your respective CSS will be,
body {
padding: 0;
margin: 0
}
.container {
display: block;
width: 100%;
background: yellow;
clear: both;
padding: 10px;
}
.container:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
a {
text-decoration: none
}
.menu-list,
.menu-links {
list-style: none;
margin: 0;
padding: 0
}
.menu-list {
float: left
}
.menu-links {
float: right
}
.menu-list li,
.menu-links li {
display: inline-block
}
Try this one
.container{
width:100%;
background:yellow;
float:left;
}
.content{
height: 50px;
}
.menu-list{
list-style:none;
margin: 0;
float: left;
}
.menu-list li{
display: inline-block;
}
.menu-links{
float: right;
}
.menu-links a{
line-height: 50px;
}
.menu-list li a{
color:#aaa;
text-decoration: none;
line-height: 50px;
font-weight: bold;
}
.menu-list li a:hover{
background:red;
padding: 16px 0px;
}
<div class="container">
<div class="content">
<ul class="menu-list">
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
<li><a title="" href="">Home</a></li>
</ul>
<div class="menu-links">
Link 1
<a href="" >Link 2</a>
</div>
</div>
</div>
How about this fiddle?
It is generally better to float the ul and give the li display: inline;
ul also has a default margin and padding on it so you need to use a reset of some sort
ul.menu-list{
margin: 0 0 0 15px;
padding: 0;
}
*Revised to make CSS only
Would something like this work? You just need to know the height of your nav bar, and add a pseudo-element of that height to each of the separate nav sections. https://jsfiddle.net/will0220/wg7hwc7s/
Just set them to display: inline-block and vertical-align: middle to keep them centered in the height of your nav bar by comparing it to the pseudo element. This should work with any number of lines of text in each button.
.container{
width:100%;
background:yellow;
float:left;
}
.menu-list{
list-style:none;
}
.menu-list{
float: left;
padding: 0;
margin: 0;
}
.menu-links{
float: right;
}
.menu-list li a{
color:#aaa;
text-decoration: none;
line-height: 50px;
font-weight: bold;
}
.menu-list:before, .menu-links:before{
content: '';
display: inline-block;
vertical-align: middle;
height: 50px;
width: 0;
}
.menu-list li, .menu-links a{
display: inline-block;
vertical-align: middle;
}
.menu-list li a:hover{
background:red;
}
You can use display:table-cell property to easily do that and also you don't have to use float for li to display as horizontal menu instead use display:inline
updated fiddle here

Horizontal menu: how to float right but keep the menu items in the correct order?

I'm using float: right for my horizontal menu (.drop_menu li) as I want the menu to be aligned to the right side of the screen (and logo to the left side). It works OK, the only issue is that my menu items are now in the wrong order (Link 3 then Link 3 then Link 1 instead of the opposite). Is there a way to fix that?
Many thanks
http://jsfiddle.net/eLSbq/
<div class="header">
<div class="logo">Logo</div>
<ul class="drop_menu">
<li><a href='#'>Link 1</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
</ul>
</li>
<li><a href='#'>Link 2</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
<li><a href='#'>Link 3</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
</ul>
</div>
.header {
width: 100%;
background: #fff;
color: #124191;
font-weight: 300;
font-size: 28px;
height: 120px;
display: table;
position: fixed;
z-index: 999999;
opacity: 0.7;
background: aqua;
}
.logo {
display: inline-block;
vertical-align: middle;
left:0;
color: #333;
font-size: 30px;
font-weight: 800;
letter-spacing: -1px;
margin-left: 60px;
background: red;
}
.drop_menu {
padding:0;
margin:0;
list-style-type:none;
right: 0;
display: table;
z-index: 3000;
display: table-cell;
vertical-align: middle;
right: 0;
}
.drop_menu li { display: table-cell;
vertical-align: middle; float: right;}
.drop_menu li a {
padding:9px 20px;
display:block;
color:#666;
text-decoration:none;
font-size: 15px;
font-weight: 400;
text-transform: uppercase;
}
/* Submenu */
.drop_menu ul {
position:absolute;
left:-9999px;
top:-9999px;
list-style-type:none;
}
.drop_menu li:hover { position:relative; background:#5FD367; }
.drop_menu li:hover ul {
left:0px;
top:30px;
background:#5FD367;
padding:0px;
}
.drop_menu li:hover ul li a {
padding:5px;
display:block;
width:168px;
text-indent:15px;
background-color:#5FD367;
}
.drop_menu li:hover ul li a:hover { background:#005555; }
Remove float:right from li which prevent the reverse order.
Add float:right to the ul's .dropdown class which put your entire menu at right side.
Add float:left to the li which helps your sub-menu to stay align.
.drop_menu {
float: right;
}
.drop_menu li {
display: table-cell;
vertical-align: middle;
float:left;
}
Js Fiddle Demo
flex solution:-
This is what worked for me:
ul {
display: flex;
justify-content: flex-end;
}
ul li {
outline: 2px violet solid;
display: list-item;
list-style: none;
padding: 10px;
margin: 0 5px;
}
ul li:hover {
outline: 2px deeppink solid;
cursor: pointer;
}
<div>
<h1>Hello World....</h1>
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
</div>
Here is the fiddle: https://jsfiddle.net/appsparkler/emjst7f3/4/
Good Luck...
Add one more div around menu items and set float to right
<div style='float:right'>
<!-- put menu controls here -->
</div>
Remove float right from following class
.drop_menu li { display: table-cell;
vertical-align: middle;}
for demo click on jsfiddle link
Try this...
.drop_menu li { display: table-cell;
vertical-align: middle; float: left;
}
It results in LINK 1 LINK 2 LINK 3 order and reduce the width of submenu links.
It works.
Float the <ul> to the right, then float its <li> elements to the left.

CSS - How to display dropdown text in one row?

I'm building a dropdown menu and I've got an issue showed on this img:
I want the "2.1" text to be displayed next to the "Item" text. For some reaason, every new word gets a new line.
Here's the html:
<ul id='nav'>
<li><a href='/'>Item 1</a></li>
<li><a href='/'>Item 2</a>
<ul>
<li><a href='/'>Item 2.1</a></li>
</ul>
</li>
<li><a href='/'>Item 3</a></li>
<li><a href='/'>Item 4</a></li>
<li><a href='/'>Item 5</a></li>
</ul>
And here's the CSS:
#nav {
list-style: none;
}
#nav li {
float: left;
position: relative;
}
#nav li a {
display: block;
text-decoration: none;
text-align: center;
background: #ccc;
margin-right: 5px;
}
#nav li ul {
position: absolute;
}
#nav li ul li {
display: block;
}
#nav li ul li a {
padding: 0px 10px;
height: 20px;
text-align: left;
background: #999;
}
Thanks for any help, Mike.
An easy way to fix this is adding a nowrap property to #nav li ul li a:
#nav li ul li a {
padding: 0px 10px;
height: 20px;
text-align: left;
background: #999;
white-space: nowrap; /* Forbids text wrapping */
}