How to center CSS Navigation Menu - css

Please can someone help?
How can I centre this CSS menu?
See below for the beginning of the code
Whatever I try doesn't seem to work.
I'm sure it is quite simple really but I cannot figure it out.
Thanks in advance!
#cssmenu {
position: relative;
height: 44px;
background: #ffffff;
width: auto;
float: left;
}
#cssmenu ul {
list-style: none;
padding: 0;
margin: 0;
line-height: 1;
position:relative;
display: inline-block;
vertical-align: top;
}
#cssmenu > ul {
position: relative;
display: block;
background: #ffffff;
height: 32px;
width: 100%;
z-index: 500;
}
#cssmenu > ul > li {
display: block;
position: relative;
float: left;
margin: 0;
padding: 0;
position:relative;
}
#cssmenu > ul > #menu-button {
display: none;
}
#cssmenu ul li a {
display: block;
font-family: 'Noto Sans';
text-decoration: none;
}
#cssmenu > ul > li > a {
font-size: 14px;
font-weight: bold;
padding: 15px 20px;
color: #000000;
text-transform: uppercase;
-webkit-transition: color 0.25s ease-out;
-moz-transition: color 0.25s ease-out;
-ms-transition: color 0.25s ease-out;
-o-transition: color 0.25s ease-out;
transition: color 0.25s ease-out;
}
#cssmenu > ul > li.has-sub > a {
padding-right: 32px;
}
#cssmenu > ul > li:hover > a {
color: #000000;
}
#cssmenu li.has-sub::after {
display: block;
content: '';
position: absolute;
width: 0;
height: 0;
}
#cssmenu > ul > li.has-sub::after {
right: 10px;
top: 20px;
border: 5px solid transparent;
border-top-color: #000000;
}
#cssmenu > ul > li:hover::after {
border-top-color: #000000;
}
HTML
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'><span>Home</span></a></li>
<li class='has-sub'><a href='#'><span>Products</span></a>
<ul>
<li class='has-sub'><a href='#'><span>Product 1</span></a>
<ul>
<li><a href='#'><span>Sub Item</span></a></li>
<li class='last'><a href='#'><span>Sub Item</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Product 2</span></a>
<ul>
<li><a href='#'><span>Sub Item</span></a></li>
<li class='last'><a href='#'><span>Sub Item</span></a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'><span>About</span></a></li>
<li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>

One way of doing it is by setting the display type of #cssmenu to inline-block and then wrap everything in a container with text-align: center. You also need to set text-align: left on #cssmenu such that the text aligns correctly again.
JSFiddle

Ok, I'm going to assume that in your HTML, the #cssmenu element is the one you are trying to center.
As others have said, block elements can be centered by using a width: and a margin: 0 auto.
The dilema here is that if you set a width to it, then you have to adjust the width everytime the menu items change, so what I do is leave it without a width attribute and only enhance it (center it) for users with JS support.
If using jQuery, you could do something like this:
function navCenter(){
if($('#cssmenu').length){
var ulWidth = 0;
$('#cssmenu > ul > li').each(function(){
ulWidth += $(this).outerWidth(true);
});
$('#cssmenu').css('width', ulWidth);
}
}
So the above piece of code sets a width on the #cssmenu element and the css applies a margin: 0 auto, centering it.
Let me know if this make sense.

This is how you can center the ul with all its list-items in a container without js or jquery. There's no need to adjust something if the list-items are changed.
<div class="menuwrapper">
<ul class="nav">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
.menuwrapper {
width: 100%;
text-align: center;
}
.nav, .nav li, .nav a {
margin: 0;
padding: 0;
border: 0;
}
.nav {
float: left;
position: relative;
left: 50%;
top: 0;
list-style-type: none;
text-align: center;
}
.nav li {
float: left;
width: auto;
right: 50%;
display: block;
position: relative;
list-style-type: none;
cursor: pointer;
}
.nav a {
display: block;
padding: 8px 10px;
text-decoration: none;
font: bold 16px arial,sans-serif;
line-height: 1;
outline: 0;
}

