In my nav bar i have 4 <li> items which on :hover i would like a drop-down menu to appear under the <li> element which has 3 different links.
The problem is when :hover is active the drop-down displays but then the 3 other <li> elements move underneath the nav bar when they should not move at all.
I have tried using position:absolute on nav ul li elements but that just squishes them all on top of each other.
nav bar
nav ul li:hover
nav ul li {position:absolute;}
html
<nav>
<ul>
<li>
<button>Home</button>
<div class="dropdown-content">
Link A
Link B
Link C
</div>
</li>
<li>
<button>Profile</button>
<div class="dropdown-content">
Link A
Link B
Link C
</div>
</li>
...
..
.
</ul>
</nav>
css
nav {
border-bottom: 0.1rem rgb(228, 220, 220) solid;
position:fixed;
width: 100%;
z-index: 5;
height: 3rem;
background-color: white;
top:0;
}
nav ul {
display: flex;
flex-direction: row;
justify-content: space-around;
width: 50%;
margin-left: 25%;
position: absolute;
}
nav ul li {
/* position:absolute; */
list-style: none;
display: flex;
justify-content: center;
flex-direction: column;
}
nav ul li button {
background-color: transparent;
border: none;
font-size: medium;
}
.dropdown-content {
display: none;
justify-content: center;
flex-direction: column;
text-align: center;
border: 0.1rem rgb(228, 220, 220) solid;
border-top: none;
margin-top: 0.8rem;
width: 5rem;
}
.dropdown-content a {
background-color: white;
padding: 0.4rem 0 0.4rem;
}
.dropdown-content a:hover {
background-color: whitesmoke;
}
nav ul li:hover .dropdown-content {
display: flex;
}
The absolute position should be added to the dropdown-content div. You can find an example below :
nav {
border-bottom: 0.1rem rgb(228, 220, 220) solid;
position:fixed;
width: 100%;
z-index: 5;
height: 3rem;
background-color: white;
top:0;
}
nav ul {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: flex-start;
width: 50%;
margin-left: 25%;
position: absolute;
height: 100%;
margin: 0;
}
nav ul li {
list-style: none;
display: flex;
flex-direction: column;
height: 100%;
position: relative;
}
nav ul li button {
background-color: transparent;
border: none;
font-size: medium;
height: 100%;
}
.dropdown-content {
display: none;
justify-content: center;
flex-direction: column;
text-align: center;
border: 0.1rem rgb(228, 220, 220) solid;
border-top: none;
margin-top: 0;
width: 5rem;
position: absolute;
top: calc(100% - 1px);
}
.dropdown-content a {
background-color: white;
padding: 0.4rem 0 0.4rem;
}
.dropdown-content a:hover {
background-color: whitesmoke;
}
nav ul li:hover .dropdown-content {
display: flex;
}
<nav>
<ul>
<li>
<button>Home</button>
<div class="dropdown-content">
Link A
Link B
Link C
</div>
</li>
<li>
<button>Profile</button>
<div class="dropdown-content">
Link A
Link B
Link C
</div>
</li>
</ul>
</nav>
Related
I am working on a project and I'm struggling with one piece. It may be very simple, but I'm clearly over looking something. My objective is to have a menu and sub-menu populate on a hover over a specific image. When you hover over the image you'll see "categories" and to the right see the "sub-menu" to that category. I am able to get the first category to work, but unable to get any of the subsequent categories to change the "sub-menu" to the right. I've included the code below. Any help would be appreciated - I'm struggling to make this work
Menu Example:
This is my styling css:
/* Dropdown Button */
.dropbtn {
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
z-index: 1;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
float: right;
position: absolute;
background-image: url(../images/Framework-and-Navigation_03.png);
min-width: 100px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content2{
display: block;
position: relative;
z-index: 1;
color: black;
padding: 12px 16px;
text-decoration: none;
}
.dropdown-content3{
display: block;
position: relative;
z-index: 1;
color: black;
padding: 12px 16px;
text-decoration: none;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content-sub1 {
color: black;
text-decoration: none;
display: none;
z-index: 1;
}
.dropdown-content-sub2 {
color: black;
text-decoration: none;
display: none;
}
.dropdown-content-sub3 {
color: black;
text-decoration: none;
display: none;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {display: inline-block;}
/* Change the sub menu */
.dropdown-content1:hover .dropdown-content-sub1 {display: block;}
.dropdown-content2:hover .dropdown-content-sub2 {display: inline-block;}
This is my menu/submenu test:
<div class="dropdown">
<center>
<input type="image" class="dropbtn" src="images/menu_07.png"/>
</center>
<div class="dropdown-content">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="158">
<div class="dropdown-content1">Link 1</div>
<div>Link 2</div>
<div>Link 3</div>
</td>
<td class="dropdown-content-sub1" valign="top"><div>
Test
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
See if this solves your problem :)
/* Dropdown Button */
html,
body {
font-family: arial;
padding: 0;
margin: 0;
}
a {
padding: 10px;
text-decoration: none;
display: block;
}
.menu-container {
width: 615px;
height: 200px;
background: #eee url("https://images.unsplash.com/photo-1538098269808-2ace9a4d9680?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2a48defb8057de5b3f79b49b85c173bc&auto=format&fit=crop&w=500&q=60") no-repeat 200px 0;
display: none;
position: absolute;
top: 50px;
left: 0;
box-shadow: 0 0 8px 8px rgba(0, 0, 0, 0.3);
}
ul.menu,
ul.menu ul {
width: 200px;
padding: 0;
margin: 0;
display: none;
flex-direction: column;
}
ul.menu li,
ul.menu ul li {
background: rgba(0, 0, 0, 0.5);
height: 30px;
display: flex;
align-items: center;
}
ul.menu li a,
ul.menu ul li a {
color: white;
margin-bottom: 1px;
}
ul.menu li:hover ul,
ul.menu ul li:hover ul {
display: flex;
}
ul.menu ul {
position: absolute;
margin: 0 0 0 200px;
top: 0;
z-index: 2;
}
/* The container <div> - needed to position the dropdown content */
.nav {
background-color: black;
display: flex;
flex-direction: row;
}
.nav > .nav-item {
height: 30px;
padding: 10px;
margin-right: 2px;
background-color: rgba(255, 255, 255, 0.4);
color: white;
display: flex;
align-items: center;
}
.nav > .nav-item:hover > .menu-container,
.nav > .nav-item:hover > .menu-container > .menu {
display: flex;
}
<div class="nav">
<div class="nav-item" href="">Nav item 1
<div class="menu-container">
<ul class="menu">
<li>Menu item 1 >
<ul>
<li>Sub menu 1 item 1</li>
<li>Sub menu 2 item 2</li>
</ul>
</li>
<li>Menu item 2</li>
<li>Menu item 3 >
<ul>
<li>Sub menu 2 item 1</li>
<li>Sub menu 2 item 2</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="nav-item" href="">Nav item 2</div>
</div>
I'm trying to mimic this website (but with my own images): https://www.w3schools.com/w3css/tryw3css_templates_band.htm#
Every time I click on more to try to go down to "merchandise", "Extras" or "media" it closes (unless I do it very quickly). How can I fix this?
I know there's probably a better way of doing the drop-down menu in HTML/CSS but this is the only way I know how.
nav {
background-color: black;
height: 3rem;
widows: 100%;
}
.navbar {
height: 100%;
display: flex;
flex-flow: row wrap;
border: 1px solid hotpink;
justify-content: flex-start;
align-items: center;
}
.navbar li {
border: 2px solid green;
list-style-type: none;
height: 100%;
}
.navbar li a {
border: 1px solid orange;
color: white;
text-decoration: none;
padding: 0 20px;
text-transform: uppercase;
display: block;
height: 100%;
line-height: 2rem;
}
.navbar li a:hover {
background-color: #CCCCCC;
color: black;
display: block;
height: 100%;
}
section#header-image {
background-image: url('images/sky.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
width: 100%;
height: 60vh;
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
align-items: center;
color: white;
}
.moreArrow{
height: 6px;
width: 8px;
margin-left: 5px;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
ul li:hover ul li a {
color: white;
background-color: black;
text-decoration: none;
padding: 0 20px;
height: 100%;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>more<img src="images/arrow.png" alt="x" class="moreArrow">
<ul>
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</li>
</ul>
</nav> <!--end of nav-->
<section id="header-image">
<div class="header-text">
<h2>London</h2>
<p>We killing it up in 'ere </p>
</div>
</section>
</header>
So, i made a lil change on your HTML, in order to persist ':hover' state you need to put anything that it has to stay visible inside the hovered item, in this case, a 'span' tag (i did changed from an 'a' tag because you cant nest 'a' tags)
The css is pretty simple from this point, you hide the sub menu and display on hover, anything that its inside the span tag belongs to ':hover' state as well
body {
margin: 0px;
}
nav {
width: 100vw;
height: auto;
border: 1px solid red;
}
nav ul.navbar {
display: flex;
margin: 0;
padding: 0;
}
nav ul.navbar > li {
width: auto;
display: inline-block;
vertical-align: top;
}
nav ul.navbar > li a {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
padding: 10px 30px;
box-sizing: border-box;
width: auto;
}
nav ul.navbar > li span {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
position: relative;
padding: 10px 30px;
}
nav ul.navbar > li span:hover ul {
display: inline-block;
}
nav ul.navbar > li span ul {
display: none;
margin: 0;
padding: 0;
position: absolute;
top: 38px;
left: 0px;
width: auto;
border: 1px solid blue;
}
nav ul.navbar > li span ul li {
display: inline-block;
list-style: none;
width: 100%;
}
nav ul.navbar > li span ul li a {
width: 100%;
display: inline-block;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>
<span class="more">more
<ul rel=":D">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</span>
</li>
</ul>
</nav> <!--end of nav-->
</header>
Hope this helps
So I am trying to create a navbar for a school project, however I cannot get all the buttons to be evenly spaced across the whole navbar. I have been trying to do this for a while now, and the navbar code is all messy which doesn't help my situation. Any solutons?
The HTML code is:
<ul>
<li> <img src="Images/homepage/logo.png" width="20" height="20" href="#home"> </li>
<div class="other";>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
The CSS code is:
ul {
list-style-type: none;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
z-index: 1;
top: 0;
left: 0;
width: 100%;
text-align: center;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
z-index: 1;
width: 100px;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
Please help, this has been confusing me for a while now and I would rather not write up a new navbar code unless absolutely necessary!
Use Flex
ul{ display:flex;}
li{flex:1;}
* {
margin: 0;
}
ul {
display: flex;
flex-direction: row;
list-style-type: none;
padding: 0;
background-color: #333;
position: fixed;
z-index: 1;
height: 50px;
line-height: 50px;
width: 100%;
}
li {
flex: 1;
}
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
z-index: 1;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover {
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color: white;
}
img {
margin: -15px;
}
<ul>
<li>
<img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" width="40" height="40px" href="#home">
</li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
You can only nest li elements within the ul element. Nesting div inside the ul is not semantically correct. Also, the incorrectly nested div element within ul has a semicolon after its class name: this is not legal.
The flex box module is the best option to use in order to achieve what you are after. You can read more about it here.
One way to get the results you are after is as follows:
ul,
li,
a,
img {
box-sizing: border-box;
padding: 0px;
margin: 0px;
border: 0px;
height: 50px; /* Adjust this value to whatever height you want your nav to be */
}
ul {
position: fixed;
top: 0px;
left: 0px;
right: 0px;
list-style-type: none;
background-color: #333;
text-align: center;
display: flex;
justify-content: space-around;
align-items: center;
}
ul li {
flex: 0 1 100%;
}
ul li a{
display: block;
color: white;
text-decoration: none;
line-height: 50px; /* This value should be the same as your nav's height is in order for text to stay centered. */
padding: 0px 1em;
}
ul li a img {
height: 100%;
width: auto;
padding: 5px; /*You can adjust this value to whatever you want. */
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
<ul>
<li> <img src="https://www.w3.org/html/logo/downloads/HTML5_1Color_White.png" href="#home"> </li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</ul>
I hope this is helpful.
I want to create a dropdown nav menu with a modal like box appearing on hover.
Here in my example, Products heading needs to open 4 columns of subheadings that align themselves into a bootstrap like grid.
I am close to the result but I am facing a couple of hurdles: my hover does not seem to work. Also, my subheading appears only within the perimeter of my navbar - whereas I want it to appear a little below the navbar, with some padding.
I looked at these 2 examples but they did not help me:
stackoverflow reference 1
stackoverflow reference 2
Please find the code and guide me in the right direction:
.topnav {
background-color: white;
overflow: hidden;
}
.topnav a {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
color: grey;
}
.nav {
list-style: none;
display: -webkit-flex;
-webkit-flex-direction: row;
-webkit-justify-content: space-between;
-webkit-flex-wrap: wrap;
}
.nav li:first-child {
margin-right: auto;
}
.nav li {
position: relative;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
position: absolute;
background-color: #f9f9f9;
min-width: 560px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 20;
border: 1px solid white;
padding: 80px;
height: 220px;
}
.dropdown-content ul {
display: block;
}
.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
.noshow {
display: none;
}
.dropdown-content:hover .noshow {
display: block
}
.subheading {
font-weight: 700;
}
<nav class="topnav">
<ul class="nav">
<li><a class="active" href="#title"> Title</a></li>
<li>
Products
<div class="dropdown-content arrow-up noshow">
<ul class="column large-3 each-column">
<li class="subheading">subheading</li>
<li>
subheading1
</li>
<li>
subheading2
</li>
<li>
subheading3
</li>
</ul>
</div>
</li>
<li>link2</li>
<li>link3</li>
<li><img src="http://lorempixel.com/30/30/" width="30" height="30" alt="User Account Icon"></li>
</ul>
</nav>
<!DOCTYPE html>
<html>
<head>
<style>
.left_menu {
float: left;
width: 50%;
}
.right_menu {
float: left;
width: 50%;
}
.right_menu ul {
float: right;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: grey;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
/* background-color: red;*/
/*color: white;*/
}
li.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;
}
</style>
</head>
<body>
<div class="left_menu">
<ul>
<li>Title</li>
</ul>
</div>
<div class="right_menu" style="float: left; width: 50%">
<ul>
<li class="dropdown">
Product
<div class="dropdown-content">
I am new to CSS and I am making a navigation bar. Currently, my navigation bar is situated on the left and I would like to move the whole bar to the middle. How can I do that?
Below are the codes. Thanks in advance!
/* Navigation bar */
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<!--Navigation bar-->
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li>Promotion.</li>
<!--drop down menu-->
<li class="dropdown">Hot Products.
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li>Cold Products.</li>
<li>Snacks.</li>
<li>About Us.</li>
<li>Contact Us.</li>
</ul>
Update your #navigation_bar with
#navigation_bar {
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
Here is the working Demo
#navigation_bar {
list-style-type: none;
margin: 0;
position: fixed;
background-color: #333;
top: 0;
Left: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.active {
background-color: #4CAF50;
}
<ul id="navigation_bar">
<li><a class="active" href="navigation_bar/home.html">Home.</a></li>
<li>Promotion.</li>
<!--drop down menu-->
<li class="dropdown">Hot Products.
<div class="dropdown-content">
<a herf="navigation_bar/sub_menu/sandwiches.html">Sandwiches</a>
<a herf="navigation_bar/sub_menu/burger.html">Burger</a>
<a herf="navigation_bar/sub_menu/rice.html">Rice</a>
<a herf="navigation_bar/sub_menu/noodles.html">Noddles</a>
</div>
</li>
<!--Back to normal-->
<li>Cold Products.</li>
<li>Snacks.</li>
<li>About Us.</li>
<li>Contact Us.</li>
</ul>
By changing a bit of your CSS code you can easily do that as:
#navigation_bar {
text-align: center;
}
#navigation_bar li {
float: none;
display: inline-block;
vertical-align: top;
}
li .dropdown-content a:hover:not(.active) {
background: #d0d0d0; /* for altering the hover effect on submenus */
}
I have also created a JSFiddle.
Change navigation_bar css to :
#navigation_bar {
list-style-type: none;
margin: 0 auto;
position: fixed;
background-color: #333;
top: 0;
width: auto;
display: block;
float: left;
}
Try this
#navigation_bar{ text-align: center;} /*add align center */
li {display: inline; /* float:left; */} /* remove float left here and add display inline */