How to fix navbar dropdown menu in css - html

I have a problem with the dropdown menu when I hover in services menu. Here's the problem (on Codepen)
*{
margin: 0;
padding: 0;
}
body{
margin: 0 auto;
width: 800px;
}
a{
font-family: 'Alegreya Sans Regular';
}
#navigation{
height: 113px;
width: 800px;
background-color: #17384D;
}
.weblogo{
margin-top: 34px;
margin-left: 20px;
}
nav{
float: right;
margin-top: 60px;
margin-right: 60px;
}
nav ul ul{
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul li{
display: inline-block;
padding-left: 45px;
}
nav ul li a{
text-decoration: none;
list-style: none;
color: #FAD46B;
font-size: 1.3em;
}
.active{
border-bottom: 2px solid #FF972C;
padding-left: 0px;
}
<div id="navigation">
<img class="weblogo"src="weblogo.png"></img>
<nav>
<ul>
<li class="active">home</li>
<li>work</li>
<li>about</li>
<li class="dropdown">services
<ul>
<li>Search</li>
<li>Search</li>
<li>Search</li>
<li>Search</li>
</ul>
</li>
<li>contact</li>
</ul>
</nav>

Better way to create sub menus is this
Replace your html with this
HTML
<div id="navigation">
<img class="weblogo"src="weblogo.png"></img>
<nav>
<ul>
<li class="active">home</li>
<li>work</li>
<li>about</li>
<li class="dropdown"><a onClick="toggleNavPanel('panel')" >services</a>
<div id="panel">
<div>
<ul>
<li>Search</li>
<li>Search</li>
<li>Search</li>
<li>Search</li>
</ul>
</div>
</div>
</li>
<li>contact</li>
</ul>
</nav>
And add this to your css
#panel {
position: absolute;
background-color: red;
height: 0;
width: 100px;
top: 120px;
transition: height 0.3s linear 0s;
overflow:hidden;
}
Add this javascript also
function toggleNavPanel(x) {
var panel = document.getElementById(x), maxH="300px";
if(panel.style.height == maxH) {
panel.style.height = "0px";
}else {
panel.style.height = maxH;
}
}
Also remove this from your css
nav ul li:hover > ul {
display: block;
}

Related

How to make dropdown menu same width as each <li> in nav?

I'm trying to create a dropdown menu, but for some reason, I cannot find a way to make its width the same as each <li>in nav.
/* GENERAL STYLES */
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url("../img/paven.png");
}
#font-face {
font-family: "nevis";
src: url("../fonts/nevis.ttf");
}
/* MENU */
nav {
width: 100%;
height: 40px;
background-color: #333;
}
nav > div {
width: 1000px;
margin: 0px auto;
}
nav > div > ul {
float: left;
}
nav > div > ul > li {
list-style: none;
height: 40px;
padding-left: 20px;
padding-right: 20px;
border-right: 1px solid gray;
}
nav > div > ul > li > a {
display: block;
height: 40px;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
color: white;
line-height: 35px;
}
nav > div > ul > li:hover {
cursor: pointer;
background-color: #70B231;
}
/* SUBMENU */
nav > div > ul > li > ul {
position: absolute;
width: 100px;
background-color: #70B231;
}
<!-- MENU -->
<nav>
<div>
<ul>
<li>Home</li>
</ul>
<ul>
<li>Content</li>
</ul>
<ul>
<li>Requirements</li>
</ul>
<ul>
<li>
Languages
<ul>
<li>JavaScript</li>
<li>CSS</li>
<li>HTML</li>
</ul>
</li>
</ul>
<ul>
<li>
Frameworks
<ul>
<li>Less</li>
<li>Sass</li>
<li>Flexbox</li>
</ul>
</li>
</ul>
<ul>
<li>Projects</li>
</ul>
<ul>
<li>Instructor</li>
</ul>
<ul>
<li>Reviews</li>
</ul>
<ul>
<li>Blog</li>
</ul>
<ul>
<li>Contact</li>
</ul>
</div>
</nav>
As you can see, I've tried setting a width for each <ul>, but they are not well aligned to the <li> they belong to:
I would like to achieve something like this:
Here you go, I've edited a few thing.
I removed the padding on the main menu li element and placed it on the a instead so the submenu can take the full width.
Added postition: relative; to the main menu li so the submenu postition: absolute will be relative to the main menu item
Made the submenu full width with width 100% and removed the list styles.
added some padding to the submenu li
/* GENERAL STYLES */
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url("../img/paven.png");
}
#font-face {
font-family: "nevis";
src: url("../fonts/nevis.ttf");
}
/* MENU */
nav {
width: 100%;
height: 40px;
background-color: #333;
}
nav>div {
width: 1000px;
margin: 0px auto;
}
nav>div>ul {
float: left;
}
nav>div>ul>li {
list-style: none;
height: 40px;
position: relative;
border-right: 1px solid gray;
}
nav>div>ul>li>a {
display: block;
height: 40px;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
color: white;
line-height: 35px;
padding-left: 20px;
padding-right: 20px;
}
nav>div>ul>li:hover {
cursor: pointer;
background-color: #70B231;
}
/* SUBMENU */
nav>div>ul>li>ul {
position: absolute;
width: 100%;
background-color: #70B231;
list-style: none;
}
nav>div>ul>li>ul>li {
padding: 3px 10px;
}
<!-- MENU -->
<nav>
<div>
<ul>
<li>Home</li>
</ul>
<ul>
<li>Content</li>
</ul>
<ul>
<li>Requirements</li>
</ul>
<ul>
<li>
Languages
<ul>
<li>JavaScript</li>
<li>CSS</li>
<li>HTML</li>
</ul>
</li>
</ul>
<ul>
<li>
Frameworks
<ul>
<li>Less</li>
<li>Sass</li>
<li>Flexbox</li>
</ul>
</li>
</ul>
<ul>
<li>Projects</li>
</ul>
<ul>
<li>Instructor</li>
</ul>
<ul>
<li>Reviews</li>
</ul>
<ul>
<li>Blog</li>
</ul>
<ul>
<li>Contact</li>
</ul>
</div>
</nav>

