How do I *really* justify a horizontal menu in HTML+CSS?
Modern Approach - Flexboxes!
Now that CSS3 flexboxes have better browser support, some of us can finally start using them. Just add additional vendor prefixes for more browser coverage.
In this instance, you would just set the parent element's display
to flex
and then change the justify-content
property to either space-between
or space-around
in order to add space between or around the children flexbox items.
Using justify-content: space-between
- (example here):
ul { list-style: none; padding: 0; margin: 0;}.menu { display: flex; justify-content: space-between;}
<ul class="menu"> <li>Item One</li> <li>Item Two</li> <li>Item Three Longer</li> <li>Item Four</li></ul>
Horizontal menu justified positioning of items
The text-align:justify
positioning trick requires to have white space (blank space) between the child items in the markup.
In the following example, it works, because there are white spaces between the <li>
tags.
ul { text-align: justify; padding: 0;}li { display: inline-block;}ul:after { content: ''; display: inline-block; width: 100%;}
<ul> <li>1</li> <li>2</li> <li>3</li></ul>
Justify Menu Items in Dynamic Horizontal Menu with overflow
Getting the elements to justify with wrapping to next line is tricky. Using display:table
and table-cell
can justify elements like tables but only in one row. Because your requirement is to also keep elements justified while wrapping within a fixed width container, the table-cell
won't work.
There is a hack based on :after
pseudo-element which can make this possible with wrapping across rows.
Demo: http://jsfiddle.net/abhitalks/MXZ6w/3/
Apply a fixed width to the wrapping div
, text-align:justify
on the ul
and display:inline-block
on li
are required.
#upper-menu-wrapper {
width: 500px; /* fixed width on wrapping container */
}
#upper-menu { } /* this div is not really needed */
#upper-menu > ul {
list-style-type: none; /* getting rid of bullets */
margin: 0px; padding: 0px; /* getting rid of default indents */
text-align: justify; /* important to justify contents */
}
#upper-menu > ul > li {
display: inline-block; /* required. float won't work. */
text-align: left; /* to properly align list items */
white-space: no-wrap; /* to prevent wrapping of list items if required */
}
#upper-menu > ul:after {
/* this is the hack without which the list items won't get justified */
content:''; display: inline-block; width: 100%; height: 0;
}
Note 1: The display: inline-block
is required, however it generates html white-spaces. In order to get rid of those white-spaces, html comments can be used in the markup of list items.
Note 2: The :after
pseudo element in the hack is what seems to do the trick. However, that will create an unintended space below the ul
. This space seems to be there because the elements are flushed across. If not justified, then this space does not appear.
IMPORTANT: Credit: @SamGoody from his answer here.
How do I justify a horizontal list?
Modern Approach - CSS3 Flexboxes
Now that we have CSS3 flexboxes, you no longer need to resort to tricks and workarounds in order to make this work. Fortunately, browser support has come a long way, and most of us can start using flexboxes.
Just set the parent element's display
to flex
and then change the justify-content
property to either space-between
or space-around
in order to add space between/around the children flexbox items. Then add additional vendor prefixes for more browser support.
Using justify-content: space-between
- (example here):
ul {
list-style: none;
padding: 0;
margin: 0;
}
.menu {
display: flex;
justify-content: space-between;
}
<ul class="menu">
<li>About</li>
<li>Contact</li>
<li>Contact Longer Link</li>
<li>Contact</li>
</ul>
Horizontal alignment of CSS sub-submenu?
You can simply try adding this:
#nav ul li ul li ul {
left: 0;
}
Demo - http://jsfiddle.net/d8hrcxmr/2/
If you ever need all the sub menus appear to the very left check out this demo - http://jsfiddle.net/d8hrcxmr/1/
#nav ul li ul {
left: 0;
}
I think this is better for solving the problem of sub menus get cut off on the right (Imagine you have long sub menus on Menu6).
Center a horizontal CSS menu
You can center the navigation bar by using the following CSS rules:
nav {
margin: 0 auto;
text-align: center;
border:1px solid black;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
margin: 0; /* << add this */
padding: 0; /* << add this */
display: inline-block; /* << add this */
vertical-align: top; /* << add this */
}
nav ul li {
float: left;
margin: 0; /* << add this */
padding: 0; /* << add this */
}
nav ul li:hover a {
color: #000000;
}
nav ul li a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
background-color: pink; /* optional... */
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
position: relative;
}
nav ul ul li a {
color: #000000;
}
nav ul ul li a:hover {
color: #666666;
}
nav ul ul ul {
position: absolute;
top:0;
}
See demo at: http://jsfiddle.net/audetwebdesign/DP6Ax/
The key is to set display: inline-block
for nav ul
, which will allow your text-align: center
rule to take effect.
Make sure to zero out margins and paddings on the ul
and li
elements. Everything else that you did was more or less right, so you should be good.
Horizontal Centered Menu in CSS?
With the provided HTML:
ul { text-align: center; }
li { display: inline-block; } /* Don't float them */
http://jsfiddle.net/NpLR3/
Related Topics
Why Media Queries Has Less Priority Than No Media Queries Css
Javaserver Faces 2.2 and Html5 Support, Why Is Xhtml Still Being Used
Css ''Background-Color" Attribute Not Working on Checkbox Inside ≪Div≫
Change Cursor Type on Input Type="File"
Using CSS Td Width Absolute, Position
Prevent Scroll-Bar from Adding-Up to the Width of Page on Chrome
Removing Ul Indentation with CSS
Elongated Hexagon Shaped Button Using Only One Element
Css Set Background-Image by Data-Image Attr
Animate/Shrink Navbar on Scroll Using Bootstrap
Href Overrides Ng-Click in Angular.Js
CSS Background Image on Top of <Img>
Required Attribute Not Work in Safari Browser
How to Make a Whole 'Div' Clickable in HTML and CSS Without JavaScript