How can align center a horizal menu? - css

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>

Related

CSS menu aligns to left but when floating to right, the order of menu items are reversed

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>

Responsive CSS submenu

I've made this navigation with CSS and now I'm trying to make it responsive using media queries, but I can't get the submenus to show properly. In responsive mode, I'd like to display the full menu with all links neatly underneath each other in one box. Would really appreciate some help!
https://jsfiddle.net/4L8ghza0/1/
HTML:
<header>
<div class="nav">
<ul>
<li>Start</li>
<li>Submenu1 <span class="arrow">▼</span>
<ul>
<li>link1</li>
<li>link2</li>
<li>link3</li>
</ul>
</li>
<li>Service</li>
<li>Events</li>
<li>Submenu2 <span class="arrow">▼</span>
<ul>
<li>link4</li>
<li>link5</li>
<li>link6</li>
</ul>
</li>
</ul>
</div>
</header>
CSS:
header {
top: 0px;
background-color: #EFE7D2;
position: fixed !important;
width: 100%;
height: 125px;
z-index: 10;
box-shadow: 0 2px 2px 0 rgba(0,0,0,0.12), 0 2px 10px 0 rgba(0,0,0,0.12);
}
.nav {
float: right;
padding: 40px 80px 0 0;
}
ul {
list-style-type: none;
}
ul li {
font-family: Arial, sans-serif;
font-size: 95%;
text-transform: uppercase;
display: inline-block;
position: relative;
float: left;
margin: 5px;
}
ul li a {
padding: 8px 10px;
display: block;
text-decoration: none;
color: #000000;
}
ul li:hover{
background: #CCB18E;
}
.nav .arrow {
font-size: 70%;
line-height: 0%;
}
ul li ul {
display: none;
position: absolute;
width: 210%;
padding: 0;
}
ul li ul li {
display: block;
text-decoration: none;
background: #CCB18E;
padding: 0px 10px;
margin: 0;
width: 100%;
}
ul li ul li:hover {
display: block;
background: #DAC7AD;
text-decoration: none;
padding: 0px 10px;
margin: 0;
width: 100%;
}
ul li:hover ul{
display:block;
visibility:visible;
}
ul ul li:hover li{
display:block;
}
.current {
background:#CCB18E;
color: #000000;
}
#menu-icon {
display: hidden;
width: 40px;
height: 40px;
background: url(https://cdn0.iconfinder.com/data/icons/social-messaging-productivity-4/128/menu-2-512.png) center;
}
a:hover#menu-icon {
border-radius: 4px 4px 0 0;
}
#media screen and (max-width: 1080px){
#menu-icon {
display: inline-block;
}
ul li ul li a {
display: block;
}
ul, ul:active {
display: none;
z-index: 1000;
position: absolute;
padding: 10px;
background: #EFE7D2;
right: 100px;
top: 60px;
width: 25%;
border: 1px #5F7B65 solid;
}
.nav:hover ul {
display: block;
}
ul li:hover ul li ul li {
display: none;
}
}
#JD26 I find it easier using flex-box. You can set .nav {display: flex; flex-direction:column;} in your media query. This should get you started. Or with block display: .nav {display: block}.

Background color does not cover entire <a>-element when hovered

I am currently coding a navigation bar where when the links are hovered, the background-color of the -element changes. Currently, the background-color in the navigation bar's dropdown menu does not cover the entire "box".
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: black;
}
ul li {
display: inline-block;
position: relative;
background-color: black;
}
ul li a {
display: block;
color: white;
text-decoration: none;
text-align: center;
margin: -left:0;
padding: 15px 25px;
-webkit-transition-duration: 0.4s;
transition-duration: 0.4s;
}
ul li a:hover {
background-color: #333;
}
ul li ul#dropdowncontent {
display: inline-block;
position: absolute;
text-align: center;
min-width: 100%;
font-size: 70%;
z-index: 999;
width: 90px;
}
<ul>
<li>Produkter
<ul id="dropdowncontent">
<li>Moderkort</li>
<li>Processorer</li>
<li>Hårddiskar</li>
<li>Grafikkort</li>
</ul>
</li>
<li>Butiker</li>
<li>Kontakt</li>
<li>Vanliga frågor</li>
</ul>
You can also take a look at the code in this Jsfiddle: https://jsfiddle.net/fdvcmnx6/
Set your dropdown list items to display: block.
#dropdowncontent li {
display: block;
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: black;
}
ul li {
display: inline-block;
position: relative;
background-color: black;
}
ul li a {
display: block;
color: white;
text-decoration: none;
text-align: center;
margin: -left:0;
padding: 15px 25px;
-webkit-transition-duration: 0.4s;
transition-duration: 0.4s;
}
ul li a:hover {
background-color: #333;
}
ul li ul#dropdowncontent {
display: inline-block;
position: absolute;
text-align: center;
min-width: 100%;
font-size: 70%;
z-index: 999;
width: 90px;
}
/* Additional */
#dropdowncontent li {
display: block;
}
<ul>
<li>Produkter
<ul id="dropdowncontent">
<li>Moderkort</li>
<li>Processorer</li>
<li>Hårddiskar</li>
<li>Grafikkort</li>
</ul>
</li>
<li>Butiker</li>
<li>Kontakt</li>
<li>Vanliga frågor</li>
</ul>
set your dropdown list width:100%
#dropdowncontent li {
display: block;
}