Image behind drop down menu issue HTML CSS

I'm trying to make the drop down menu stay top right with the image behind it if anyone could help would be greatly appreciated i tried using z-index but that didn't work out for me !
( using a img off google so u guys can see what im talking about! )
/* Drop Down Menu */
.navClass {
z-index: 999;
float: right;
}
.right {
float: right;
}
.navClass > ul {
background-color: #5E5D5D;
color: #D8D8D8;
font-size: 20px;
font-family: sans-serif;
}
.navClass > ul > li {
list-style-type: none;
display: inline-block;
padding: 5px 25px;
position: relative;
}
.navClass > ul > li:hover {
background-color: #383838;
}
ul.sub-menu {
position: absolute;
background-color: #383838;
list-style-type: none;
width: 125px;
margin-top: 5px;
padding-left: 0pc;
margin-left: -25px;
padding-top: 5px;
opacity: 0;
}
ul.sub-menu li {
padding-left: 25px;
padding-top: 5px;
padding-bottom: 5px;
}
.navClass li:hover .sub-menu {
opacity: 1;
background-color: #7b7b7b;
}
.navClass ul li a {
text-decoration: none;
color: #D8D8D8;
}
.sub-menu li:hover {
background-color: #383838;
}
/* Background */
/* ZeeFro */
.zeefro {
z-index: -1;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title>STREAMER // DESIGNER // YOUTUBER</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<nav class="bg-Main">
<img src="">
</nav>
<nav class="zeefro">
<img src="https://i.ytimg.com/vi/d-zKJCKsoWw/maxresdefault.jpg" text-align: center; >
</nav>
<nav class="navClass">
<ul>
<li>Contact Me</li>
<li>Home</li>
<li>About Me</li>
<li><a href="">Portfolio
<ul class="sub-menu">
<li>Logos</li>
<li>Banners</li>
<li>Twitch</li>
<li>Youtube</li>
</ul>
</li>
<li><a href="">Shop
<ul class="sub-menu">
<li>Graphics</li>
<li>Merch</li>
</ul>
</li>
<li><a href="">Social Media
<ul class="sub-menu">
<li>Twitch</li>
<li>Youtube</li>
<li>All</li>
</ul>
</li>
</ul>
</nav>
</body>
</html>
Either i did something wrong within the z-index unless i don't know whats happening :P
Kind regards
ZeFrolity
You need to put position: absolute to the div with background.
but
if a image is for background, you will usually want to use css background-image attribute instead of <img> tag.
you will also only need one nav tag only
/* Drop Down Menu */
.navClass {
z-index: 999;
float: right;
}
.right {
float: right;
}
.navClass > ul {
background-color: #5E5D5D;
color: #D8D8D8;
font-size: 20px;
font-family: sans-serif;
}
.navClass > ul > li {
list-style-type: none;
display: inline-block;
padding: 5px 25px;
position: relative;
}
.navClass > ul > li:hover {
background-color: #383838;
}
ul.sub-menu {
position: absolute;
background-color: #383838;
list-style-type: none;
width: 125px;
margin-top: 5px;
padding-left: 0pc;
margin-left: -25px;
padding-top: 5px;
opacity: 0;
}
ul.sub-menu li {
padding-left: 25px;
padding-top: 5px;
padding-bottom: 5px;
}
.navClass li:hover .sub-menu {
opacity: 1;
background-color: #7b7b7b;
}
.navClass ul li a {
text-decoration: none;
color: #D8D8D8;
}
.sub-menu li:hover {
background-color: #383838;
}
/* Background */
/* ZeeFro */
.zeefro {
z-index: -1;
text-align: center;
position: absolute;
width:100%;
height:100%;
background-image: url("https://i.ytimg.com/vi/d-zKJCKsoWw/maxresdefault.jpg")
}
<!DOCTYPE html>
<html>
<head>
<title>STREAMER // DESIGNER // YOUTUBER</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div class="zeefro">
</div>
<nav class="navClass">
<ul>
<li>Contact Me</li>
<li>Home</li>
<li>About Me</li>
<li><a href="">Portfolio
<ul class="sub-menu">
<li>Logos</li>
<li>Banners</li>
<li>Twitch</li>
<li>Youtube</li>
</ul>
</li>
<li><a href="">Shop
<ul class="sub-menu">
<li>Graphics</li>
<li>Merch</li>
</ul>
</li>
<li><a href="">Social Media
<ul class="sub-menu">
<li>Twitch</li>
<li>Youtube</li>
<li>All</li>
</ul>
</li>
</ul>
</nav>
</body>
</html>
You can absolutely position the menu instead. I also removed the margin from body.
/* Drop Down Menu */
body {
margin: 0;
}
.navClass {
position: absolute;
top: 0; right: 0;
}
.right {
float: right;
}
.navClass > ul {
background-color: #5E5D5D;
color: #D8D8D8;
font-size: 20px;
font-family: sans-serif;
}
.navClass > ul > li {
list-style-type: none;
display: inline-block;
padding: 5px 25px;
position: relative;
}
.navClass > ul > li:hover {
background-color: #383838;
}
ul.sub-menu {
position: absolute;
background-color: #383838;
list-style-type: none;
width: 125px;
margin-top: 5px;
padding-left: 0pc;
margin-left: -25px;
padding-top: 5px;
opacity: 0;
}
ul.sub-menu li {
padding-left: 25px;
padding-top: 5px;
padding-bottom: 5px;
}
.navClass li:hover .sub-menu {
opacity: 1;
background-color: #7b7b7b;
}
.navClass ul li a {
text-decoration: none;
color: #D8D8D8;
}
.sub-menu li:hover {
background-color: #383838;
}
/* Background */
/* ZeeFro */
.zeefro {
z-index: -1;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title>STREAMER // DESIGNER // YOUTUBER</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<nav class="bg-Main">
<img src="">
</nav>
<nav class="zeefro">
<img src="https://i.ytimg.com/vi/d-zKJCKsoWw/maxresdefault.jpg" text-align: center; >
</nav>
<nav class="navClass">
<ul>
<li>Contact Me</li>
<li>Home</li>
<li>About Me</li>
<li><a href="">Portfolio
<ul class="sub-menu">
<li>Logos</li>
<li>Banners</li>
<li>Twitch</li>
<li>Youtube</li>
</ul>
</li>
<li><a href="">Shop
<ul class="sub-menu">
<li>Graphics</li>
<li>Merch</li>
</ul>
</li>
<li><a href="">Social Media
<ul class="sub-menu">
<li>Twitch</li>
<li>Youtube</li>
<li>All</li>
</ul>
</li>
</ul>
</nav>
</body>
</html>

Navigation Menu Alignment

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>

How to position submenus to appear correctly under the main menu (CSS)?

I've been able to create a horizontal menu using (display:inline) and I now have a drop menu thanks to sousMenu. My problem is that all the submenus, regardless of element I hovered over, appear in the same place. How do I work around this?
My menu code thus far:
<ul>
<li>Home</li>
<li class="sousMenu">About Us
<ul>
<li>Board of Directors</li>
</br>
<li>Student Profiles</li>
</br>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul>
<li>Donations</li>
</br>
<li>Job Board</li>
</br>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul>
<li>Connections</li>
</br>
<li>Gallery</li>
</br>
<li>Tours</li>
</ul>
</li>
CSS:
#navcontainer ul {
/*margin: 0;*/
margin-left: auto;
margin-right: auto;
padding: 0;
top:180;
right:20;
width:800px;
list-style-type: none;
text-align: center;
position: absolute;
color: #fff;
background-color: #003300;
padding: .2em 1em;
}
#navcontainer ul li {
display: inline;
padding-left:2cm;
}
#navcontainer ul li a {
text-decoration: none;
color: #fff;
background-color: #030;
}
#navcontainer ul li a:hover {
color: #fff;
background-color: #000;
}
.sousMenu:hover ul {
display: block;
}
.sousMenu ul {
text-align: center;
display: none;
list-style-type: none;
}​
Try setting the parent list item to position: relative and the child ul to position: absolute for starters. I made some other slight modifications to your code to achieve the desired effect.
Here's the CSS:
* {
margin: 0;
padding: 0;
vertical-align: baseline;
}
li {
list-style-type: none;
}
ul.main li {
position: relative;
display: inline-block;
}
.main li:hover > ul {
display: block;
}
ul.sub {
position: absolute;
display: none;
top: 100%;
left: 0;
}
ul.sub li {
display: block;
}
I also cleaned up the HTML a bit. You were missing a closing </ul> tag as well:
<ul class="main">
<li>Home</li>
<li class="sousMenu">About Us
<ul class="sub about">
<li>Board of Directors</li>
<li>Student Profiles</li>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul class="sub get-involved">
<li>Donations</li>
<li>Job Board</li>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul class="sub resources">
<li>Connections</li>
<li>Gallery</li>
<li>Tours</li>
</ul>
</li>
</ul>
Here's the fiddle: http://jsfiddle.net/vXhZb/2/