Related

How to stop drop down menu from dropping when hovering under the "parent link"?

In this example the drop down menu shows up when you hover under the "parent link". I only want the drop down menu to show when I hover on the "parent link" and not under it. Is there any way to stop this?
codepen : Codepen
code :
<ul class="menu">
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
</ul>
.menu {
display: block;
margin: 0 auto;
position: relative;
width: 200px;
}
.menu > li > a {
background: blue;
color: #fff;
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
}
.menu ul {
background: #ddd;
height: 0;
left: 0;
opacity: 0;
position: absolute;
transition: all .5s ease;
top: 35px;
width: 100%;
}
.menu li:hover ul {
height: 200px;
opacity: 1;
transform: translateY(0);
}
.menu ul a {
color: #000;
display: block;
padding: 5px 20px;
}
you have set hover to li in which the all the links come, You have to add hover to anchor tag and use + to make action on ul tag which is sibling of anchor tag.
.menu li a:hover + ul {
height: 200px;
opacity: 1;
transform: translateY(0);
}
The only way that I could think of is to instead of using opacity:1 use display:none; and when you want to display it change it to display:block/flex/grid or the other way would be with javascript, by adding an event listener to the parent link and when mouseenter toggle the class of the animation to active or the name of the class and when the mouseleave toggle or remove the class.
This is your current code -
.menu {
display: block;
margin: 0 auto;
position: relative;
width: 200px;
}
.menu > li > a {
background: blue;
color: #fff;
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
}
.menu ul {
background: #ddd;
height: 0;
left: 0;
opacity: 0;
position: absolute;
transition: all .5s ease;
top: 35px;
width: 100%;
}
.menu li:hover ul {
height: 200px;
opacity: 1;
transform: translateY(0);
}
.menu ul a {
color: #000;
display: block;
padding: 5px 20px;
}
<ul class="menu">
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
</ul>
Modified Code
Just change menu-ul to display:none and when hover change it to display:block
.menu {
display: block;
margin: 0 auto;
position: relative;
width: 200px;
}
.menu > li > a {
background: blue;
color: #fff;
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
}
.menu ul {
background-color: #f1f1f1;
display: none;
height: 0;
left: 0;
opacity: 0;
position: absolute;
transition: all .5s ease;
top: 35px;
width: 100%;
}
.menu:hover ul{display: block;}
.menu li:hover ul {
height: 200px;
opacity: 1;
transform: translateY(0);
}
.menu ul a {
color: #000;
display: block;
padding: 5px 20px;
}
<ul class="menu">
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
</ul>
Try this, add display: none; on <ul>, then display:block; on <ul> when the parent is hovered.
.menu ul {
background: #ddd;
height: 0;
left: 0;
opacity: 0;
position: absolute;
transition: all .5s ease;
top: 35px;
width: 100%;
display: none; /*dont display it yet*/
}
.menu > li:hover ul {
display: block; /*dispplay ul on hover*/
height: 200px;
opacity: 1;
transform: translateY(0);
}
.menu {
display: block;
margin: 0 auto;
position: relative;
width: 200px;
}
.menu > li > a {
background: blue;
color: #fff;
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
}
.menu ul {
background: #ddd;
height: 0;
left: 0;
opacity: 0;
position: absolute;
transition: all .5s ease;
top: 35px;
width: 100%;
display: none;
}
.menu > li:hover ul {
display: block;
height: 200px;
opacity: 1;
transform: translateY(0);
}
.menu ul a {
color: #000;
display: block;
padding: 5px 20px;
}
<ul class="menu">
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
</ul>

CSS dropdown menu to stay visible when hovering over list items?

