This is my nav bar with a drop down menu below some. Book goes below Departments, I have a lot more but this is an example. When I hover over more options, it loses focus on the nav bar and shows the on hover discription for an image. how do i stop this?
<div class="NavBar">
<div align="center">
<ul>
<li>Home</li>
<li>Departments
<ul class="MiniNavBar">
<li>Books</li>
<li>Books</li>
<li>Books</li>
<li>Books</li>
</ul></li>
<li>Top Brands</li>
<li>Gallery</li>
<li>About Us</li>
</ul>
</div>
</div>
My CSS
.NavBar{
background-color: #C10000;
height: auto;
width: 430px;
border: thin solid #000000;
border-radius: 5px;
margin-top: 8px;
}
.MiniNavBar{
width: 97px;
}
.NavBar ul ul {
display: none;
}
.NavBar ul li:hover > ul {
display: block;
font-family: Calibri;
font-size: 10px;
}
.NavBar ul {
padding: 2px;
list-style: none;
position: relative;
display: inline-block;
font-family: Calibri;
font-size: 15px;
}
.NavBar ul li {
float: left;
}
.NavBar ul li:hover {
background-color: #C10000;
color: #FFDD00;
}
.NavBar ul li:hover a {
background-color: #000000;
color: #FFDD00;
}
.NavBar ul li a {
display: block;
padding: 10px 10px;
background-color: #C10000;
color: #FFDD00;
text-decoration: none;
}
.NavBar ul ul {
position: absolute;
}
.NavBar ul ul li {
float: none;
margin-top: 0px;
position: relative;
}
.NavBar ul ul li a {
padding: 15px 40px;
background-color: #C10000;
color: #FFFFFF;
}
.NavBar ul ul li a:hover {
background-color: #C10000;
color: #FFFFFF;
}
.NavBar ul ul ul {
position: absolute;
left: 100%;
top:0;
}
Related
I have a navbar with a dropdown menu which works perfectly, but to show what is selected I want to make the background change on hover.
But when I try to do this it doesn't fit it looks like this:
And on the dropdown it looks like this:
And I want the background to fit with the navbar, but I don't know how.
nav {
width: 100%;
height: 60px;
background-color: gray;}
nav ul {
float: left;}
nav ul li {
float: left;
list-style: none;
position: relative;}
nav ul li a {
margin-left: -60px;
padding:50px;
font-family: Verdana;
color: black;
font-size: 24px;
text-decoration: none;}
nav ul li a:hover {
margin-left: -60px;
padding:50px;
font-family: Verdana;
color: white;
background-color: lightblue;
font-size: 24px;
text-decoration: none;}
nav ul li ul {
display: none;
position: absolute;
background-color: gray;
padding:10px 60px;
border-radius: 4px;}
nav ul li:hover ul {
display: block;}
nav ul li ul li {
width: 180px;
border-radius: 4px;}
nav ul li ul li a {
padding: 0;}
/*top right bottom left*/
nav ul li ul li a:hover {
padding: 10px 10px 10px 10px;
color: white;
background-color: lightblue;}
<nav>
<ul>
<li><a href='#'>Thuispagina</a></li>
<li><a href='#'>Religie</a>
<ul>
<li><a href='#'>Christendom</a>
<li><a href='#'>Islam</a>
<li><a href='#'>Boedhisme</a>
</ul>
</li>
<li><a href='#'>Interview</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
I think this is your requirement.
No need of any taking -margin instead that make ul padding-left to zero and for hover background add padding to <a> tag.
nav {
width: 100%;
background-color: gray;}
nav ul {
float: left;
font-size: 0;
padding: 0;
}
nav ul li {
float: left;
list-style: none;
position: relative;
background: gray;
}
nav ul li a {
margin-left: 0;
padding:15px 20px;
font-family: Verdana;
color: black;
font-size: 24px;
text-decoration: none;
display: block;
}
nav ul li a:hover {
font-family: Verdana;
color: white;
background-color: lightblue;
font-size: 24px;
text-decoration: none;}
nav ul li ul {
display: none;
position: absolute;
background-color: gray;
padding:10px 0;
border-radius: 4px;}
nav ul li:hover ul {
display: block;}
nav ul li ul li {
width: 180px;
border-radius: 4px;}
nav ul li ul li a {
padding: 15px 20px;}
/*top right bottom left*/
nav ul li ul li a:hover {
color: white;
background-color: lightblue;}
<nav>
<ul>
<li><a href='#'>Thuispagina</a></li>
<li><a href='#'>Religie</a>
<ul>
<li><a href='#'>Christendom</a>
<li><a href='#'>Islam</a>
<li><a href='#'>Boedhisme</a>
</ul>
</li>
<li><a href='#'>Interview</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
there are many points you need to understand Like where to use float, negative margins and should not copy same code on hover state, I removed some of those and added some basic style and your navbar is good to go. you can also check this codepen link https://codepen.io/shahilparichay/pen/OJOJMpj
body {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
margin: 0;
padding: 0;
}
nav {
width: 100%;
background-color: gray;
}
nav ul {
/* float: left; */
}
nav ul li {
/* float: left; */
list-style: none;
position: relative;
display: inline-block;
}
nav ul li a {
margin-left: 0px;
padding: 15px 10px;
font-family: Verdana;
color: black;
font-size: 24px;
text-decoration: none;
display: block;
}
nav ul li a:hover {
color: white;
background-color: lightblue;
}
nav ul li ul {
display: none;
position: absolute;
background-color: gray;
padding:10px 15px;
border-radius: 4px;}
nav ul li:hover ul {
display: block;
}
nav ul li ul li {
width: 180px;
border-radius: 4px;}
nav ul li ul li a {
padding: 10px 10px 10px 10px;
}
/*top right bottom left*/
nav ul li ul li a:hover {
color: white;
background-color: lightblue;
}
<nav>
<ul>
<li><a href='#'>Thuispagina</a></li>
<li><a href='#'>Religie</a>
<ul>
<li><a href='#'>Christendom</a>
<li><a href='#'>Islam</a>
<li><a href='#'>Boedhisme</a>
</ul>
</li>
<li><a href='#'>Interview</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>
You have to edit the padding values to set the background size on hover.
To change the background color to match the navbar when hovered in the category:
change this code:
nav ul li a:hover {
margin-left: -60px;
padding:50px;
font-family: Verdana;
color: white;
background-color: lightblue;
font-size: 24px;
text-decoration: none;}
to this:
nav ul li a:hover {
margin-left: -60px;
padding:16px;
margin-right: 10px;
font-family: Verdana;
color: white;
background-color: lightblue;
font-size: 24px;
text-decoration: none;
}
Blog about padding:
https://www.w3schools.com/css/css_padding.asp
I don't have time to trace your code, but you can use this code and make changes according to your need:
<!DOCTYPE html>
<html>
<head>
<title> Responsive Navigation Menus with Social Media</title>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-
awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script>
$(document).ready(function () {
$(".toggle-nav").click(function () {
$(".menu ul li").slideToggle("slow");
});
});
</script>
<style>
body {
margin: 0;
padding: 0;
font-family: arial;
}
.menu {
width: 100%;
background: #333;
overflow: auto;
}
.menu ul {
margin: 0;
padding: 0;
list-style: none;
line-height: 70px;
}
.menu ul li {
float: left;
}
.menu ul li a {
text-decoration: none;
width: 100px;
display: block;
text-align: center;
font-size: 15px;
text-transform: uppercase;
font-weight: bold;
color: #fff;
}
.menu ul li a:hover {
background: #6666ff;
}
.menu ul li a.active {
background: orange;
}
.toggle-nav {
color: #fff;
text-decoration: none;
display: none;
}
.social {
float: right;
margin-right: 100px;
line-height: 70px;
}
.social a {
max-width: 50px;
text-align: center;
}
.social a i {
font-size: 20px;
background: #fff;
width: 40px;
height: 40px;
line-height: 40px;
color: #000;
border-radius: 50%;
margin: 5px;
}
.social a i:hover {
background: #6666ff;
color: #fff;
}
#media(max-width:800px) {
.toggle-nav {
font-size: 40px;
text-align: center;
font-weight: bold;
padding: 10px 0px 10px 0px;
background: #6666ff;
width: 100%;
display: block;
}
.menu ul li a {
width: 100%;
}
.menu ul li {
float: none;
width: 100%;
display: none;
;
}
.social {
width: 100%;
text-align: center;
border-top: 1px solid #fff;
float: none;
}
}
#media(min-width:850px) {
.menu ul li {
display: block !important;
}
}
</style>
</head>
<body>
<nav class="menu">
<a class="toggle-nav" href="#">☰</a>
<ul>
<li><a class="active" href="#">Home</a></li>
<li>News</li>
<li>Blog</li>
<li>About</li>
<li>Privacy</li>
<li>Contact</li>
</ul>
<div class="social">
<i class="fa fa-facebook"></i>
<i class="fa fa-twitter"></i>
<i class="fa fa-youtube"></i>
<i class="fa fa-instagram"></i>
<i class="fa fa-linkedin"></i>
</div>
</nav>
</body>
</html>
I am making a website and my navigation bar's drop down is not lining up with the parent navigation bar when I try and position it into the middle of the page. When I use relative positioning, it does not line up. This is my code:
nav ul ul {
display: none;
margin: 0;
padding: 10px;
list-style-type: none;
text-align: center;
background-color: #A7C5A5;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
text-decoration: none;
padding: 0em;
color: #030;
background-color: #A7C5A5;
font-family: Georgia, "Times New Roman", Times, serif;
list-style: none;
position: relative;
display: inline-table;
top: 60px;
left: 300px;
float: inherit;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav ul li {
float: left;
display: inline;
}
nav ul li:hover {
background: #4b545f;
}
nav ul li:hover a {
color: #000;
background-color: #CCC;
}
nav ul li a {
display: block;
padding: 25px 40px;
color: #757575;
text-decoration: none;
}
nav ul ul {
background-color: #A7C5A5;
border-radius: 0px;
padding: 0;
position: relative;
top: 60px;
left: 300px;
}
nav ul ul li {
float: none;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li a:hover {
color: #000;
background-color: #CCC;
}
<body>
<div id="bg-right"></div>
<div id="bg-left"></div>
<div class="img">
<img src="greenlogo.jpg" width="504" height="160">
</div>
<nav>
<ul>
<li> Home</li>
<li>Animals
<ul>
<li>Disney</li>
<li>Farm</li>
<li>Nickolodean
</ul>
</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</body>
I tried to make a fiddle for private and I saw in Chrome-Devconsole that some styles are double used or used where you won't that they are used. I would recommend to use classes for the different ul's
Devlen
I have noticed my nav bar is transparent and I would like it to not be. I have no previous opacity/transparency set that would cause it to be inheriting the property. I would like to make my nav bar non transparent.
Here is the CSS:
nav {
margin: 20px auto;
text-align: center;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
font-size: 25px;
background: white;
padding: 0px;
border-radius: 10px;
border-style: solid;
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 {
background: black;
}
nav ul li:hover a {
opacity: 1;
color: white;
}
nav ul li a {
display: block;
padding: 15px 20px;
color: black;
text-decoration: none;
}
nav ul ul {
background: #000000;
border-radius: 0px 0px 10px 10px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul li {
float: none;
}
nav ul ul li a {
padding: 15px 20px;
}
nav ul ul li a:hover {
background: #2E2E2E;
border-radius: 10px;
}
#welcome_paragraph {
position: relative;
top: 50px;
width: 500px;
margin: auto;
}
Here is the corresponding HTML:
<nav>
<ul>
<li>Information
<ul>
<li>Getting Started?</li>
<li>About Us</li>
<li>Contact</li>
</ul>
</li>
<li>Starter Kits</li>
<li>Rebuildables
<ul>
<li>Genesis</li>
<li>Dripper</li>
<li>Silica/Cotton</li>
</ul>
</li>
<li>Mods
<ul>
<li>Mechanical</li>
<li>Variable Voltage</li>
</ul>
</li>
<li>Accessories</li>
</ul>
</nav>
<p id="welcome_paragraph">
Welcome, blah blah (this text shows through the nav bar)<br />
</p>
HTML
<nav>
<ul>
<li>Information
<ul>
<li>Getting Started?
</li>
<li>About Us
</li>
<li>Contact
</li>
</ul>
</li>
<li>Starter Kits
</li>
<li>Rebuildables
<ul>
<li>Genesis
</li>
<li>Dripper
</li>
<li>Silica/Cotton
</li>
</ul>
</li>
<li>Mods
<ul>
<li>Mechanical
</li>
<li>Variable Voltage
</li>
</ul>
</li>
<li>Accessories
</li>
</ul>
</nav>
<p id="welcome_paragraph">Welcome, blah blah (this text shows through the nav bar)
<br />
</p>
CSS
nav {
margin: 20px auto;
text-align: center;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
font-size: 25px;
background: white;
padding: 0px;
border-radius: 10px;
border-style: solid;
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 {
background: black;
position:relative;
z-index:1;
}
nav ul li:hover a {
color: white;
position:relative;
z-index:1;
}
nav ul li a {
display: block;
padding: 15px 20px;
color: black;
text-decoration: none;
}
nav ul ul {
background: #000000;
border-radius: 0px 0px 10px 10px;
padding: 0;
position: absolute;
top: 100%;
}
nav ul ul li {
float: none;
}
nav ul ul li a {
padding: 15px 20px;
}
nav ul ul li a:hover {
background: #2E2E2E;
border-radius: 10px;
}
#welcome_paragraph {
position: relative;
top: 50px;
width: 500px;
margin: auto;
color:white;
}
body
{
background-color:blue;
}
Updated CSS of yours
nav ul li:hover {
background: black;
position:relative;
z-index:1;
}
nav ul li:hover a {
color: white;
position:relative;
z-index:1;
}
Updated Fiddle
Have you tried;
nav {
background: white;
}
Elements are transparent unless you set a background on them or its inherited.
EDIT: If this doesn't help set up a fiddle for us jsfiddle.net.
In your css
nav ul {
font-size: 25px;
background: white;
padding: 0px;
border-radius: 10px;
border-style: solid;
list-style: none;
position: relative;
display: inline-table;
}
background is white.
If you change background to other colour may be your problem will go off. Hope it will help
Cheers !!
I'm trying to make a basic dropdown menu with css and html. However when I hover on the li that has to drop down, my whole menu comes down with it.
This is my code:
<nav class="icons">
<ul>
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
<li>Account
<ul id="login">
<li>Change password</li>
<li>Update details</li>
<li>Logout</li>
</ul>
</li>
</ul>
</nav>
And the CSS
nav {
height: 70px;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
ul {
margin: 0;
padding: 0;
list-style: none;
margin-left: 5%;
}
ul li {
display: inline-block;
width: 15%;
text-align: center;
line-height: 70px;
}
ul li a {
text-decoration: none;
color: #fff;
font-size: 2em;
display: block;
}
ul li a:hover {
border-bottom: solid black 1px;
}
ul#login{
margin: 0;
padding: 0;
list-style: none;
}
ul#login li {
display: block;
width: 30%;
text-align: center;
line-height: 70px;
}
ul#login li a {
text-decoration: none;
color: #fff;
font-size: 2em;
display: block;
}
ul#login li ul li{
width: 20%;
}
ul#login li ul li a {
text-decoration: none;
color: #fff;
font-size: 0.7em;
display: block;
}
ul#login li a:hover {
border-bottom: solid black 1px;
}
I know it's a lot of CSS for such a basic menu but I don't know how to make it more compact.
Can someone please tell me what I'm doing wrong with the dropdown menu?
Add this css
ul li{
position:relative;
}
#login{
position:absolute;
top:71px;
left:0;
}
FIDDLE
Below is my code, currently when I hover on "About Us" everything below the dropdown menu opens; how can i change the css so that it only hovers when i mouseover, this means, when i hover on "Team", then I should see the menus below it.
also how can i adjust the width so that it is shiffted more to the left.
also when the dropdown menu is longer in lenth, it hides below my content, i want the dropdown menu to be over the body of the page, not in hiding.
thanks in advance you all.
<style>
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul { display: none; }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #000061;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
display: block;
position: absolute;
}
ul li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
</style>
<body>
<ul id="menu">
<li><b>Home</b></li>
<li><b>About Us</b>
<ul>
<li>Team
<ul>
<li>Profile</li>
<li>Board</li>
</ul>
</li>
</ul>
</li>
<ul>
</body>
JSFiddle: http://jsfiddle.net/LWEry/
Like this:
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul { display: none; }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #000061;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover > ul {
display: block;
position: absolute;
width: 100%;
}
ul li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
.sub-menu
{
position: absolute;
top: 0;
left: 100%;
}
http://jsfiddle.net/3xWcu/2/
I changed one selector.
FROM
li:hover ul
TO
li:hover > ul
Edited my fiddle above. Added a sub-menu class to the ul containing the Profile and Board li tags:
<ul class="sub-menu">
<li>Profile</li>
<li>Board</li>
</ul>
and added some CSS above.
You mean like this? http://jsfiddle.net/3xWcu/
<style>
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul { display: none; }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #000061;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
display: block;
position: absolute;
width: 100%;
}
ul li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
</style>
<body>
<ul id="menu">
<li><b>Home</b></li>
<li><b>About Us</b>
<ul>
<li>Team
<ul>
<li>Profile</li>
<li>Board</li>
</ul>
</li>
</ul>
</li>
<ul>
</body>