Creating drop down UL navigation bar

Here is my code so far:
<?php
echo '
<nav class="floatfix">
<ul>
<li>Home</li>
<ul>
<li>£3.99 T-Shirts</li>
<li>£4.99 T-Shirts</li>
</ul>
<li>Log In</li>
<li>Account</li>
<li>Feedback</li>
<li>Checkout</li>
<li>About Us</li>
</ul>
</nav>
';
?>
Here is my CSS
/* =float clearing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
.floatfix {
display: block;
height: 1%;
text-transform: uppercase;
}
.floatfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
How can I get the £3.99 Shirts & £4.99 Shirts as a drop down menu beneath the 'Home' on the navigation bar.
Thankyou very much
Not even going into the CSS you need to enclose the secondary UL in the Home LI like below:
<nav class="floatfix">
<ul>
<li>Home
<ul>
<li>£3.99 T-Shirts</li>
<li>£4.99 T-Shirts</li>
</ul>
</li>
<li>Log In</li>
<li>Account</li>
<li>Feedback</li>
<li>Checkout</li>
<li>About Us</li>
</ul>
</nav>
CSS stolen from a quick tutorial:
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: #1e7c9a;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: #3b3b3b;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #3b3b3b; }
li:hover li a:hover {
background: #1e7c9a;
} ​
Demo: http://jsfiddle.net/calder12/7j6H9/
Unless I am missing something, you want to use a select like like so:
<nav class="floatfix">
<ul>
<li>Home</li>
<select id="lstPrice">
<option value="3.99">£3.99 T Shirts</option>
<option value="4.99">£4.99 T Shirts</option>
</select>
<li>Log In</li>
<li>Account</li>
<li>Feedback</li>
<li>Checkout</li>
<li>About Us</li>
</ul>
</nav>