When I hover over the html section the html & css nav option is moved to the right for some reason, how do I get it to align with the other nav options.
http://codepen.io/anon/pen/bEqPRQ
#nav {
list-style: none;
text-align: center;
padding: 1.250em 0 1.250em 0;
background: #ffffff;
font-size: 106.25%;
font-family: 'Oxygen';
}
#nav li a {
text-decoration: none;
text-align: center;
color: #FFF;
}
#nav ul li ul.dropdown {
background-color: #000000;
display: none;
position: fixed;
z-index: auto;
left: 400;
vertical-align: left;
}
#nav ul li:hover ul.dropdown {
display: block;
}
#nav ul li ul.dropdown li {
display: block;
}
#nav > ul > li {
display: inline-block;
margin-right: 2%;
padding: 4.375em 1.250em 4.375em 1.250em;
}
#nav > ul > li > a {
text-decoration: none;
color: #000000;
}
#nav > ul > li > a:hover {
color: #00c5a2;
}
Your <ul> has a default styling (padding-left) which has to be removed. Adding
#nav ul li ul.dropdown {
padding-left: 0;
}
moves your text to the left.
You should also check your CSS, vertical-align: left and left: 400 (doesn't have a unit) aren't valid and won't work.
You can solve the problem adding padding left = 0 in the css of #nav ul li ul.dropdown :
#nav ul li ul.dropdown {
padding-left: 0;
}
Try changing the .dropdown style as follows:
#nav ul li ul.dropdown {
background-color: #000000;
display: none;
position: absolute;
z-index: auto;
left: 0;
vertical-align: left;
}
Then the parent li position to relative:
#nav > ul > li {
display: inline-block;
margin-right: 2%;
padding: 4.375em 1.250em 4.375em 1.250em;
position: relative;
}
This ensures the dropdown always positions itself absolute to its parent (the li).
Also adding padding: 0 to the dropdown ul:
#nav ul li:hover ul.dropdown {
display: block;
padding: 0;
}
Fiddle: https://jsfiddle.net/04zospoj/
Related
I'm working on old Wordpress page created with myMag theme, and I need to centralize menu bar, I'm pretty new to CSS and looking for various solutions I cant find any solutions for my case.
the CSS file looks like this:
#navcontainer #navbar {
float: left;
height: 51px;
line-height: 50px;
margin-left: -15px;
margin-top: 1px;
}
margin:0;
padding:0;
width:900px;
list-style-type:none;
display:table;
position:relative;
line-height:50px;
z-index:5;
ul {
text-align: justify
}
li {
display: inline-block
}
#navcontainer #navbar a {
height: 40px;
display: block;
padding: 0 21px;
text-decoration: none;
text-align: center;
line-height: 28px;
outline: none;
float: left;
z-index: 35;
position: relative;
color: #FFF;
text-shadow: 1px 1px 0 #515151;
font-weight: bold;
}
#navcontainer #navbar a:hover {
color: #CCC;
}
#navcontainer #navbar ul a {
line-height: 30px;
}
#navcontainer #navbar li {
float: left;
position: relative;
z-index: 20;
margin-top: 10px;
}
#navcontainer #navbar li li {
border-left: none;
margin-top: 0;
}
#navcontainer #navbar ul {
position: absolute;
display: none;
width: 172px;
top: 38px;
left: -1px;
background: #f5f5f5;
}
#navcontainer #navbar li ul a {
width: 130px;
height: auto;
float: left;
text-align: left;
padding: 0 21px;
color: #606060;
text-shadow: none;
}
#navcontainer #navbar ul ul {
top: auto;
}
#navcontainer #navbar li ul ul {
left: 172px;
top: 0px;
}
#navcontainer #navbar li ul ul a {
border-left: 1px solid #CCC;
}
#navcontainer #navbar ul {
border-top: none;
}
#navcontainer #navbar ul a,
#navcontainer #navbar ul li {
background-color: #e7e7e7;
}
#navcontainer #navbar ul a:hover,
#navcontainer #navbar ul a:focus {
color: #18649a;
}
#navcontainer #navbar li:hover ul ul,
#navcontainer #navbar li:hover ul ul ul,
#navcontainer #navbar li:hover ul ul ul ul {
display: none;
}
#navcontainer #navbar li:hover ul,
#navcontainer #navbar li li:hover ul,
#navcontainer #navbar li li li:hover ul,
#navcontainer #navbar li li li li:hover ul {
display: block;
}
php that creates menu looks like this:
<div id="navcontainer">
<div class="navigation">
<div class="wrapper">
<ul id="navbar">
<?php $exclude = $myOptions['pages_excl'];?>
<?php if(!$myOptions['pages_limit']) {
$limit = '10'; } else {
$limit = $myOptions['pages_limit'];
}
if($myOptions['pages_limit'] == '0') {
$limit = 0;
}
?>
<li>Home</li>
<?php wp_list_pages("exclude=$exclude&title_li=&depth=3&number=$limit"); ?>
</ul>
</div>
</div>
</div>
You need to give the #navbar a fixed width and change the margin as following:
#navcontainer #navbar {
width: 600px; // change this to a width that suits your menu
height: 51px;
line-height:50px;
margin-left: auto;
margin-right: auto;
margin-top: 1px;
}
margin-left:auto; and margin-right: auto; center the navigation, this can only be done if the navigation has a fixed width. So you will need to adjust the fixed width when you change the number of items in the navigation.
I've tried a bunch of techniques to solve this. I've used float (left/right) and played with positioning (absolute & relative). None worked.
Basically my navigation keeps moving when the the text decoration (font-weight: bold; in this case) is applied.
I'm new to CSS and this is my first header- so any feedback on the header/site-navigation as a whole is welcome. My second layer nav (ul ul) seems to be lining up vertically, and seems to be using effects from the previous layer. This needs fixing too.
Here is my code:
.main-navigation {
left:50%;
transform: translateX(-50%);
position: absolute;
padding-top:0em;
margin: 0px auto;
z-index: 999;
border-top: 2px solid;
border-bottom: 1px solid;
display: inline-block;
}
.main-navigation-body {
width:1000px;
margin-left:auto;
margin-right:auto;
text-align: center;
}
.main-navigation-body ul {
list-style: none;
margin: 0;
padding-left: 0;
}
.main-navigation-body a {
color:black;
}
.main-navigation-body a:visited {
}
.main-navigation li {
display: inline-block;
text-align: center;
padding:1.64em;
padding-top: 0.3em;
padding-bottom: 0.3em;
}
.main-navigation a {
text-decoration: none;
}
.main-navigation ul ul {
box-shadow: 0 3px 3px rgba(0, 0, 0, 0.2);
position: absolute;
top: 2.0em;
left: -999em;
z-index: 99999;
background-color:pink;
width: 100px;
text-align:left;
}
.main-navigation ul ul ul {
left: -999em;
top: 0;
}
.main-navigation ul ul a {
}
.main-navigation ul ul li {
display: inline-block;
border-top: 0px solid;
border-bottom: 0px solid;
padding:0.1em;
}
.main-navigation li:hover > a,
.main-navigation li.focus > a {
font-weight: bold;
}
.main-navigation ul ul :hover > a,
.main-navigation ul ul .focus > a {
}
.main-navigation ul ul a:hover,
.main-navigation ul ul a.focus {
font-weight: bold;
}
.main-navigation ul li:hover > ul,
.main-navigation ul li.focus > ul {
left: auto;
}
.main-navigation ul ul li:hover > ul,
.main-navigation ul ul li.focus > ul {
left: 100%;
}
.main-navigation .current_page_item > a,
.main-navigation .current-menu-item > a,
.main-navigation .current_page_ancestor > a {
font-weight: bold;
color: #BB9A69;
}
Here is the website
Thanks,
This is normal behaviour: Bold text is wider, thus repositioning the centered text around it.
You could go for a fixed width on all links to prevent this:
.main-navigation a {
text-decoration: none;
min-width: 6em;
display: inline-block;
}
I have a CSS menu using the following CSS.
What is the best way to center the whole menu on the page?
I have tried using another <div> outside <nav> and setting margins but its just aligning left all the time.
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;
}
nav ul li {
float: left;
}
nav ul li:hover a {
color: #000000;
}
nav ul li a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
}
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;
}
jsfiddle : http://jsfiddle.net/njuVm/
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.
Instead of floating the li, you can display them as inline-blocks.
Then, they will be centered relatively to the ul because of text-align: center.
Since the ul is as wide as the nav by default, the li will look like centered relatively to the nav.
nav {
text-align: center;
border: 1px solid black;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav > ul > li {
display: inline-block;
}
nav a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
}
nav li:hover > ul {
display: block;
}
nav > ul ul {
display: none;
position: absolute;
}
nav > ul ul > li {
border-bottom: 1px solid #000000;
}
nav > ul ul a:hover {
color: #666666;
}
<nav>
<ul>
<li>Add Contact</li>
<li>View Contact</li>
<li>Tickets
<ul>
<li><a>TEST1</a></li>
<li><a>TEST2</a></li>
</ul>
</li>
<li>Invoices</li>
<li>Itemised Calls</li>
</ul>
</nav>
First, when you float the ul's you have to clear the float by adding clear div:
HTML :
<div class="clear"></div>
CSS :
.clear{
clear:both;
}
And for centring the menu you should specify a width of the ul as in example and randomly I have set the width to 560px :
nav ul {
list-style: none;
width : 560px;
margin: 0 auto;
}
Take a Look:
http://jsfiddle.net/njuVm/6/
This discussion has already been great in helping me set my nav bar. But now, my child links have large spaces between and do not line up with the parent links.
My site can be seen here
Any ideas on how to align my child links (Los Angeles, San Diego Honolulu...etc.) with the parent link (Location)? Also, how do I reduce the space between the child links (Los Angeles, San Diego, Honolulu...)?
This is my CSS for quicker reference.
#mainNav {
/*font-size: 1.0em; doesn't seem to do anything*/
/*height: 20px; doesn't seem to do anything*/
margin-top: -10px;
position: absolute;
right: 5%;
top: 40%;
}
/*-------------------------------*/
/*----------Below is CSS for List of Parent Links---------------------*/
#mainNav ul {
list-style-type: none;
position: relative;
display: inline-table;
}
/*----------Below is CSS for List of Location Links---------------------*/
#mainNav ul ul{
list-style: none;
display: none;
}
/*----------Below is CSS for List of Location---------------------*/
#mainNav ul li {float: left;
text-decoration: none;
margin: 0 0 0 25px;
padding-top: 0;
float: left;
width: auto;
list-style: none outside none;
font-size: 0.90em;
text-transform: uppercase;}
#mainNav ul li a {display: block; text-decoration: none;}
#mainNav ul ul {
display: none; /*if this is block, it causes all the sub links to show at once!*/
position: absolute;
width: auto;
top: 35%;
}
#mainNav ul li:hover > ul { display: block;}
#mainNav ul ul li {width: 100%;}
#mainNav li li {
float: none;
width: auto;
}
You can try this css
#mainNav ul ul {
display: none;
position: absolute;
width: auto;
top: 13px;
left: 0;
margin: 0;
padding: 0;
line-height: normal;
}
#mainNav ul ul li {
width: 100%;
margin: 0;
padding: 0;
background: red;
margin-top: 2px;
}
#mainNav ul li{}
#mainNav ul li > ul > li a {
height: auto;
line-height: 21px;
margin: 0;
padding: 0;
text-align: left;
}
Result images
This code ended up doing it for me. I think you copied and pasted wrong Rohit!
li{
list-style: none;
display: inline-block;
/*width: 150px;*/
max-width: auto;
}
ul li a{
text-decoration: none;
display: block;
text-align: center;
padding: 10px; /*needed for spacing between LOCATIONS and LOS ANGELES*/
}
ul li ul li a{
padding-top: 5px;
margin-top: 5px;
float: left;
font-weight:bold;
}
ul ul{
position: absolute;
visibility: hidden;
}
#mainNav ul li:hover ul{
visibility: visible;
padding: 0;
}
ul li ul li{
display: block;
}
Click here for the latest.
I am having a simple problem with my CSS dropdown menu, I want to make it so the dropdowns are equal length to the button above them. See example below, I just want to stretch the block to the length of the above block.
New user, can't upload image
If you use my jsFiddle link you can see my code, and edit it live. I'll post the code anyway just in case.. Note I am only posting the CSS stylesheet in here as the HTML is not part of the problem.
/* Dropdown Menu */
#nav {
float: right;
width: 600px;
height: 90px;
margin: 0 auto;
padding-top: 65px;
}
#nav ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
float: right;
}
#nav ul li {
display: block;
position: relative;
float: left;
}
#nav li ul { display: none; }
#nav ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #3636FE;
margin-left: 1px;
white-space: nowrap;
}
#nav ul li a:hover { background: #3636FE; }
#nav li:hover ul {
display: block;
position: absolute;
}
#nav li:hover li {
float: none;
font-size: 11px;
}
#nav li:hover a { background: #3636FE; }
#nav li:hover li a:hover { background: #6868FE; }
Fixed. (at least for Chrome+Firefox+IE9)
http://jsfiddle.net/QZWj3/4/
You had to add width: 100% to #nav li:hover ul and to #nav li:hover li