Submenu disappears in Flexbox

When I try to add a site navigation into a flexbox layout the submenus become not accessible anymore. They disappear when the mouse leaves the parent list element.
The final goal is to make the navigation fixed by using flexbox. If the body- and header-tag are left out the navigation works like intended. Any ideas on this?
* {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
}
header {
flex: 0 0 auto;
background: yellow;
}
.site-navigation {
font-family: 'Open Sans', 'Helvetica', 'Arial', sans-serif;
font-weight: 400;
color: #666;
}
nav ul {
background: none;
display: flex;
flex-direction: column;
}
nav ul li {
list-style-type: none;
background: none;
}
nav ul li:hover {
background: none;
}
nav ul li a {
padding: 0.8rem 1rem;
display: block;
text-decoration: none;
color: rgba(100, 100, 100, 0.8);
background: none;
text-transform: uppercase;
font-size: 12px;
}
.has-children:hover>a {
border: 1px solid #e5e5e5;
border-bottom: none;
background: #fff;
margin-left: -1px;
margin-right: -1px;
margin-top: -1px;
color: #000;
}
.has-children:hover>ul {
border: 1px solid #e5e5e5;
position: relative;
z-index: -1;
}
.has-children:hover>ul>li {
padding: 15px 15px;
}
.has-children>ul>li>a {
text-transform: none;
color: #666;
border-bottom: 1px solid #E5E5E5;
padding: 0;
padding-bottom: 15px;
}
.has-children>ul>li:hover>a {
border-bottom: 2px solid #d00;
color: #000;
padding-bottom: 14px;
}
#media only screen and (min-width:680px) {
nav ul {
flex-direction: row;
}
nav ul li {
position: relative;
flex: 0 0 auto;
text-align: left;
}
nav ul li:hover ul {
display: flex;
flex-direction: column;
}
.has-children ul {
display: none;
position: absolute;
top: 100%;
}
.has-children:hover ul {
display: block;
position: absolute;
top: calc( 100% - 1px);
width: 150%;
}
}
<header class="site-header">
<nav class="site-navigation">
<ul class="site-navigation__list">
<li class="site-navigation__item">Item 1
</li>
<li class="site-navigation__item has-children">Item 2<span class="arrow arrow-down"></span>
<ul class="site-navigation__sub-list">
<li class="site-navigation__sub-item">Subitem 1</li>
<li class="site-navigation__sub-item">Subitem 2</li>
</ul>
</li>
</ul>
</nav>
</header>
<div class="content"><p>Content goes here!</p></div>
It seems to be a matter of z-index and background.
You may use position:relative to reset z-index and add a background to submenu to hide overlapped content.
CSS update that could be done:
nav ul {
position:relative;
z-index:1
}
nav ul li:hover > ul{
background: white;
}
* {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
}
header {
flex: 0 0 auto;
background: yellow;
}
.site-navigation {
font-family: 'Open Sans', 'Helvetica', 'Arial', sans-serif;
font-weight: 400;
color: #666;
}
nav ul {
background: none;
display: flex;
flex-direction: column;
}
nav ul {
position:relative;
z-index:1
}
nav ul li:hover > ul{
background: white;
}
nav ul li ,nav ul li:hover{
list-style-type: none;
background: none;
}
nav ul li a {
padding: 0.8rem 1rem;
display: block;
text-decoration: none;
color: rgba(100, 100, 100, 0.8);
background: none;
text-transform: uppercase;
font-size: 12px;
}
.has-children:hover>a {
border: 1px solid #e5e5e5;
border-bottom: none;
background: #fff;
margin-left: -1px;
margin-right: -1px;
margin-top: -1px;
color: #000;
}
.has-children:hover>ul {
border: 1px solid #e5e5e5;
position: relative;
z-index: -1;
}
.has-children:hover>ul>li {
padding: 15px 15px;
}
.has-children>ul>li>a {
text-transform: none;
color: #666;
border-bottom: 1px solid #E5E5E5;
padding: 0;
padding-bottom: 15px;
}
.has-children>ul>li:hover>a {
border-bottom: 2px solid #d00;
color: #000;
padding-bottom: 14px;
}
#media only screen and (min-width:680px) {
nav ul {
flex-direction: row;
}
nav ul li {
position: relative;
flex: 0 0 auto;
text-align: left;
}
nav ul li:hover ul {
display: flex;
flex-direction: column;
}
.has-children ul {
display: none;
position: absolute;
top: 100%;
}
.has-children:hover ul {
display: block;
position: absolute;
top: calc( 100% - 1px);
width: 150%;
}
}
<header class="site-header">
<nav class="site-navigation">
<ul class="site-navigation__list">
<li class="site-navigation__item">Item 1
</li>
<li class="site-navigation__item has-children">Item 2<span class="arrow arrow-down"></span>
<ul class="site-navigation__sub-list">
<li class="site-navigation__sub-item">Subitem 1</li>
<li class="site-navigation__sub-item">Subitem 2</li>
</ul>
</li>
</ul>
</nav>
</header>
<div class="content"><p>Content goes here!</p></div>
Another update maybe to show submenu when parent is hovered ?
.has-children>ul {
display:none;
}
.has-children:hover>ul {
display:block;
}
https://codepen.io/anon/pen/MvKqqE

CSS Submenu Li's overlapping

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;
}

Resources