Working on a website (inherited someone else's code) and I'm trying to make a dropdown menu. The dropdown appears correctly when I hover over the item, but as soon as I mouse down to the dropdown list items, they disappear. Can someone help me figure out what's wrong here?
#nav {
position:fixed;
top: -1em;
background: #000;
font-family: 'Abel', sans-serif;
font-size: 1.25em;
letter-spacing: 0.05em;
width:100%;
text-align:right;
padding: 1.25em;
z-index: 3;
}
#nav li {
list-style-type: none;
display: inline-block;
margin: 0em 0.25em;
color: #90918F;
}
#nav li a {
text-decoration: none;
display: inline-block;
padding: 0em 0.5em;
margin: 0;
color: #90918F;
}
#nav li a:hover {
text-decoration: none;
color: #90918F;
}
#nav li a.active {
text-decoration: none;
color: #90918F;
}
#nav li.space {
padding: 2em;
}
ul {
padding: 0.05em;
}
#nav ul {
z-index: 3;
margin-top: 0;
background: #000;
display: none;
position: absolute;
top: 72px;
right: 450px;
}
#nav ul li {
display: block;
padding: 0.75em;
text-align: left;
}
#nav li:hover > ul {
display: block;
}
#nav li > ul:hover {
position: absolute !important;
}
#nav ul li:after {
position: absolute;
left: 5%;
top: -20px;
width: 0;
height: 0;
content: '';
}
<ul id="nav">
<div id="logo">
<img src="images/logo.png" class="logo">
</div>
<li>ABOUT
<ul class="dropdown">
<li>Values</li>
<li>Our Team</li>
</ul>
</li>
<li>SERVICES</li>
<li>CLIENTS</li>
<li>STUDENTS</li>
<li>CONTACT</li>
</ul>

Submenu vanishes after mouse off main menu item

Could someone explain to me why navigating away from the main menu item causes the sub-menu to vanish? And possibly provide a fix. My google-foo skills are below par this morning. Thanks all.
CSS:
body, html{
margin: 0;
}
.content{
padding: 30px;
}
.nav-main{
width: 100%;
background-color: #222;
height: 70px;
color: #fff;
}
.nav-main > ul{
margin: 0;
padding: 0;
float: left;
list-style-top: none;
}
.nav-main > ul > li {
float: left;
}
.nav-item {
display: inline-block;
padding: 15px 20px;
height: 40px;
line-height: 40px;
color: #fff;
text-decoration: none;
}
.nav-item:hover{
background-color: #444;
}
.nav-content{
position: absolute;
top: 70px;
overflow: hidden;
background-color: #222; /* same color as main nav*/
max-height: 0; /*will not display if .nav-content no padding */
}
.nav-content a{
color: #fff;
text-decoration: none;
}
.nav-content a:hover{
text-decoration: underline;
}
.nav-sub{
padding: 20px;
}
.nav-sub ul{
padding: 0;
margin: 0;
list-style-type: none;
}
.nav-sub > ul > li{
display: inline-block;
}
.nav-sub ul li a{
padding: 5px 0;
}
.nav-item:focus{
background-color: #444; /*remove if click focus not necessary*/
}
.nav-item:hover ~ .nav-content{
max-height: 400px;
-webkit-transition:max-height 0.6s ease-in;
-moz-transition:max-height 0.6s ease-in;
transition:max-height 0.6s ease-in;
}
HTML:
<body>
<nav class="nav-main">
<ul>
<li>
first
<div class="nav-content">
<div class="nav-sub">
<ul>
<li>this is a thing</li>
<li>this is a thing 2</li>
</ul>
</div>
</div>
</li>
<li>
second
</li>
<li>
third
</li>
</ul>
</nav>
<div class="content">this is content</div>
</body>
I have changed:
.nav-item:hover ~ .nav-content{
To
nav > ul > li:hover > .nav-content{
And now it's working - example:
https://jsfiddle.net/saxkayv2/

Centering Submenu

I've read all the questions concerning centering submenus. But I don't get my problem solved.
I have a simple navigation bar with 2 submenus.
You can find it here: Fiddle.
ul#nav, ul#sub1, ul#sub2 {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul#sub1 a, ul#sub2 a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul#sub1, ul#sub2 {
display: none;
position: absolute;
left: 0px;
}
ul#nav li:hover ul#sub1 {
display: block;
}
ul#sub1 li:hover ul#sub2 {
display: block;
}
<nav>
<ul id="nav">
<li>Reisen
<ul id="sub1">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul id="sub2">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul id="sub1">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
</nav>
I want the submenu centered. When I hover over "Reisen" the submenu gets the same width like the main menu.
When I hover over "Karriere", I want the submenu centered under "Karriere" and not positioned left under "Reisen".
I was thinking of a span-element to the button "Karriere" but I couldn't solve it.
Thanks for your help.
I don't really now if this is what you're looking for or not, but maybe something like this?
Note: I made a few changes to your CSS and HTML, mainly changing everything to use classes instead of IDs
JS Fiddle Example
HTML
<nav>
<ul id="nav">
<li>Reisen
<ul class="sub">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul class="sub-second">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul class="sub">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
CSS
ul#nav, ul.sub {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
position: relative;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul.sub a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul.sub {
display: none;
position: absolute;
left: 0px;
padding-left: 0;
}
ul.sub-second {
display: none;
list-style: none;
left:100px;
top: 0;
position: absolute;
}
ul#nav li:hover ul.sub {
display: block;
}
ul#nav li:hover ul.sub li:hover ul.sub-second {
display:block;
}
}

