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.
Related
I would like a deconnexion button to appear when hovering on the menu "Bonjour Toi".
But it's showed on 2 lines instead of 1.
As "Toi" can be changed according to user name, when the name is longer the menu deconnexion is correctly showed on 1 line.
Here is what i have now:
Here is my html code:
.nav-top {
background-color: #475162;
height: 70px;
width: 100%;
text-align:center;
padding:10px;
}
.nav-top nav .logo{
float:left;
}
.nav-top nav .logo{
margin-left:20px;
}
.nav-top nav ul {
padding: 0px;
margin: 0px;
float: right;
}
.nav-top nav ul {
margin-right: 50px;
}
.nav-top nav li {
display: inline;
}
.nav-top nav li a {
color: white;
font-size: 1em;
line-height: 70px;
padding: 5px 15px;
cursor: pointer;
font-family: Raleway, arial;
}
.nav-top ul li a.active {
border: solid 1px #FF307E;
}
.nav-top ul li a:hover:not(.active) {
border: solid 1px #FF307E;
}
/* BUTTON DECONNEXION */
.nav-top nav ul li{
display:inline-block;
position:relative;
}
.nav-top nav ul li ul{
position:absolute;
z-index: 1000;
max-height:0;
left: 0;
right: 0;
overflow:hidden;
}
.nav-top nav ul li:hover ul{
max-height:15em;
}
.nav-top nav ul li ul a{
padding:8px 5px;
text-decoration: none;
}
.nav-top nav ul li ul a img{
vertical-align:middle;
}
.nav-top nav ul li:hover li a{
background-color: #FF307E;
color:white;
text-transform:inherit;
}
<div class="nav-top">
<nav>
<div>
<div>
<a routerLink='./'><img class="logo" src="./assets/img/logo.png" height="70px"></a>
<ul>
<li><a>BLOG</a></li>
<li><a>CONTACT</a></li>
<li><a>SUPPORT</a></li>
<li ><a class="active ">Bonjour {{nameUserConnected}}</a>
<ul>
<li><a (click)="confirmLogout()"><img src="./assets/img/logout.png" width="17px" /> Déconnexion</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
How can I get the deconnexion + icon on the same line ?
You have a few problems here. The ul that is positioned absolute will have the width of the element it's relative to. Which is the li containing Bonjour Toi . That's why when it's longer, it will fit. If the text is smaller , the ul won't fit. You also set overflow:hidden on it , you need to remove that
I changed a bit your code ( the image i've set it like a background-image and padding-left of a is equal to the width of image, change it as you like )
All new/changed code is at the top of the CSS styles
see below
.nav-top nav ul li ul {
position: absolute;
z-index: 1000;
max-height: 0;
left: 0;
top: 100%;
display: none;
}
.nav-top nav ul li ul li a {
padding-left: 30px;
background: url('https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico') no-repeat scroll left center #FF307E;
}
.nav-top nav ul li ul {
width: 100%;
display: none;
}
.nav-top nav ul li:hover > ul {
display: block;
overflow: visible;
}
.nav-top {
background-color: #475162;
height: 70px;
width: 100%;
text-align: center;
padding: 10px;
}
.nav-top nav .logo {
float: left;
}
.nav-top nav .logo {
margin-left: 20px;
}
.nav-top nav ul {
padding: 0px;
margin: 0px;
float: right;
}
.nav-top nav ul {
margin-right: 50px;
}
.nav-top nav li {
display: inline;
}
.nav-top nav li a {
color: white;
font-size: 1em;
line-height: 70px;
padding: 5px 15px;
cursor: pointer;
font-family: Raleway, arial;
}
.nav-top ul li a.active {
border: solid 1px #FF307E;
}
.nav-top ul li a:hover:not(.active) {
border: solid 1px #FF307E;
}
/* BUTTON DECONNEXION */
.nav-top nav ul li {
display: inline-block;
position: relative;
}
.nav-top nav ul li:hover ul {
max-height: 15em;
}
.nav-top nav ul li ul a {
padding: 8px 5px;
text-decoration: none;
}
.nav-top nav ul li ul a img {
vertical-align: middle;
}
.nav-top nav ul li:hover li a {
background-color: #FF307E;
color: white;
text-transform: inherit;
}
<div class="nav-top">
<nav>
<div>
<a routerLink='./'><img class="logo" src="./assets/img/logo.png" height="70px"></a>
<ul>
<li><a>BLOG</a></li>
<li><a>CONTACT</a></li>
<li><a>SUPPORT</a></li>
<li><a class="active ">Bonjour Toi</a>
<ul>
<li><a (click)="confirmLogout()">Déconnexion</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
I've basically copy/pasted the code from Drop-down menu that opens up/upward with pure css
body {
padding: 3em;
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu {
float: center;
text-align: center;
line-height: 10px;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu" style="text-align:center; float: center; width: 100%">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
No matter what I do, I cannot align the menu to sit in the center of the web page.
Your help would be highly appreciated.
Thanks in advance.
Like this? I added the following css:
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
body {
padding: 3em;
}
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
You can horizontally center #menu by following CSS rules:
define #menu's display as inline-block
Add a container div outside of #menu and make its text-align as center.
I've made a fiddle for your code, please check.
Update your HTML code as below.
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel = "stylesheet" type = "text/css" href = "menu.css"/>
<title>Frames Test</title>
</head>
<body>
<iframe style="width:100%">padding</iframe>
<div class="container" style="text-align:center">
<div id="menu" style="text-align:center; float: center;display:inline-block">
<ul>
<li><center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
</div>
</body>
</html>
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/
I have a navigation menu with 3 sub levels. I need the 3rd level list to align left but I can't get it to behave. I can get levels 2 and 3 to align relatively with the main menu but I want the 2nd level to align relatively and the 3rd level aligning completely to the left. I'd appreciate any help/advice going, here is what I have:
JSFIDDLE HERE
HTML:
<ul class="nav">
<li>LINK1</li>
<li>LINK2
<ul class="#">
<li>LINK2a
<ul class="#">
<li>LINK2b</li>
<li>LINK2c</li>
<li>LINK2d</li>
<li>LINK2e</li>
<li>LINK2f</li>
</ul>
</li>
</ul>
</li>
<li>LINK3</li>
<li>LINK4</li>
<li>LINK5</li>
</ul>
CSS:
a, img { border:none; }
* { margin:0; padding:0; }
.nav{
display:block;
list-style:none;
text-align: left;
position: relative;
height: 30px;
}
.nav li {
display: block;
position: relative;
float: left;
border: 1px solid #FFF;
}
.nav li a {
width: 125px;
height: 30px;
display: table-cell;
vertical-align: middle;
text-decoration: none;
font-size: 12px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align: center;
color: #000;
z-index: 2000;
background-color: #999;
}
li.active {
background-color: #999 ;
color: #000;
}
.nav li a:hover {
top: 30px;
}
.nav li:hover > a {
color:#fff;
background-color:#333;
}
.nav li ul {
width: 125px;
height: 25px;
position: absolute;
left: -9999px;
border: 1px solid #FFF;
}
.nav li:hover ul {
left: -2px;
}
.nav li li:hover > a {
color:#000;
background-color:#999;
}
.nav li li{
position: relative;
border: 1px solid #FFF;
height: 25px;
}
.nav li li a {
height: 25px;
top: 25px;
text-decoration: none;
font-size: 9px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align: center;
display: table-cell;
vertical-align: middle;
color: #000;
z-index: 2000;
background-color: #999;
}
li li.active {
background-color: #a9d9d9 ;
color: #007f7b;
}
.nav li li:hover ul {
left: -2px;
top: 25px;
}
ul.nav li > ul {
width: 700px;
height: 25px;
background-color:blue;
}
ul.nav li > ul > li > ul {
background-color:red;
height: 25px;
}
ul.nav li > ul > li { display: none; }
ul.nav li:hover li { display: block; }
Well, you are missing some simple changes CSS to make it work properly. I'll post what changes I made here to make it work:
.nav li ul became .nav li > ul
.nav li:hover ul became .nav > li:hover > ul
.nav li li:hover ul became .nav li > li:hover ul
ul.nav li > ul > li { display: none; } became ul.nav li ul ul { display: none; }
ul.nav li:hover li { display: block; } became ul.nav li:hover > ul { display: block; }
Added:
ul.nav li ul ul {
position: absolute;
top:25px;
left: -129px;
}
ul.nav li li:hover > a {
color: white;
background-color: #333
}
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