I have a navigation bar with a sub navigation, but whenever i hover over a certain part of the navigation, the sub menu opens in a very strange way. Here's my code:
#menu {
background-color: rgba(0, 0, 0, 0.6);
width: 715px;
margin-left: 600px;
font-family: "Franklin Gothic Medium", "Franklin Gothic", "ITC Franklin Gothic", Arial, sans-serif;
border-radius: 4px;
}
#menu ul li {
display: inline-block;
padding: 15px;
margin-left: 90px;
}
#menu ul li a {
text-decoration: none;
color: #FFF;
}
/* Sub-menu */
#menu ul ul {
display: none;
}
#menu ul li:hover > ul {
display: block;
}
<nav id="menu">
<ul>
<li>Home</li>
<li><a href="#">Projecten<i class="material-icons" style="font-size:15px">arrow_drop_down</i>
<ul class="sub-menu">
<li>Russisch</li>
</ul>
</a>
</li>
<li>Contact</li>
</ul>
</nav>
It should open underneath the "Projecten" tab, but is does this:
problem
That's because you add an element to the menu as soon as you set something from display: none; to display: block;. This'll pull everything out of proportion and makes it look like it does.
The solution is position: absolute; to remove the submenu from the flow of the site. I'll show you an example, using your code:
#menu {
background-color: rgba(0,0,0,0.6);
width: 715px;
margin-left: 600px;
font-family: "Franklin Gothic Medium", "Franklin Gothic", "ITC Franklin Gothic", Arial, sans-serif;
border-radius: 4px;
}
#menu ul li {
display: inline-block;
padding: 15px;
margin-left: 90px;
position: relative; /* This is needed to be able to set
the submenu relative to it's parent item */
}
#menu ul li a {
text-decoration: none;
color: #FFF;
}
/* Sub-menu */
#menu ul ul {
display: none;
/* Here we'll place it at the bottom of the menu item */
position: absolute;
top: 100%; /* This should equal the bottom of the item */
left: 0; /* To put it at the left side of the item */
/** And some basic styling to make it visible */
background: rgba(0,0,0,0.6);
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul>
<li>Home</li>
<li>Projecten<i class="material-icons" style="font-size:15px">arrow_drop_down</i>
<ul class="sub-menu">
<li>Russisch</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
I hope this'll clear up enough for you to continue your work.
EDIT: Also cleared up the HTML. You shouldn't open a new UL in a link.
Use positioning. Here's how:
Add position: relative to the li elements.
Add position: absolute to the second level ul element.
Add some background color to the second level ul element.
This will show it as a dropdown menu.
Working example:
#menu {
background-color: rgba(0,0,0,0.6);
width: 715px;
margin-left: 600px;
font-family: "Franklin Gothic Medium", "Franklin Gothic", "ITC Franklin Gothic", Arial, sans-serif;
border-radius: 4px;
}
#menu ul li {
display: inline-block;
padding: 15px;
margin-left: 90px;
position: relative; /*Added this*/
}
#menu ul li a {
text-decoration: none;
color: #FFF;
}
/* Sub-menu */
#menu ul ul {
display: none;
background-color: rgba(0,0,0,0.6); /*added this*/
position: absolute; /*and this*/
/*These two are positioning the dropdown relative to the bottom left corner of the parent item*/
left: 0;
top: 100%;
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul>
<li>Home</li>
<li><a href="#">Projecten<i class="material-icons" style="font-size:15px">arrow_drop_down</i>
<ul class="sub-menu">
<li>Russisch</li>
</ul>
</a></li>
<li>Contact</li>
</ul>
</nav>
/* edit by Manish*/
.sub-menu {
background: #333 none repeat scroll 0 0;
left: 0;
position: absolute;
right: 0;
top: 49px;
}
.parent{
position: relative;
}
/* edit by Manish*/
Add "parent" class to parent "li"
<nav id="menu">
<ul>
<li>Home</li>
<li class="parent"><a href="#">Projecten<i class="material-icons" style="font-size:15px">arrow_drop_down</i>
<ul class="sub-menu">
<li>Russisch</li>
</ul>
</a></li>
<li>Contact</li>
</ul>
</nav>
Related
WHEN RUNNING THIS CODE YOU HAVE TO VIEW IN FULL PAGE MODE
code for navigation, header, and style for both. whenever I zoom in or out on my browser the navbar keeps moving around and eventually it just disappears off of the screen, same thing happens whenever I try to resize the window.
I would like to my navbar to be responsive but remain inside of the header and not move around whenever the page is zoomed in. would greatly appreciate any help.
/* ---------- Navigation ---------- */
nav{
width: 100%;
height: 60px;
font-weight: bold;
color: white;
position: absolute;
bottom: 500px;
left: -250px;
}
nav ul{
float: right;
}
nav ul li{
float: left;
list-style: none;
position: relative;
}
nav ul li a{
font-family: arial;
color: black;
font-size: 20px;
padding: 22px 14px;
display: block;
text-decoration: none;
}
nav ul li ul{
display: none;
position: absolute;
background: #4f88bd;
padding: 10px;
border-radius: 0px 0px 4px 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: 8px 14px;
}
nav ul li ul li a:hover{
background: #26227d;
}
/* ---------- Navigation ---------- */
/* ---------- Header ---------- */
header{
background: #35424a;
color: #ffffff;
padding: 20px;
min-height: 70px;
text-align: left;
}
header h1{
font-size: 50px;
text-align: center;
}
header h1 a{
text-decoration: none;
color: white;
}
/* ---------- Header ---------- */
<header>
<h1>Olympic Class Liners</h1>
<nav>
<ul>
<li>Olympic
<ul>
<li>Overview</li>
<li>Crew</li>
<li>Decks</li>
<li>Passengers</li>
</ul>
</li>
<li>Titanic
<ul>
<li>Overview</li>
<li>Crew</li>
<li>Decks</li>
<li>Passengers</li>
<li>Sinking</li>
<li>Wreck</li>
</ul>
</li>
<li>Britannic
<ul>
<li>Overview</li>
<li>Crew</li>
<li>Decks</li>
<li>Passengers</li>
<li>Sinking</li>
<li>Wreck</li>
</ul>
</li>
<li>White Star Line
<ul>
<li>Overview</li>
<li>Crew</li>
<li>Decks</li>
</ul>
</li>
<li>Harlond and Wolff
<ul>
<li>Overview</li>
<li>Crew</li>
<li>Decks</li>
</ul>
</li>
<li>Galleries
<ul>
<li>Exterior Images</li>
<li>Interior Images</li>
</ul>
</li>
<li>Acknowledgments</li>
</ul>
</nav>
</header>
use media queries, here's an example:
/* Media Queries */
#media (max-width: 1024px) {
.nav li { width: 100%; }
.nav .dropdown ul {display:none; }
.nav .open ul { display: block; }
.nav-collapse .nav > li > a, .nav-collapse .dropdown-menu a {
text-align: center;
}
Im trying to remove the hover on my submenu when the media query is active. Right now it works how I want above 979px which displays the submenu on hover. However, at 979px and below the menu changes. Im trying to get the submenu to always display and can't seem to figure it out. there is a link attached to the services button so when its pressed to display the submenu you are re-directed whcih is why I want to display the submenu all the time at media query.
HTML:
<!-- Nav Bar -->
<div id="navbar">
<label for="show-menu" class="show-menu">Menu ☰</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu" class="nav">
<li class="nav active">Home</li>
<li class="nav">
Services
<ul class="nav hidden">
<li>Fire Alarm Systems</li>
<li>Security & Intrusion</li>
<li>Closed Circuit TV</li>
<li>Access Control</li>
<li>Systems Intrigation</li>
</ul>
</li>
<li class="nav">About Us</li>
<li class="nav">Tech Support</li>
<li class="nav">Photo Gallery</li>
<li class="nav">Testimonials</li>
<li class="nav">Contact Us</li>
</ul>
</div>
<!-- Nav Bar End -->
CSS:
ul.nav
{ list-style-type:none;
margin:0;
padding:0;
position: absolute;}
li.nav
{ display:inline-block;
float: left;
margin-right: 2px;}
li.nav a {
display:block;
min-width:153px;
height: 40px;
text-align: center;
line-height: 40px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #724e27;
text-decoration: none;
}
li.nav:hover a {
background: #cccccc;
color: #2f3036;
}
li.nav:hover ul a {
background: #f3f3f3; /* Light grey */
color: #2f3036; /* dark Grey */
height: 40px;
line-height: 40px;
}
li.nav:hover ul a:hover {
background: #996633; /* Light Brown */
color: #fff;
}
li.active a, li.active a:hover
{ background: #cccccc;
color: #2f3036;}
li.nav ul {
display: none;
}
li.nav ul li {
display: block;
float: none;
}
li.nav ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
ul.nav li a:hover + .hidden, .hidden:hover {
display: block;
z-index: 999;
}
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #261a0d;
background: #f3f3f3;
text-align: center;
padding: 10px 0;
display: none;
}
input[type=checkbox]{display: none;}
input[type=checkbox]:checked ~ #menu{display: block;}
/* Responsive Styles */
#media screen and (max-width : 979px){
ul.nav
{ position: static;
display: none;}
li.nav {margin-bottom: 1px;}
ul.nav li, li.nav a {width: 100%;}
.show-menu {display:block;}
}
you can use display: block !important inside your media query to display your sub-menu on smaller screen.
#media screen and (max-width : 979px){
li.nav > ul.hidden {
display: block !important;
}
}
I want to reposition my navbar. In its current position:
#navbar ul {
width: 100%;
float: left;
margin: 0;
padding: 0;
list-style: none;
background-color: #5D2C2C;
/*position: relative;
bottom: 115px;*/
}
#navbar ul li {
float: left;
/*position: relative;
left: 420px;*/
}
#navbar li li {
display: none;
}
#navbar li:hover li {
display: block;
width: inherit;
}
#navbar a {
font-family: "Palatino Linotype";
color: #FFF;
text-decoration: none;
padding: 2px 5px;
}
the dropdown displays correctly
but when I remove the /* comments */ so the bar is correctly positioned, the menu drops down but the two items aren't showing and I cannot find out why
the HTML if needed:
<div id="navbar">
<ul>
<li>Introduction</li>
<li>History</li>
<li>National Flags</li>
<li>International Maritime Signal Flags
<ul>
<li>Maritme Signals: Letters</li>
<li>Maritme Signals: Numbers </li>
</ul>
</li>
</ul>
I'm trying to center my nav bar.
HTML is
<nav>
<ul>
<li>HJEM</li>
<li>FORUM</li>
<li>DONER</li>
<li style="margin-right: 0px;">SERVERE
<li style="margin-right: 0px;">FAQ</li>
<li style="margin-right: 0px;">KONTAKT</li>
</ul>
</nav>
CSS is
nav {margin: 3px 0; width: 700px;}
nav ul {width: 700px; height: auto; list-style: none;}
nav ul li a {
background: #FFFFFF;
font-family: 'Open Sans', Arial, Helvetica, sans-serif;
font-weight: 300;
font-size: 18px;
text-align: center;
color: #717171;
text-decoration: none;
float: left;
padding: 8px 0;
width: 106px;
margin: 0px 10px 0 0;
}
nav ul li a:hover {background: #f1f1f1;}
Right now it floats from left to right. I want to center it.
Bonus question; if someone know this, if you can point me in the direction on how to create a touch compatible sub menu for "doner".
Thanks for your time.
hjortefjellet.com
If you want the elements to be in a line, I would use li { display:inline-block; }
then yo can define for your nav element: margin: 3px auto;.
Did I understand you right that you want a dropdown menu for the items in the nav? That's not too difficult: Add the dropdown menu as a div element into the li element:
<li>
HJEM
<div class="dropdown">Hello!<br />I'm a dropdown menu!</div>
</li>
Then add to the stylesheet:
.dropdown {
display:none;
position:absolute;
top:56px;
background-color:#f1f1f1;
width:200px;
padding:10px;
}
li:hover .dropdown, .dropdown:hover { display:block; }
Just do this
nav {
margin: 3px auto;
}
first of all, close your 4th "li" tag. Also, add "margin:0 auto;" to "nav ul" and remove inline styles.
code should look like this:
HTML
<nav>
<ul>
<li>HJEM</li>
<li>FORUM</li>
<li>DONER</li>
<li>SERVERE</li>
<li>FAQ</li>
<li>KONTAKT</li>
</ul>
</nav>
And CSS
nav {margin: 3px auto; width: 700px;}
nav ul {width: 700px; height: auto; list-style: none; margin:0 auto; display:block;}
nav ul li a {
background: none repeat scroll 0% 0% #FFF;
font-family: 'Open Sans',Arial,Helvetica,sans-serif;
font-weight: 300;
font-size: 18px;
text-align: center;
color: #717171;
text-decoration: none;
float: left;
padding: 8px 0px;
width: 106px;
}
nav ul li a:hover {background: #f1f1f1;}
http://jsfiddle.net/Sb42u/1/
1. To center your nav bar:
You just need to change margin: 3px 0; to margin: 3px auto in nav.
2. To create a DropDown menu:
First I would advise to change your markup this way:
<nav>
<ul>
<li>HJEM</li>
<li>FORUM</li>
<li>
DONER
<ul class="submenu">
<li>SERVERE
<li>FAQ</li>
<li>KONTAKT</li>
</ul>
</li>
</ul>
</nav>
Then you can simulate a dropdown using this css classes:
nav ul li{
position:relative;
float:left;
}
nav ul li ul.submenu {
position: absolute;
width: auto;
display:none;
top: 35px;
}
nav ul > li:hover > ul {
left: 0;
display: block;
}
Fiddle here: http://jsfiddle.net/9Yg47/4/
When I place the width of by web page as 100% it fits to the browser. But when I re-adjust the size of the browser the navigation bar I used overlaps and all the positions go wrong. I can't figure out the mistake yet. I will post the css and the html code below. Please someone help me to fix my web page.
HTML:
<div class="main">
<div class="header">
<div class="logo12">
<h2 style="position:absolute; left:20px; top:2px; color:#FFFFFF;">
<a>
<font face="verdana">PHIS</font>
<small>Public Health Information System-Sri Lanka</small>
</a>
</h2>
</div>
<div>
</div>
<div id="menu">
<ul>
<li>Home</li>
<li>
Services
<ul id="SubNav">
<li id="AccessFormPHI">Data Entry</li>
<li id="DataEntry">Edit Temporary Forms</li>
<li id="ViewData">View Data</li>
<li id="ViewAggregatedData">Data Aggregation</li>
</ul>
</li>
<li>Help</li>
<li>
Profile
<ul id="SubNav">
<li id="AccessForm">View and Maintain Profile</li>
<li id="ChangePassword">Change Password</li>
<li>Log Out</li>
</ul>
</li>
</ul>
</div>
</div>
CSS:
/* Height of the navigation bar became shorter */
.main {
width: 100%;
font: 13px Georgia, "verdana", "Times New Roman", Times, serif;
color: #FFFFFFF;
background-color: #F2F2F2;
height: 1000px;
left: 0px;
}
/* No difference was appeared*/
#cssmenu {
position: absolute;
top: 100px;
left: 0px;
}
/* Adjust the olor of the navigation bar */
.logo12 {
position: absolute;
padding-top: 10px;
/*padding-left: 60px; */
top: 0%;
height: 6%;
width: 100%;
left: 0px;
background: -moz-linear-gradient(top,#084B8A ,#0B173B);
}
/* Adjust the place where navigation */
#menu {
position: absolute;
left: 45%; /* change to 390px to 350px */
top: 20px;
display: inline;
}
#menu ul {
margin-left: auto;
margin-right: auto;
width :700px; /*change to 60% to 800px*/
}
/* upto now no difference */
#menu ul li {
display: inline;
float: left;
list-style: none;
position: relative;
height: 60px;
width: 130px;
padding-bottom: 20px;
font-size: 13px;
}
/* Adjust the width of the sub navigation bar */
#menu ul li ul li {
float: left;
list-style: none;
position: relative;
height: 30px;
width: 180px;
font-size: 12px;
}
/* Adjust the color of the sub navigation bar */
#menu li ul {
margin: 0px;
padding: 10px;
display: none;
position: absolute;
left: 0px;
z-index: 99;
top: 28px;
background: -moz-linear-gradient(top,#084B8A ,#0B173B); #726E6D; /*Menu Hover Color*/
border: 1;
}
#menu ul li:hover ul {
display: block;
width: auto;
}
/* Upto now no difference was visible */
#menu li li {
list-style: none;
}
/* Color of sub navigation bar letters became gray */
#menu li li a {
color: #FFFFFF;
text-decoration: none;
}
/* Letters hover color red in the sub navigation bar */
#menu li li a:hover {
color: #900;
text-decoration: none;
}
/* Upto now no difference */
li#main1 {
padding-top: 0px;
}
/* Main navigation bar disappeared */
#menu a {
padding-right: 25px;
text-decoration: none;
letter-spacing: -1px;
font-size: 1.2em;
color: #BDBDBD;
font: cambria;
}
/* Upto now no difference */
#menu ul li a:hover {
/* background: #D8D8D8; /*a tag hover color*/
padding: 5px; /*change to 12px to 5px*/
left: 0px;
}
You can use relative width for CSS,for example:
.main, .header{width:100%;}
Add white-space: nowrap to your navigation CSS. I know this is an old post, but seems to have no solution yet.