Need to change dropdown css menu to drop up

I need to modify this pure css dropdown menu to be a dropup. Saw a similar post, but can't seem to modify mine. Here is the css code being used for the dropdown, which is working as expected. I tried using bottom: 100% within ul.drop li.hover, ul.drop li:hover {, but that didn't work. Any suggestions.
HTML
<body>
<ul id="nav" class="drop">
<li>Home</li>
<li>Portfolio
<ul>
<li>Horses
<ul>
<li>Horses 1
<li>Horses 2
<li>Horses 3
</ul>
</li>
<li>Dogs</li>
<li>People</li>
<li>Stills</li>
</ul>
</li>
<li>Order</li>
<li>Contact Me
</li>
</ul>
</li>
</ul>
CSS
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
width: 130px;
text-align: center;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 0px solid #ffffff;
padding: 0px 0px 0px 0px;
background: transparent;
# margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: transparent;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
width: 160px;
text-align: left;
}
li:hover a { background: transparent; }
li:hover li a:hover {
background: #3ba110;
}
ul.drop a {
display:block; color: #fff; font-family: Verdana; font-size: 14px; text-decoration: none;
}
ul.drop, ul.drop li, ul.drop ul {
list-style: none; margin: 0; padding: 0; border: 1px solid #fff; background: #1302CB; color: #fff;
}
ul.drop {
position: relative; z-index: 597; float: left;
}
ul.drop li {
float: left; line-height: 1.3em; vertical-align: middle; zoom: 1; padding: 5px 10px;
}
ul.drop li.hover, ul.drop li:hover {
position: relative; z-index: 599; cursor: default; background: #3ba110;
}
ul.drop ul {
visibility: hidden; position: absolute; bottom: 100%; left: 0; z-index: 598; width: 160px; background: #cccccc; border: 1px solid #fff;
}
ul.drop ul li {
float: none;
}
ul.drop ul ul {
top: -2px; left: 100%;
}
ul.drop li:hover > ul {
visibility: visible
}
I didn''t use your CSS because I am not sure what is going on without HTML. Anyways I created a basic demo of what I think you are trying to accomplish.
http://jsfiddle.net/krishollenbeck/JgWEL/45/
HTML
<div class="page">
<ul>
<li class="navlink">
MENU ITEM:
<ul class="dropmenu">
<li>Link One</li>
<li>Link Two</li>
<li>Link Three</li>
</ul>
</li>
</ul>
</div>
CSS
.page {
width:200px;
height:100px;
position:fixed;
top:50%;
bottom:50%;
}
.dropmenu {
display:none;
border:1px solid #CCC;
background-color:#eee;
width:150px;
padding:5px;
position:absolute;
top:-70px;
}
.navlink {
background-color:#000;
padding:5px;
}
.navlink > a {color:#FFF;}
.navlink:hover > .dropmenu {display:block;}
​

Resources