I have been checking on other questions related to mine, and most of the time "text-align:center" is suggested. However, even if I implement it to my code, the navbar still doesn't center... Here's my code:
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
float:left;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
Thanks for any suggestions in advance!
remove float:left on your navli class ang set it to display:inline-block and add text-align:center on your #nav, and to center your #navigation add margin:0 auto;
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
margin : 0 auto;
}
#nav {
width: auto;
text-align : center;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
/* float:left;*/
display : inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
You've used float:left on the li, which aligns them to left. I've removed it and added text-align:center to the ul
Is this what you're looking for?
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align:center;
}
.navli {
display:inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
Related
My Dropdown menu opens up but it goes down under behind the screen like unless I increase the navbar height it isn't visible.
here is my code.
I want it to come on top of everything so the user can actually see it.
It goes underneath the nav section, so is not visible.
.sub-menu {
display: none;
margin-top: 0 !important;
}
.navbar-header .navbar-ul .main-menu-items .about-us-menu #about-sub-menu {
z-index: 100;
position: relative;
}
.navbar-header .navbar-ul .about-us-menu .sub-menu {
width: 150px;
}
.navbar-header .navbar-ul .about-us-menu .sub-menu ul li a {
padding-top: 10px;
padding-left: 0;
padding-right: 0;
padding-bottom: 0;
}
.sub-menu ul li {
margin: 15px;
width: 120px;
position: relative;
/* padding: 15px; */
}
.navbar-header .navbar-ul .courses-sub-menu:hover .sub-menu ul li a {
width: 150px;
margin: 0 !important;
padding-top: 10px;
padding-left: 0;
padding-right: 0;
padding-bottom: 0;
}
.navbar-header .navbar-ul .about-us-menu .sub-menu {
width: 180px;
}
.navbar-header ul li:hover .sub-menu {
display: block !important;
position: absolute;
background: #333333;
margin-top: 15px;
}
.sub-menu ul:hover {
display: block !important;
}
.navbar-header ul li:hover .sub-menu ul {
display: block;
/* margin: 10px; */
}
.sub-menu ul li:hover {
display: block;
background: #f5f5f7;
color: black !important;
}
.sub-menu ul li a {
color: #f5f5f7;
}
.navbar-header ul li:hover .sub-menu ul li {
display: block !important;
color: black;
width: 150px;
padding: 0 !important;
border-radius: 0;
text-align: center;
}
.sub-menu ul li a:hover {
color: black;
}
<div class="navbar-header" style="padding: 0; margin: 0; ">
<ul class="navbar-ul">
<div class="main-menu-items">
<li>
<a routerLink="/home"><img src="../../../assets/xyz" class="navbar-logo" alt=""></a>
</li>
<!-- logo -->
<!-- <li><a routerLink="/home">Home</a></li> -->
<!-- <li><a routerLink="/aboutus">About Us</a></li> -->
<li class="about-us-menu"><a routerLink="#">About Us</a>
<div class="sub-menu">
<ul id="about-sub-menu">
<li>
Gallery
</li>
<li>
Testimonials
</li>
<li>
Blogs
</li>
</ul>
</div>
</li>
<li class="courses-sub-menu"><a routerLink="/courses">Courses</a>
<div class="sub-menu">
<ul>
<li>
Science
</li>
<li>
Physics
</li>
<li>
Biology
</li>
<li>
Mathematics
</li>
<li>
Chemistry
</li>
<li>
Business Studies
</li>
<li>
Accountancy
</li>
<li>
Economics
</li>
<li>
Psychology
</li>
<li>
SAT/ACT
</li>
</ul>
</div>
</li>
Here you go.
Check out these CSS properties to achieve your desired result.
.main-menu-items {
background: none;
color: Black;
padding: 12px 26px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
min-width: 160px;
}
.sub-menu {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.sub-menu a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.sub-menu a:hover {background-color: #f1f1f1}
.dropdown:hover .sub-menu {
display: block;
position: absolute;
left: 100%;
top: 0;
}
.nav-wrapper {
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="navbar-header">
<div class="nav-wrapper">
<div class="dropdown">
<button class="main-menu-items">About Us</button>
<div class="sub-menu">
Gallery
Testimonials
Blogs
</div>
</div>
</div>
<div class="nav-wrapper">
<div class="dropdown">
<button class="main-menu-items">Courses</button>
<div class="sub-menu">
Science
Physics
Biology
Mathematics
Chemistry
Business Studies
Accountancy
Economics
Psychology
SAT/ACT
</div>
</div>
</div>
</div>
</body>
</html>
I am trying to make a hoverable dropdown menu and the items of the dropdown are overlapped. I don't know how the CSS should be, but I tried to modify each class, but still doesn't work.
I also tried to modify the display of links, but that doesn't work. Here is the code I made:
<style>
#menu
{
margin:0;
font-size: 30px;
border-bottom: 1px solid;
text-align: center;
}
#menu a
{
color:#900;
text-decoration:none;
}
#menu .subitem a{
display: block;
padding: 12px 16px;
z-index: 1;
background-color: #f1f1f1;
border-bottom: 1px solid;
}
#menu a:hover
{
text-decoration:underline;
}
.item{
position: relative;
display: inline-block;
border-right: 0.5px solid;
padding-right: 30px;
padding-left: 30px;
}
.subitem{
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
left: 0;
z-index: 1;
}
#menu .item:hover .subitem{
display: block;
}
</style>
<div id="navWrapper">
<ul id="menu">
<li class="item">Small Things
<ul class="submenu">
<li class="subitem">Gnomes</li>
<li class="subitem">Fairies</li>
<li class="subitem">Elves</li>
<li class="subitem">Leprechauns</li>
</ul>
</li>
<li class="item">Big Things
<ul class="submenu">
<li class="subitem">Loch Ness Monster</li>
<li class="subitem">Ogres</li>
<li class="subitem">Giants</li>
<li class="subitem">Dragons</li>
</ul>
</li>
</ul>
</div>
I want to display properly each item when I hover with the mouse like it is in this
image.
You confused your .submenu with .subitem:
#menu
{
margin:0;
font-size: 30px;
border-bottom: 1px solid;
text-align: center;
}
#menu a
{
color:#900;
text-decoration:none;
}
#menu .subitem a{
display: block;
padding: 12px 16px;
z-index: 1;
background-color: #f1f1f1;
border-bottom: 1px solid;
}
#menu a:hover
{
text-decoration:underline;
}
.item{
position: relative;
display: inline-block;
border-right: 0.5px solid;
padding-right: 30px;
padding-left: 30px;
}
.subitem {
/* display: none; <- get rid of that */
/* position: absolute; <- get rid of that, otherwise all your subitems will be in the top left corner */
background-color: #f1f1f1;
min-width: 160px;
left: 0;
z-index: 1;
}
.submenu {
position: absolute; /* <- put it here */
display: none; /* <- put it here */
list-style-type: none;
left:-40px;
top: 30px;
}
#menu .item:hover .submenu {
display: block;
}
<div id="navWrapper">
<ul id="menu">
<li class="item">Small Things
<ul class="submenu">
<li class="subitem">Gnomes</li>
<li class="subitem">Fairies</li>
<li class="subitem">Elves</li>
<li class="subitem">Leprechauns</li>
</ul>
</li>
<li class="item">Big Things
<ul class="submenu">
<li class="subitem">Loch Ness Monster</li>
<li class="subitem">Ogres</li>
<li class="subitem">Giants</li>
<li class="subitem">Dragons</li>
</ul>
</li>
</ul>
</div>
Hello I am trying to create menu with absolute position CSS rule for each list item. display: inline-block for parent doesn't work. Here's an example of markup.
.menu-items{
}
.main-menu{
position: relative;
list-style:none;
}
.main-menu li {
position: absolute;
float:left;
}
.main-menu li a{
font-size: 12px;
color: rgba(1, 1, 1, 1);
}
.main-menu li a:hover{
font-size: 21px;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can use transform:scal(x); , it won't disturb the layout.
.menu-items {} .main-menu {
list-style: none;
}
.main-menu li {
display: inline-block;/*modified*/
padding-left: 10px;/* moved here*/
}
.main-menu li a {
font-size: 12px;
color: rgba(1, 1, 1, 1);
display: inline-block;/*added*/
background:white;/*added*/
transition: 0.25s;/*added*/
}
.main-menu li a:hover {
transform: scale(1.75);/*modified*/
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index
</li>
<li>shop
</li>
<li>about
</li>
<li>projects
</li>
<li>contacts
</li>
</ul>
</div>
</div>
.menu-items {}
.main-menu {
position: relative;
list-style: none;
}
.main-menu li {
position: relative;
float: left;
width: 100px;
}
.main-menu li a {
font-size: 12px;
padding-left: 10px;
color: rgba(1, 1, 1, 1);
}
.main-menu li a:hover {
font-size: 21px;
overflow: hidden;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can use position:absolute, too, but on a elements:
.menu-items{
}
.main-menu{
list-style:none;
}
.main-menu li {
position:relative;
display:inline-block;
padding:30px;
}
.main-menu a {
font-size: 12px;
color: rgba(1, 1, 1, 1);
position:absolute;
width:100%;
height:100%;
left:0;
top:0;
}
.main-menu a:hover{
font-size: 21px;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can hardcode a "left: XXXpx" for each li but then you will have issues based on screen sizes. If you really want that for some reason though let me know and I will send an example of that.
Another option is to use a flex box (assuming you do not need this to work with older versions of IE) like below. The spacing between items will change, but it will be responsive to the size of the text and screen , versus just pushing everything to the right.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
padding: 5px;
margin-top: 10px;
color: white;
text-align: center;
font-size: 12px;
color: rgba(1, 1, 1, 1);
}
.flex-item:hover {
font-size: 21px;
}
<div class="row">
<div class="menu-items">
<ul class="flex-container">
<li class="flex-item">index</li>
<li class="flex-item">shop</li>
<li class="flex-item">about</li>
<li class="flex-item">projects</li>
<li class="flex-item">contacts</li>
</ul>
</div>
</div>
I want to make a vertical dropdown menu with the following structure(for each "button" a different "dropdown" div).The idea is when I hover "button", the "dropdown" to appear over the "button" with the same size ( the "dropdown" shrinks to the "button" size with equal heights "subnav1" ) or to appear left to the "dropdown" ( again with equal heights "subnav1" ). The problem, however , is that I can't make the "dropdown" display:blocks on "button" hover and ,in the second case, the "button" to be hovered while selecting "subnav1" element.
HTML:
<div class="nav">
<ul class="nav1">
<li class="button"><div class="subnav">BUTTON1</div></li>
<li class="button"><div class="subnav">BUTTON2</div></li>
<li class="button"><div class="subnav">BUTTON3</div><li>
<li class="button"><div class="subnav">BUTTON4</div></li>
<li class="button"><div class="subnav">BUTTON5</div></li>
</ul></div>
<div class="dropdown">
SUBBUTTON1
SUBBUTTON1
SUBBUTTON1
</div>
CSS:
.nav {
width: 180px;
padding: 1vm;
float:left;
height:450px;
position:relative;
text-align:center;
background-color:#a56d3b;
}
ul{
list-style:none;
margin: 0;
padding: 0;
text-align:right;
}
.nav a{
text-decoration:none;
}
.subnav{
font-weight:bold;
text-align:center;
color:#FCFBE3;
font-family: 'PT Serif', serif;
font-size:1.3em;
display:block;
}
.button{
border-bottom:1PX DOTTED #FFECBA;
border-right:1PX DOTTED #FFECBA;
height:90px;
width:100%;
padding:2em 0 0 0 ;
cursor:pointer;
}
.button:last-child{
border-bottom:0px;
}
.button:hover{
background-color:rgba(0,0,0,0.5);
}
.dropdown{
height:89px;
float:left;
width:auto;
min-width:100px;
text-align:center;
margin:0 180px;
position:absolute;
cursor:pointer;
border:1px solid black;
}
.subnav1{
height:30px;
width:180px;
padding:5px 0;
color:black;
font-family: 'PT Serif', serif;
font-size:1em;
color:white;
text-decoration:none;
background-color:#D8A06E;
DISPLAY:BLOCK;
}
.subnav1:hover{
background-color:rgba(0,0,0,0.5);
}
.nav.nav1 .button:hover .dropdown{
display:block;
}
I think this is how you want it to be. Let me know if this is not the result you are looking for
.dropbtn {
background-color: #E61094;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.nav {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 115px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a{
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #9E1192
}
.nav:hover .dropdown-content {
display: block;
}
.nav:hover .dropbtn {
background-color: #F576EA;
}
<div class="nav">
<button class="dropbtn">Dropdown 1</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 2</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 3</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 4</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
<div class="nav">
<butoon class="dropbtn">Dropdown 5</button>
<div class="dropdown-content">
Subbutton 1
Subbutton 2
Subbutton 3
</div>
</div>
you are using absolutely wrong markup of the HTML navigation, correct and valid structure is:
<nav class="nav">
<ul>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
<li>
1st Level
<ul>
<li>2nd Level</li>
<li>2nd Level</li>
<li>2nd Level</li>
</ul>
</li>
</ul>
</nav>
and then it is easy as a pie, just styling:
.nav {
display: block;
font-family: sans-serif;
}
.nav ul {
padding: 0;
list-style: none;
}
.nav ul li {
display: inline-block;
position: relative;
white-space: nowrap;
}
.nav ul li a {
display: block;
padding: 8px 16px;
text-decoration: none;
color: #000000;
}
.nav ul li ul {
display: none;
position: absolute;
top: 100%;
left: 0;
border: 1px solid #000000;
}
.nav ul li ul li {
display: block;
}
.nav ul li:hover ul {
display: block;
}
See this fiddle https://jsfiddle.net/vtt0qnpt/
Enjoy
I'm trying to create a basic drop down menu. I'm having issues with the alignment. How can I get sub menu items to be directly underneath one another? For example, I am trying to get twitter, instagram, and facebook underneath "Social".
Thanks.
https://jsfiddle.net/xalxnder/ostaqgyk/
HTML
<body>
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
</body>
CSS
body {
background: #000000;
color: white;
}
.main-nav {
float: left;
}
.main-nav li {
font-size: 10px;
display: inline;
padding-right: 10px;
}
.main-nav .sub {
color: pink;
float: none;
z-index: -200;
}
//** .sub{display: none;
}
.dropdown:hover > .sub {
display: block;
position: absolute;
}
Here's the basics of it.
JSFiddle
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: black;
}
ul {
list-style-type: none;
color: white;
}
.main-nav >li {
display: inline-block;
position: relative;
padding-right: 10px;
}
.sub {
position: absolute;
top: 100%;
left: 0;
color: pink;
/* display:none */ /* disabled for demo */
}
.dropdown:hover .sub {
display:block;
}
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
Since block-level elements take up their own line, this rule should hold you:
.sub li {
display: block;
}
Add to .main-nav .sub:
position: absolute;
padding: 0;
body {
background: #000000;
color: white;
}
.main-nav {
float: left;
}
.main-nav li {
font-size: 10px;
display: inline-block;
padding-right: 10px;
}
.main-nav .sub {
position: absolute;
padding: 0;
color: pink;
float: none;
z-index: -200;
}
.sub li {
display: block;
}
<body>
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
</body>