I used this video https://www.youtube.com/watch?v=QPO9HJjUktk to create a CSS dropdown menu with submenus.
I got everything to look right and I have tried linking the list items in the submenus to other .html files and actual websites, but nothing works. When I click on the submenu item to go to whatever page, nothing loads and it just stays on the current page with the submenu open.
HTML:
'<nav class="nav-main">
<div class="logo">Kudler Fine Foods</div>
<ul>
<li> Departments
<div class="nav-content">
<div class="nav-sub">
<ul>
<li>Bakery</li>
<li>Meat & Seafood</li>
<li>Produce</li>
<li>Cheese & Dairy</li>
<li>Wine</li>
</ul>
</div>
</div>
</li>
</ul>
<ul>
<li> Information
<div class="nav-content">
<div class="nav-sub">
<ul>
<li>News</li>
<li>About</li>
<li>Locations</li>
</ul>
</div>
</div>
</li>
</ul>
<ul>
<li> Other
<div class="nav-content">
<div class="nav-sub">
<ul>
<li>Surveys</li>
<li>Terms & Conditions</li>
</ul>
</div>
</div>
</li>
</ul>
</nav>'
CSS:
#charset "utf-8";
/* CSS Document */
body, html {
margin: 0;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
}
.content {
padding:30px;
}
.nav-main {
width:100%;
background-color:#FF6700;
height:70px;
color:#fff;
}
.nav-main .logo {
float:left;
height:40px;
padding:15px 30px;
font-size:1.4em;
line-height:40px;
}
.nav-main > ul {
margin:0;
padding:0;
float:left;
list-style-type:none;
}
.nav-main > ul > li {
float:left;
}
.nav-item {
display: inline-block;
padding: 15px 20px;
height: 40px;
line-height:40px
color: #fff;
text-decoration: none;
color: #FFFFFF;
}
.nav-tem:hover {
background-color:#444;
}
.nav-content {
position:absolute;
top:70px;
overflow:hidden;
background-color:#222;
color:#fff;
max-height:0;
}
.nav-content a {
color:#fff;
text-decoration:none;
}
.nav-content a:hover {
text-decoration:underline;
}
.nav-sub {
padding:20px;
}
.nav-sub ul {
padding:0;
margin:0;
list-style-type:none;
}
.nav-sub ul li a {
display:inline-block;
padding:5px 0;
}
.nav-item:focus {
background-color:#444;
}
.nav-item:focus ~ .nav-content {
max-height:400px;
-webkit-transition:max-height 400ms ease-in;
-moz-transition:max-height 400ms ease-in;
transition:max-height 400ms ease-in;
}
Related
For my website I am designing a navigation bar and the goal is to position each element at the same height. This is the code I have so far:
body {
display:block;
margin:8px;
}
li, a {
font-family: "Open Sans", sans-serif;
font-size:14px;
text-decoration:none;
font-weight:600px;
color:black;
}
header {
display:flex;
justify-content:space-between;
align-items:center;
padding:10px 10%;
}
.Nav_links {
list-style: none;
}
.Nav_links li {
display: inline-block;
padding: 0px 20px;
}
.Nav_links li, a:hover {
transition: 400ms;
color:grey;
}
#Login_nav {
display:flex;
justify-content: space-around;
}
<header>
<img class="logo" src="images/logo_image.svg" alt="logo">
<nav>
<ul class="Nav_links">
<li><a href="#">Features <i class="fa-solid fa-angle-down"></i></a</li>
<li>Pricing</li>
<li>Contact Us</li>
</ul>
</nav>
<div id="Login_nav">
Login
<button id="Demobutton">Get demo</button>
</div>
</header>
I assume I did something wrong with the styling of the elements in CSS, because whenever I remove the CSS formatting of my id "#Login_nav" the login and get demo button do stand next to each other in the correct fashion, but still located too closely to each other.
Add a { gap: 1rem } and { align-items: center } on Login_nav, and I think you will get what you want, like so:
body {
display:block;
margin:8px;
}
li, a {
font-family: "Open Sans", sans-serif;
font-size:14px;
text-decoration:none;
font-weight:600px;
color:black;
}
header {
display:flex;
justify-content:space-between;
align-items:center;
padding:10px 10%;
}
.Nav_links {
list-style: none;
}
.Nav_links li {
display: inline-block;
padding: 0px 20px;
}
.Nav_links li, a:hover {
transition: 400ms;
color:grey;
}
#Login_nav {
display:flex;
align-items:center;
gap:1rem;
}
<header>
<img class="logo" src="images/logo_image.svg" alt="logo">
<nav>
<ul class="Nav_links">
<li><a href="#">Features <i class="fa-solid fa-angle-down"></i></a</li>
<li>Pricing</li>
<li>Contact Us</li>
</ul>
</nav>
<div id="Login_nav">
Login
<button id="Demobutton">Get demo</button>
</div>
</header>
I have created a position:fixed; navbar at the top of my web page and one of its items is supposed to be a drop down list named "services" which is supposed to show the dropdown on hover but its not working.
Below is the HTML page code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mainpage.css">
<title>Company name</title>
</head>
<body>
<div class="navdiv">
<img style="width:30%;margin-left:0;" src="images/HEH_logo_No_bd_mini.png">
<ul class="nav">
<li>HOME</li>
<li>ABOUT US</li>
<li >
<a class="dropdown" href="">SERVICES</a>
<div class="dropdownlist">
AUDIT & ASSURANCE
TAXATION
CORPORATE FINANCE
SERVICES
MANAGEMENT CONSULTING
RISK MANAGEMENT
SECTORS
HUMAN RESOURCES
ACCOUNTING, AUDIT & TAX
</div>
</li>
<li>PARTNERS</li>
<li>CONTACT US</li>
</ul>
</div>
<div class="divbody">
<p>This here will serve as <br>Some text ....</p>
<p>Some text ....</p>
</div>
</body>
</html>
And here is the CSS stylesheet code:
body {
background-color:white;
}
.divbody {
margin-top:12%;
height:100%;
padding:0;
}
.navdiv {
position:fixed;
top:0;
right:0;
width:100%;
background-color:white;
background-image:url("images/always_grey.png");
}
.nav {
list-style-type:none;
background-color:#5f5f5f;
margin:0;
padding:0;
position:relative;
top:0;
right:0;
width:100%;
font-family:"Segoe UI",Arial,sans-serif;
font-size:17px;
letter-spacing:1px;
overflow:hidden;
}
.nav li {
float:left;
}
.nav li a {
text-decoration:none;
color:white;
display:block;
padding:8px 16px;
}
.nav li a:hover {
background-color:black;
}
.nav .dropdownlist {
display: none;
background-color: #f9f9f9;
color: black;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.nav .dropdown:hover .dropdownlist {
display: block;
}
This code does not work because dropdownlist is not a child of dropdown, they're just adjacent elements so you need to use the + sign in order to achieve what you want.
Here's a working example:
body {
background-color:white;
}
.divbody {
margin-top:12%;
height:100%;
padding:0;
}
.navdiv {
position:fixed;
top:0;
right:0;
width:100%;
background-color:white;
background-image:url("images/always_grey.png");
}
.nav {
list-style-type:none;
background-color:#5f5f5f;
margin:0;
padding:0;
position:relative;
top:0;
right:0;
width:100%;
font-family:"Segoe UI",Arial,sans-serif;
font-size:17px;
letter-spacing:1px;
overflow:hidden;
}
.nav li {
float:left;
}
.nav li a {
text-decoration:none;
color:white;
display:block;
padding:8px 16px;
}
.nav li a:hover {
background-color:black;
}
.nav .dropdownlist {
display: none;
background-color: #f9f9f9;
color: black;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.nav .dropdown:hover + .dropdownlist,
.dropdownlist:hover {
display: block;
}
<div class="navdiv">
<img style="width:30%;margin-left:0;" src="images/HEH_logo_No_bd_mini.png">
<ul class="nav">
<li>HOME</li>
<li>ABOUT US</li>
<li >
<a class="dropdown" href="">SERVICES</a>
<div class="dropdownlist">
AUDIT & ASSURANCE
TAXATION
CORPORATE FINANCE
SERVICES
MANAGEMENT CONSULTING
RISK MANAGEMENT
SECTORS
HUMAN RESOURCES
ACCOUNTING, AUDIT & TAX
</div>
</li>
<li>PARTNERS</li>
<li>CONTACT US</li>
</ul>
</div>
<div class="divbody">
<p>This here will serve as <br>Some text ....</p>
<p>Some text ....</p>
</div>
</div>
Below is the code. hope this will help you.
body {
background-color:white;
}
.navdiv {
position:fixed;
top:0;
right:0;
width:100%;
background-color:white;
background-image:url("images/always_grey.png");
}
ul.nav {
list-style-type:none;
background-color:#5f5f5f;
margin:0;
padding:0;
position:relative;
top:0;
right:0;
width:100%;
font-family:"Segoe UI",Arial,sans-serif;
font-size:17px;
letter-spacing:1px; float:left;
}
ul.nav li {
float:left;
}
ul.nav li a {
text-decoration:none;
color:white;
display:block;
padding:8px 16px;
}
ul.nav li a:hover {
background-color:black;
}
ul.nav li ul.dropdownlist {
display: none;
background-color: #ccc;
color: black;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); position:absolute; right:0; top:38px;
}
ul.nav li:hover ul.dropdownlist {
display: block;
}
ul.nav li ul.dropdownlist li a {
color:#333;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mainpage.css">
<title>Company name</title>
</head>
<body>
<div class="navdiv">
<img style="width:30%;margin-left:0;" src="images/HEH_logo_No_bd_mini.png">
<ul class="nav">
<li>HOME</li>
<li>ABOUT US</li>
<li>
<a class="dropdown" href="">SERVICES</a>
<ul class="dropdownlist">
<li>AUDIT & ASSURANCE</li>
<li>TAXATION</li>
<li>CORPORATE FINANCE</li>
<li>SERVICES</li>
<li>MANAGEMENT CONSULTING</li>
<li>RISK MANAGEMENT</li>
<li>SECTORS</li>
<li>HUMAN RESOURCES</li>
<li>ACCOUNTING, AUDIT & TAX</li>
</ul>
</li>
<li>PARTNERS</li>
</ul>
</div>
<div class="divbody">
<p>This here will serve as <br>Some text ....</p>
<p>Some text ....</p>
</div>
</body>
</html>
Below change will help you only to show drop down on hover but you need to make few CSS changes in order to display the drop down properly
You need replace the following CSS
.nav .dropdown:hover .dropdownlist {
display: block;
}
With
.nav .dropdown:hover + .dropdownlist {
display: block;
}
Whenever I hover mouse on that ul the background color must be changed for both list item <i> and <a> tags.
CSS
.mainlist {
list-style: none;
margin-top: 50px;
margin-left: 15px;
}
.submenu {
list-style: none;
display: list-item;
margin: 0px;
padding: 0px;
width: 15px;
}
.submenu li {
margin-top: 10px;
float: left;
display: inline;
margin: 5px;
text-align: center;
}
.submenu li i {
font-size: 25px;
color: white;
}
.submenu li a {
margin-top: 20px;
font-size: 20px;
color: white;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
HTML
<div style="float:left;margin-left:-250px;width:200px;background-color:dimgray;">
<nav id="sidenav">
<ul class="mainlist">
<ul class="submenu">
<li><i class="fa fa-home"></i></li>
<li>Home</li>
</ul>
</ul>
</nav>
</div>
How can I do that? I want something like this:
To change children's appearance on hover do something like this:
.submenu:hover li {
background:red;
}
UPDATE:
Added a snippet to show the effect you want.
.mainlist {
list-style: none;
display:block;
background:#35414f;
position:relative;
width:200px;
padding:50px;
margin:0;
}
.mainlist li {
display: block;
text-align: center;
color: #808b97;
width:100%;
padding:10px 0;
cursor:pointer;
border-bottom:1px solid #3a4654;
}
.mainlist li:hover {
background:#1eae9a;
color:white;
}
.mainlist li:hover .submenu li a {
color:white;
}
.submenu {
list-style: none;
display: list-item;
margin: 0;
padding: 0;
}
.submenu li {
display:block;
color: #808b97;
margin: 10px 0 0;
padding:0;
border:none;
}
.submenu li i {
display:inline-block;
font-size: 25px;
color:inherit;
}
.submenu li a {
color: inherit;
text-decoration:none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<div>
<nav id="sidenav">
<ul class="mainlist">
<li>Lorem Ipsum</li>
<li>
Lorem Ipsum
<ul class="submenu">
<li><i class="fa fa-home"></i> Home</li>
<li><i class="fa fa-home"></i> Home</li>
</ul>
</li>
</ul>
</nav>
</div>
NOTE: Make sure to always wrap your submenu list into another li tag.
<ul class="mainmenu">
<li>
<ul class="submenu"></ul>
</li>
</ul>
UPDATE 2: CSS change + added Icon font
Use css :hover selector
.submenu:hover li{
background-color: black /* or any color you want */;
}
Also, is not correct to place a ul element as a child of another ul element
Check this:
correct semantics for ul in ul
You will have to use the pseudo class :hover, like this
.submenu:hover li{
background-color:green;
}
And for the text to change color too
.submenu:hover li > a{
color:black;
}
Here is jsfiddle
I am trying to create a sliding menu docked at the top right corner of the screen, everything seems ok, except the menu isn't sliding as intended.
HTML:
<nav class="social-nav">
<ul>
<li class="twitter"><img src="images/twitter-up.jpg" /> Follow us on Twitter</li>
<li class="fb"><img src="images/fb-up.jpg" /> Like our Facebook page</li>
<li class="insta"><img src="images/insta-up.jpg" /> Follow us on Instagram</li>
<li class="mail"><img src="images/mail-up.jpg" /> Send us a mail</li>
</ul>
</nav>
css:
body {background:#efefef; margin:0; padding:0;}
.social-nav {
position:absolute;
top:20px;
right:0;
z-index:999;
font:10px Verdana, Geneva, sans-serif;
}
.social-nav ul {padding:0; margin:0;}
.social-nav ul li {
list-style:none;
display: block;
margin: 1px;
width:36px;
text-decoration:none;
transition-duration:0.5s;
-webkit-transition-duration:0.5s;
-moz-transition-duration:10.5s;
}
.social-nav ul li:hover {width:200px;}
.social-nav a{color:rgba(0,0,0,0); display:block; text-decoration:none;width:36px; height:25px; padding:0 0 15px 0;}
.social-nav a:hover{color:rgba(255,255,255,1); display:block; width:200px;}
.social-nav .twitter{background:#00acee;}
.social-nav .fb{ background:#3b5999;}
.social-nav .insta{ background:#5280a4;}
.social-nav .mail a{ background:#fff; color:rgba(0,0,0,0);}
.social-nav .mail a:hover { background:#fff; color:rgba(0,0,0,0);}
Cant comment, so I updated your fiddle..
http://jsfiddle.net/2p6h7xq0/1/
.social-nav:hover li{ width: 200px}
.social-nav li:hover a{color:rgba(255,255,255,1); display:block; width:200px;}
I am having troubles centering (horizontally) my ul# menu with the middle of the page. It is all functioning properly and as intended, I am just having issues with the centering.
Here is the CSS:
ul#menu, ul#menu ul.sub-menu {
padding: 0px;
margin: 0px;
}
ul#menu li, ul#menu ul.sub-menu li {
list-style-type: none;
display: inline-block;
}
ul#menu li a, ul#menu li ul.sub-menu li a {
text-decoration: none;
padding: 0px;
display: inline-block;
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif
font-weight: bold;
font-size: 17px;
}
ul#menu li a {
padding: 15px;
}
ul#menu li {
position: relative;
}
ul#menu li ul.sub-menu {
display:none;
position: absolute;
top: 46px;
left: 15px;
width: 150px;
}
ul#menu li:hover ul.sub-menu {
display:block;
}
ul#menu li ul.sub-menu li a {
background-color:grey;
opacity:1;
height:20px;
border-bottom-style:solid;
border-bottom-color:#30F;
border-bottom-width:2px;
padding:5px;
font-weight:bold;
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
font-size: 12px;
}
#menucontainer {
position:absolute;
width:700px;
top:158px;
margin-left: auto;
margin-right: auto;
opacity:0.8;
z-index:20;
}
Here is the HTML:
<div id="menucontainer">
<ul id="menu">
<li>
Home
</li>
<li>
Games
<ul class="sub-menu">
<li>
>> Descent
</li>
</ul>
</li>
<li>
Prizes
</li>
<li>
Ladder
</li>
<li>
Community
<ul class="sub-menu">
<li>
>> Submissions
</li>
<li>
>> Support/Help
</li>
</ul>
</li>
<li>
Forums
</li>
<li>
About Us
<ul class="sub-menu">
<li>
>> Engage Arcade
</li>
<li>
>> Those Involved
</li>
</ul>
</li>
</ul>
</div>
Sorry for the big hunk of code, but I do not know exactly which parts would be relevant in helping solve the issue. The main thing to look for is the menucontainer.
Cheers for any suggestions.
Fiddle: http://jsfiddle.net/HcfMT/
The problem is that it's positioned absolutely. Get rid of that, since you're misusing it anyway.
http://jsfiddle.net/HcfMT/2/
width:700px;
margin-top:158px;
This is a simple fix... you are very close to having it.
#menucontainer
position:absolute;
width:700px;
top:158px;
margin-left: auto;
margin-right: auto;
opacity:0.8;
z-index:20;
to
#menucontainer {
position:relative;
width:700px;
top:158px;
margin: 0 auto;
opacity:0.8;
z-index:20;
}
You need the #menucontainer to be relatively positioned.