I have the following HTML snippet for my menu bar:
#menubar {
display: block;
background: #FFFFFF;
}
#menubar ul li a.menubutton {
display: none;
/* dont show burger symbol (mobile menu symbol) */
}
#menubar ul {
display: block;
width: 2em;
/* restrict burger symbol */
padding: 0.9em;
}
#menubar ul li {
display: inline;
}
#menubar ul li ul li {
display: none;
}
/*Header*/
header {
display: block;
background: #2F2C2C;
text-align: center;
}
/* Navigation */
nav {
display: block;
height: 2.5em;
background: #FFFFFF;
text-align: center;
}
nav ul {
display: block;
}
nav ul li {
display: inline;
margin: 0em 0.188em 0em 0.188em;
}
nav ul li a {
color: #454040;
font-size: 1.125em;
line-height: 2.5em;
padding: 0.563em 0.938em 0.375em 0.983em;
transition: background 0.2s;
/* nice transition effect */
-webkit-transition: background 0.2s;
}
nav ul li a:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
nav ul li a.active {
border-bottom: 0.188em solid #E7590B;
}
nav ul li ul {
display: none;
}
nav ul li:hover ul.submenu {
text-align: center;
position: fixed;
display: block;
margin-left: 40%;
}
nav ul li ul li:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
<nav class="nav">
<ul>
<li>Start</li>
<li>Termine</li>
<li>Organisation
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>About
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
The ul #submenu are displayed on hover.
I managed to center the submenus with the whole page using left: 40% in the nav ul li:hover ul.submenu rule. However, I want to position the submenus centered to their parent components, so taking organisation for example I'd like to have the page 2 submenu centered with the parents organisation field and for about the same.
How can I achieve to center relatively to the parent component?
Add position: relative to the menu entries which have a submenu (i.e. to the nav ul li selector) and change position: fixed to position: absolute for the submenu, i.e. the nav ul li:hover ul.submenu selector.
This makes the submenu position "relative" to their parent (the main menu entry), so you should then adjust the left and top settings in the submenu CSS rule accordingly.
#menubar {
display: block;
background: #FFFFFF;
}
#menubar ul li a.menubutton {
display: none;
/* dont show burger symbol (mobile menu symbol) */
}
#menubar ul {
display: block;
width: 2em;
/* restrict burger symbol */
padding: 0.9em;
}
#menubar ul li {
display: inline;
}
#menubar ul li ul li {
display: none;
}
/*Header*/
header {
display: block;
background: #2F2C2C;
text-align: center;
}
/* Navigation */
nav {
display: block;
height: 2.5em;
background: #FFFFFF;
text-align: center;
}
nav ul {
display: block;
}
nav ul li {
display: inline;
margin: 0em 0.188em 0em 0.188em;
position: relative;
}
nav ul li a {
color: #454040;
font-size: 1.125em;
line-height: 2.5em;
padding: 0.563em 0.938em 0.375em 0.983em;
transition: background 0.2s;
/* nice transition effect */
-webkit-transition: background 0.2s;
}
nav ul li a:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
nav ul li a.active {
border-bottom: 0.188em solid #E7590B;
}
nav ul li ul {
display: none;
}
nav ul li:hover ul.submenu {
text-align: center;
position: absolute;
display: block;
left: -50%;
}
nav ul li ul li:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
<nav class="nav">
<ul>
<li>Start</li>
<li>Termine</li>
<li>Organisation
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>About
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
Hi I just read your question. So you will have to implement this with your own class names. But this is how to centre your child elements inside their respective parents.
.child-class{
width:max-content;
margin:0px auto;
}
In order for this to work you have to have a width. This will auto centre align your div or whatever tag you are using.
I recommend this method over position relative, because what's really convenient about using this method is that it will centre align your content evenly even if the different li tags are different widths. Which is convenient for mobile optimisation. :)
Related
In the below snippet I have a CSS menu using nested lists. A problem I have with it is that when you hover over the second list item, it reveals the nested list but in the process, increases the parent list's height pushing everything else down.
I'm aware I can use a position of absolute however that leads to a problem of the nested list not sitting below it's parent element and making it incredibly annoying to style for each nested list I may want.
Is there a simple way I can solve my problem while maintaining the nested loop sitting below it's parent (and by extension, making it possible to access with the :hover)
* {
margin: 0;
padding: 0;
}
nav ul {
list-style-type: none;
background: #000;
text-align: center;
}
nav ul li {
display: inline-block;
}
nav ul li a {
display: inline-block;
padding: 20px;
color: #fff;
text-decoration: none;
}
nav ul li a:hover {
background-color: #3ab795;
text-decoration: underline;
}
nav ul li > ul {
display: none;
position: relative;
left: 50px;
border: 1px solid #fff;
}
nav ul li > ul li {
display: block;
color: #fff;
}
nav ul li:hover > ul {
display: block;
}
<nav>
<ul>
<li>Item-1</li>
<li>Item-2
<ul>
<li>Item-2A</li>
<li>Item-2B</li>
<li>Item-2C</li>
<li>Item-2D</li>
</ul>
</li>
<li>Item-3</li>
<li>Item-4</li>
</ul>
</nav>
I hope your issue is fixed in below fiddle. Try it.
* {
margin: 0;
padding: 0;
}
nav ul {
list-style-type: none;
background: #000;
text-align: center;
}
nav ul li {
display: inline-block;
position: relative;
}
nav ul li a {
display: inline-block;
padding: 20px;
color: #fff;
text-decoration: none;
}
nav ul li a:hover {
background-color: #3ab795;
text-decoration: underline;
}
nav ul li > ul {
display: none;
position: absolute;
left: 50px;
top:100%;
border: 1px solid #fff;
}
nav ul li > ul li {
display: block;
color: #fff;
}
nav ul li:hover > ul {
display: block;
}
<nav>
<ul>
<li>Item-1</li>
<li>Item-2
<ul>
<li>Item-2A</li>
<li>Item-2B</li>
<li>Item-2C</li>
<li>Item-2D</li>
</ul>
</li>
<li>Item-3</li>
<li>Item-4</li>
</ul>
</nav>
For this you will need to understand the concept of position...Use position:absolute for the drop-menu and position:relative for its parent li...no need to write css for every drop-menu
* {
margin: 0;
padding: 0;
}
nav ul {
list-style-type: none;
background: #000;
text-align: center;
}
nav ul li {
display: inline-block;
position: relative;
}
nav ul li a {
display: inline-block;
padding: 20px;
color: #fff;
text-decoration: none;
}
nav ul li a:hover {
background-color: #3ab795;
text-decoration: underline;
}
nav ul li>ul {
display: none;
position: absolute;
top: 100%;
left: 0px;
border: 1px solid #fff;
min-width: 150px;
}
nav ul li>ul li {
display: block;
color: #fff;
}
nav ul li:hover>ul {
display: block;
}
<nav>
<ul>
<li>Item-1</li>
<li>Item-2
<ul>
<li>Item-2A</li>
<li>Item-2B</li>
<li>Item-2C</li>
<li>Item-2D</li>
</ul>
</li>
<li>Item-3
<ul>
<li>Item-3A</li>
<li>Item-3B</li>
<li>Item-3C</li>
<li>Item-3D</li>
</ul>
</li>
<li>Item-4</li>
</ul>
</nav>
There is nothing to worry about using absolute position for submenu. just make the parent relative. According to your code
nav ul li {
display: inline-block;
position: relative; // Added
}
and than modify nested ul like this
nav ul li > ul {
display: none;
position: absolute; // Added
left: 0; // Changed
border: 1px solid #fff;
width: 160px; // Change as per your requirement
}
My sub menu is disappearing on hover. When I hover over the menu item it appears but when i try to go to the sub menu item.. it goes away. Any idea why?
I have tried doing this:
.nav ul li:hover ul {
display: block !important;
}
But i still have the same issue. Any help will be appreciated!
HTML:
<div class="nav">
<ul>
<li>
Testing
<ul>
<li>Testing 1</li>
</ul>
</li>
</ul>
</div>
CSS:
.nav ul {
letter-spacing: 2px;
margin-top: 10px;
}
.nav ul li {
display: inline-block;
border-right: 1px solid #7d7a7a;
}
.nav ul ul li {
border-right: none;
}
.nav ul li:last-child {
border-right: none;
}
.nav ul li a {
display: inline-block;
color: #000;
text-decoration: none;
padding: 0 10px;
height: 80%;
}
.nav ul li a i {
color: #000;
}
.nav ul ul li:hover ul {
display: block;
}
.nav ul li:hover ul {
display: block !important;
}
.nav ul li ul {
position: absolute;
display: none;
background-color: #333;
height: auto;
top: 34px;
padding: 13px 10px;
}
.nav ul li ul li:hover {
background-color: #47a3da;
}
JSFiddle demo
It's happening because there is a gap between the dropdown and the button.
You need to get rid of any margin and top for the dropdown to be right under the button.
Demo
.nav > ul {
letter-spacing: 2px;
margin-top: 10px;
}
.nav ul li ul {
position: absolute;
display: none;
background-color: #333;
height: auto;
padding: 13px 10px;
}
Since you parent li and its dropdown menu-item has extra space between them, dropdown ul losses the event of .nav ul li ul li:hover. To make it work,
simply adjust the vertical distance b/w parent and its dropdown child menu-item
.nav ul li ul {
position: absolute;
display: none;
background-color: #333;
height: auto;
top: 18px; /* Works fine on 18px*/
padding: 13px 10px;
}
JSFiddle
How can i center this nav bar so it stays in the center of the screen despite the users resolution setting? Also how to stop the dropdown menu on 'Portfolio' stretching the menu?
http://jsfiddle.net/y4vDC/382/
<ul id="menu">
<li>Home</li>
<li>
About
<ul class="hidden">
<li>Who We Are</li>
<li>What We Do</li>
</ul>
</li>
some of the html....
This should get you started. The important thing is that floating makes centering items quite difficult.
display:inline-block works much better as you can center thme list items by applying text-align:center to the parent ul.
Then it's just a case of using positioning to set the location and size of the submenu.
/*Strip the ul of padding and styling*/
ul {
list-style-type: none;
margin: 0;
padding: 0;
/* center contents of menu */
text-align: center;
}
/*Create a horizontal list with spacing*/
li {
display: inline-block;
vertical-align: top;
margin-right: 1px;
/* create positioning context */
position: relative;
}
/*Style for menu links*/
li a {
display: block;
min-width: 140px;
/* removed set height */
min-height: 50px;
line-height: 50px;
text-align: center;
font-family: helvetica, arial, sans-serif;
color: #ffffff;
background: #6BD6FA;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #A0A2A3;
color: #ffffff;
}
/*Hover state for dropdown links*/
li ul a:hover {
background: #bada55;
color: #ffffff;
}
/*Hide dropdown menu until are needed*/
li ul {
display: none;
position: absolute;
top: 100%;
left: 0;
height: auto;
}
/*Show dropdown menu on hover */
li:hover ul {
display: block;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
}
<ul id="menu">
<li>Home
</li>
<li> About
<ul class="hidden">
<li>Who We Are
</li>
<li>What We Do
</li>
</ul>
</li>
<li>Portfolio
<ul class="hidden">
<li>Web & User Interface Design
</li>
</ul>
</li>
<li>News
</li>
<li>Contacts
</li>
</ul>
/*Strip the ul of padding and styling*/
ul {
list-style-type: none;
margin: 0;
padding: 0;
text-align:center;
}
/*Create a horizontal list with spacing*/
li {
display: inline-block;
margin-right: 0px;
}
ul#menu > li{
position:relative;
}
ul > li > ul{
position:absolute;
}
ul > li > ul li{
white-space:nowrap;
}
/*Style for menu links*/
li a {
display: block;
padding:25px 50px;
line-height: 50px;
text-align: center;
font-family: helvetica, arial, sans-serif;
color: #ffffff;
background: #6BD6FA;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a{
background: #A0A2A3;
color: #ffffff;
}
/*Style for dropdown links*/
li:hover a {
background: #A0A2A3;
color: #ffffff;
height: 50px;
line-height: 50px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #A0A2A3;
color: #ffffff;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
}
/*Display dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
I updated jsfiddle too: http://jsfiddle.net/y4vDC/385/
try this
ul {
list-style-type: none;
margin: 0 auto;
width:80%;
padding:0;
}
I want to add a dropdown menu to my navigation bar but i am not being able to do it properly ? Can anyone help me to properly format it so that that would drop down properly whenever hovered ?
HTML:
<nav>
<ul>
<li>Home</li>
<li>About
<ul>
<li>Me</li>
<li>Website</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
CSS:
nav {
background-color: #311310;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
position: absolute;
left: 0px;
width: 100%:
}
nav ul li ul li {
background: #311310;
display: block;
}
nav ul{
margin: 0px;
padding: 10px 0px 10px 100px;
}
nav ul li {
color: #d9d9d9;
display: inline;
padding: 0px 10px;
font-family: klavika;
font-size: 14pt;
}
nav ul li a {
color: #d9d9d9;
text-decoration: none;
}
nav ul li a:hover{
color: #ffffff;
}
And here is the broken navigation bar Snap :
How can i display the nested one right below about ?
Give position: relative; to the parent:
nav ul li {
color: #d9d9d9;
display: inline;
padding: 0px 10px;
font-family: klavika;
font-size: 14pt;
position: relative;
}
And position: absolute; to the menu:
nav ul li ul {
background: #311310;
display: block;
position: absolute;
left: 0;
}
Try this code
add position: relative for nav ul li
I'm trying to create a hover effect in my drop down menu but I can't figure out how to do it.
What I want to do is this:
instead of this:
My Code - You can also see it here, (updated version)
HTML:
<nav>
<ul>
<li>One
<ul>
<li>1.1</li>
<li>1.2
</ul>
</li>
<li>Two
<ul>
<li>2.1</li>
<li>2.2</li>
<li>2.3</li>
</ul>
</li>
<li>Three
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
<li>Four</li>
</ul>
</nav>
CSS:
nav {
float: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content:"";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
}
nav ul li:hover {
background-color: #000;
}
nav ul li a:hover {
color: #fff;
}
nav ul li a {
display: block;
padding: 25px 15px;
color: #6F0;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #000;
}
Any help will be appreciated.
Working fiddle:
Change the styles for the below two:
nav ul li:hover {
border-bottom: 1px solid #000;
color: #000;
}
nav ul li a:hover {
color: #000;
}
And add:
nav ul li:hover li:hover{
background: #000;
}
In order to style the sub-menus.
The first (li:hover) you want to set a bottom border - you can change the width of this border from 1px to something more thick, say, 3px