I have tried many changes but none of them work, i also search in Stackoverflow but i couldn't put it working correctly.
The 'li' inside the submenu 'ul' are overlapping in just one sub item instead of showing the 4 sub items.
The HTML:
<div id="menu">
<div class="container">
<div class="row">
<ul class="menu-list">
<li>Menu Item 1</li>
<li>Menu Item 2
<ul class="menu-sub-list">
<li>Sub-menu Item 1
<li>Sub-menu Item 2
<li>Sub-menu Item 3
<li>Sub-menu Item 4
</ul>
</li>
<li>Menu Item 3</li>
<li>Menu Item 4</li>
<li>Menu Item 5</li>
</ul>
</div>
</div>
</div>
The CSS:
#menu {
margin: 3% 0;
background-color: rgb(255,138,61);
height: 70px;
}
#menu ul.menu-list {
list-style: none;
text-align: center;
line-height: 70px;
height: 70px;
display:block;
clear:both;
}
#menu ul.menu-list li {
display: inline-block;
padding: 0 20px;
text-transform: uppercase;
font-size: 1.05em;
height: 70px;
position:relative;
}
#menu ul.menu-list li:hover {
background-color: rgb(255,102,0);
}
#menu a {
color: #fff;
text-decoration: none;
display:block;
}
#menu ul.menu-list ul.menu-sub-list {
display: none;
}
#menu ul.menu-list li:hover > ul {
display: block;
}
#menu ul.menu-list > li > ul > li {
overflow: hidden;
display: block;
width: 100%;
padding: 10px;
text-transform: uppercase;
font-size: 1.05em;
height: 70px;
position: absolute;
top: 100%;
left: 0px;
z-index: 1;
background: #f2f2f2;
float: none;
}
It's because each sub-navigation element has position: absolute; in your code, making them overlap. Only the submenu wrapper needs to be position: absolute;
http://jsfiddle.net/8kzxyj3g/
#menu {
margin: 3% 0;
background-color: rgb(255,138,61);
height: 70px;
}
#menu ul.menu-list {
list-style: none;
text-align: center;
line-height: 70px;
height: 70px;
display:block;
clear:both;
top: 100%;
}
#menu ul.menu-list li {
display: inline-block;
padding: 0 20px;
text-transform: uppercase;
font-size: 1.05em;
height: 70px;
position:relative;
}
#menu ul.menu-list li:hover {
background-color: rgb(255,102,0);
}
#menu a {
color: #fff;
text-decoration: none;
display:block;
}
#menu ul.menu-list ul.menu-sub-list {
display: none;
position: absolute;
top: 100%;
}
#menu ul.menu-list li:hover > ul {
display: block;
}
#menu ul.menu-list > li > ul > li {
overflow: hidden;
display: block;
width: 100%;
padding: 10px;
text-transform: uppercase;
font-size: 1.05em;
height: 70px;
left: 0px;
z-index: 1;
background: #f2f2f2;
float: none;
}
Related
I know others have asked this question and I'm not able to wrap my head around those solutions due to the complexity of the CSS being used and I have submenus within. This was made by an online menu CSS generator. This is the page that I'm building:
http://sandboxisle.com/home-page-main/
#menu {
background: #00338D;
color: #FFF;
height: 45px;
padding-left: 18px;
border-radius: 10px;
}
#menu ul, #menu li {
margin: 0 auto;
padding: 0;
list-style: none;
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 45px;
padding: 0 14px;
text-decoration: none;
color: #EBB700;
font-size: 16px;
}
#menu a.dropdown-arrow:after {
content: "\25BE";
margin-left: 5px;
}
#menu li a:hover {
color: #FFFFFF;
background: #00338D;
}
#menu input {
display: none;
margin: 0;
padding: 0;
height: 45px;
width: 100%;
opacity: 0;
cursor: pointer
}
#menu label {
display: none;
line-height: 45px;
text-align: center;
position: absolute;
left: 35px
}
#menu label:before {
font-size: 1.6em;
content: "\2261";
margin-left: 20px;
}
#menu ul.sub-menus{
height: auto;
overflow: hidden;
width: 250px;
background: #766A62;
position: absolute;
z-index: 99;
display: none;
}
#menu ul.sub-menus li {
display: block;
width: 100%;
}
#menu ul.sub-menus a {
color: #EBB700;
font-size: 16px;
}
#menu li:hover ul.sub-menus {
display: block
}
#menu ul.sub-menus a:hover{
background: #766A62;
color: #ffffff;
}
#media screen and (max-width: 800px){
#menu {position:relative}
#menu ul {background:#766A62;position:absolute;top:100%;right:0;left:0;z-index:3;height:auto;display:none}
#menu ul.sub-menus {width:100%;position:static;}
#menu ul.sub-menus a {padding-left:30px;}
#menu li {display:block;float:none;width:auto;}
#menu input, #menu label {position:absolute;top:0;left:0;display:block}
#menu input {z-index:4}
#menu input:checked + label {color:white}
#menu input:checked + label:before {content:"\00d7"}
#menu input:checked ~ ul {display:block}
}
Here's my HTML
<nav id='menu'>
<input type='checkbox' id='responsive-menu' onclick='updatemenu()'><label></label>
<ul>
<li><a href='Calendar'>Calendar</a></li>
<li><a href='News'>News</a></li>
<li><a class='dropdown-arrow' href='For Members'>For Members</a>
<ul class='sub-menus'>
<li><a href='Orientation'>Orientation</a></li>
<li><a href='Leadership Building'>Leadership Building</a></li>
<li><a href='Newsletters'>Newsletters</a></li>
</ul>
</li>
<li><a class='dropdown-arrow' href='Resource'>Resource</a>
<ul class='sub-menus'>
<li><a href='Service Documents'>Service Documents</a></li>
<li><a href='District 50 Clubs Directory'>District 50 Clubs Directory</a></li>
<li><a href='Awards & Contests'>Awards & Contests</a></li>
<li><a href='D50 Resource'>D50 Resource</a></li>
<li><a href='Club Resource'>Club Resource</a></li>
<li><a href='LCIF Resource'>LCIF Resource</a></li>
</ul>
</li>
</ul>
</nav>
Please check the code below:
#menu {
background: #00338d;
color: #fff;
height: 45px;
padding-left: 18px;
border-radius: 10px;
}
#menu ul {
margin: 0;
padding: 0;
}
#menu li {
padding: 0;
list-style: none;
}
#menu .navbar {
display: flex;
justify-content: end;
width: 1170px;
margin: 0 auto;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 45px;
padding: 0 14px;
text-decoration: none;
color: #ebb700;
font-size: 16px;
}
#menu a.dropdown-arrow:after {
content: "\25BE";
margin-left: 5px;
}
#menu li a:hover {
color: #ffffff;
background: #00338d;
}
#menu input {
display: none;
margin: 0;
padding: 0;
height: 45px;
width: 100%;
opacity: 0;
cursor: pointer;
}
#menu label {
display: none;
line-height: 45px;
text-align: center;
position: absolute;
left: 35px;
}
#menu label:before {
font-size: 1.6em;
content: "\2261";
margin-left: 20px;
}
#menu ul.sub-menus {
height: auto;
overflow: hidden;
width: 250px;
background: #766a62;
position: absolute;
z-index: 99;
display: none;
}
#menu ul.sub-menus li {
display: block;
width: 100%;
}
#menu ul.sub-menus a {
color: #ebb700;
font-size: 16px;
}
#menu li:hover ul.sub-menus {
display: block;
}
#menu ul.sub-menus a:hover {
background: #766a62;
color: #ffffff;
}
#media screen and (max-width: 800px) {
#menu {
position: relative;
}
#menu ul {
background: #766a62;
position: absolute;
top: 100%;
right: 0;
left: 0;
z-index: 3;
height: auto;
display: none;
}
#menu ul.sub-menus {
width: 100%;
position: static;
}
#menu ul.sub-menus a {
padding-left: 30px;
}
#menu li {
display: block;
float: none;
width: auto;
}
#menu input,
#menu label {
position: absolute;
top: 0;
left: 0;
display: block;
}
#menu input {
z-index: 4;
}
#menu input:checked + label {
color: white;
}
#menu input:checked + label:before {
content: "\00d7";
}
#menu input:checked ~ ul {
display: block;
}
}
<nav id="menu">
<input
type="checkbox"
id="responsive-menu"
onclick="updatemenu()"
/><label></label>
<ul class="navbar">
<li>Calendar</li>
<li>News</li>
<li>
<a class="dropdown-arrow" href="For Members">For Members</a>
<ul class="sub-menus">
<li>Orientation</li>
<li>Leadership Building</li>
<li>Newsletters</li>
</ul>
</li>
<li>
<a class="dropdown-arrow" href="Resource">Resource</a>
<ul class="sub-menus">
<li>Service Documents</li>
<li>
<a href="District 50 Clubs Directory"
>District 50 Clubs Directory</a
>
</li>
<li>Awards & Contests</li>
<li>D50 Resource</li>
<li>Club Resource</li>
<li>LCIF Resource</li>
</ul>
</li>
</ul>
</nav>
I create a horizontal menu, it contains a submenu.
I have tried using some attributes as:
justify-content:center , align-items: center; and text-align: center;
Here's a snippet with the code:
/*==Reset CSS==*/
* {
margin: 0;
padding: 0;
}
#menu {
justify-content: center;
align-items: center;
text-align: center;
padding: 20px;
}
/*==Style cho menu===*/
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
float: left;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size:16px;
}
#menu a:hover {
color: #444;
}
header {
background: #fff;
width: 100%;
height: 85px;
position: fixed;
top: 0;
left: 0;
border-bottom: 1px solid #4C9CF1;
z-index: 100;
}
/*==Dropdown Menu==*/
.sub-menu {
display: none;
position: absolute;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<header >
<div id="menu">
<ul>
<li>Trang chủ</li>
<li>Diễn đàn</li>
<li>Tin tức
<ul class="sub-menu">
<li>WordPress</li>
<li><a href="https://thachpham.com/category/seo" data-wpel-link="internal">SEO</a></li>
<li>Hosting</li>
</ul>
</li>
<li>Hỏi đáp</li>
<li>Liên hệ</li>
</ul>
</div>
</header>
But menu still can't align to center.
How can I center a horizontal menu?
Thanks all.
Add display: flex; on #menu
The properties(justify-content: center;) that you used work with display flex only. Also you wont need float then.
* {
margin: 0;
padding: 0;
}
#menu {
display: flex;
justify-content: center;
padding: 20px;
}
/*==Style cho menu===*/
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size:16px;
}
#menu a:hover {
color: #444;
}
header {
background: #fff;
width: 100%;
height: 85px;
position: fixed;
top: 0;
left: 0;
border-bottom: 1px solid #4C9CF1;
z-index: 100;
}
/*==Dropdown Menu==*/
.sub-menu {
display: none;
position: absolute;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<header >
<div id="menu">
<ul>
<li>Trang chủ</li>
<li>Diễn đàn</li>
<li>Tin tức
<ul class="sub-menu">
<li>WordPress</li>
<li><a href="https://thachpham.com/category/seo" data-wpel-link="internal">SEO</a></li>
<li>Hosting</li>
</ul>
</li>
<li>Hỏi đáp</li>
<li>Liên hệ</li>
</ul>
</div>
</header>
Add display flex to #menu
#menu {
display:flex;
justify-content: center;
align-items: center;
text-align: center;
padding: 20px;
}
Just remove float:left from #menu li
/*==Reset CSS==*/
* {
margin: 0;
padding: 0;
}
#menu {
justify-content: center;
align-items: center;
text-align: center;
padding: 20px;
}
/*==Style cho menu===*/
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size:16px;
}
#menu a:hover {
color: #444;
}
header {
background: #fff;
width: 100%;
height: 85px;
position: fixed;
top: 0;
left: 0;
border-bottom: 1px solid #4C9CF1;
z-index: 100;
}
/*==Dropdown Menu==*/
.sub-menu {
display: none;
position: absolute;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<header >
<div id="menu">
<ul>
<li>Trang chủ</li>
<li>Diễn đàn</li>
<li>Tin tức
<ul class="sub-menu">
<li>WordPress</li>
<li><a href="https://thachpham.com/category/seo" data-wpel-link="internal">SEO</a></li>
<li>Hosting</li>
</ul>
</li>
<li>Hỏi đáp</li>
<li>Liên hệ</li>
</ul>
</div>
</header>
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>
This question already has answers here:
My inline-block elements are not lining up properly
(5 answers)
Closed 6 years ago.
I'm a CSS starter and my pure CSS dropdown isn't working.
Example in this jsfiddle: https://jsfiddle.net/uevewfsy/
I've been looking around on internet and trying to fix it myself but I have tried countless things and it still isn't fixed. Having a float: left; on the primary ul seems to fix it, but then my nav isn't centered anymore.
Hope someone can help me so I can go further with programming again ;)
* {
padding: 0;
margin: 0;
border: 0;
}
html, body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
margin: 0 20px 0 20px;
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px 0 15px;
}
.nav li:hover > ul {
display: block;
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
#media only screen and (max-device-width: 480px){
}
<body>
<div class="nav">
<ul>
<li>
PAGE
<ul>
<li>DROPDOWN</li>
<li>DROPDOWN</li>
</ul>
</li>
<li>PAGE</li>
<li>PAGE</li>
<li>PAGE</li>
</ul>
</div>
</body>
you are just missing vertical-align:top in your nav > ul > li,
you can add position:relative/top to your li/ul as well.
* {
padding: 0;
margin: 0;
border: 0;
}
html,
body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
padding: 0 20px;
vertical-align: top
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px;
position:relative
}
.nav li:hover > ul {
display: block;
position:absolute;
top:100%;
width:30%
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
#media only screen and (max-device-width: 480px) {}
<body>
<div class="nav">
<ul>
<li>
PAGE
<ul>
<li>DROPDOWN
</li>
<li>DROPDOWN
</li>
</ul>
</li>
<li>PAGE
</li>
<li>PAGE
</li>
<li>PAGE
</li>
</ul>
</div>
</body>
So, on a page I'm making, I have a menu bar and a black box that changes color on hover. When I try to open the menu, and move the mouse over the black box (still in the boundaries of the dropdown), the drop down disappears and the black box :hover selector is called.
Basically, :hover is being detected when it's behind an element. I tried searching, but the only results I'm getting are how to force it to detect behind an element. I want to prevent that.
http://jsfiddle.net/yFD2E/13/
Important HTML:
<div class="menuBar">
<ul>
<li>Home
<ul>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
</ul>
</li>
<li>Home 2
<ul>
<li>Item 1</li>
</ul>
</li>
</ul>
</div> <!--End menu bar-->
<div class="center">
<div class="buttonGroup">
<div class="buttonMain">
<p class="hover">Plot</p>
<p class="show title">Plot</p>
<a class="show inFront" href="#">Test</a>
</div>
</div>
</div>
menubar.css:
.menuBar {
height: 50px;
width: auto;
background-color: #E9523F;
border-radius: 5px;
box-shadow: 0px 0px 10px #DA281F;
z-index: 10000;
}
.menuBar ul {
padding: 0;
list-style-type: none;
positoin: relative;
}
.menuBar ul li {
float: left;
width: 80px;
height: 50px;
display: inline-block;
text-align: center;
border-radius: 5px;
}
.menuBar ul ul {
visibility: hidden;
position: absolute;
width: 80px;
border-radius: 0pxx 5px;
}
.menuBar ul ul li{
width: 80px;
height: 50px;
float: none;
position: relative;
}
.menuBar ul li a {
text-decoration: none;
color: #FAE12D;
text-align: center;
width: 80px;
line-height: 50px;
margin: 0;
padding: 0;
font-size: 17pt;
height: 50px;
display: block;
font-weight: 800;
}
.menuBar ul li:hover ul {
background-color: #DA281F;
box-shadow: 0px 0px 10px #DA281F;
}
.menuBar ul li:hover {
background-color: #DA281F;
box-shadow: 0px 0px 10px #DA281F;
}
.menuBar ul li:hover ul {
visibility: visible;
}
.menuBar ul li li:hover {
background-color: #FE3845;
}
buttons.css
.buttonMain {
height: 400px;
width: 400px;
background-color: #000;
float: left;
margin-left: 20px;
margin-right: 20px;
line-height: 400px;
}
.buttonGroup {
height: 500px;
}
.buttonMain .hover {
position: absolute;
width: inherit;
height: inherit;
line-height: inherit;
margin: 0;
padding: 0;
text-align: center;
overflow: show;
color: #FFF;
}
.buttonMain .show {
position: absolute;
width: inherit;
height: inherit;
line-height: inherit;
margin: 0;
padding: 0;
text-align: center;
overflow: show;
color: #FFF;
background-color: #AAF;
opacity: 0;
}
.buttonMain:hover .show {
opacity: 1;
}
.buttonMain:hover .hover {
opacity: 0;
}
.inFront {
background-color: transparent !important;
}
.title {
line-height: normal !important;
text-decoration: underline;
font-size: 30pt;
font-weight: 600;
}
z-index will do the trick like following
.menuBar ul {
padding: 0;
list-style-type: none;
position: relative; // Just changed the spelling of position
z-index:1000; // Added
}
http://jsfiddle.net/peqh662a/