remove hover on submenu during media query - html

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;
}
}

Related

HTML navbar keeps disappearing and moving around the page whenever i zoom in or resize the window

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;
}

Hovering over navigation bar

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>

Responsive Dropdown Menu Navigation Position Next To Logo CSS & HTML

I have a problem and its been killing me, been following various tutorials to create my navigation bar for my website but cant seem to get it right. The problem i have is that i cant position my navigation to be next to my logo when the screen is large. Everytime i shrink it down then click my menu button it appears in the same position as the last screen so im unable to see it.
Any help from experiance users will be much appreciated, the CSS & HTML i have is below.
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:0;
position: fixed;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #19c589;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #19c589;
color: #fff;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #fff;
background: #19c589;
text-align: center;
padding: 10px 0;
display: none;
}
/*Hide checkbox*/
input[type=checkbox]{
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
display: block;
}
/*Responsive Styles*/
#media screen and (max-width : 760px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display:block;
}
}
#nav_wrapper{
}
#logo img{
max-width: 100%;
height: auto;
}
#nav{
background-color: #222;
width: 100%;
top:0px;
left:0px
}
<div id="pageWrapper"> <!-- page wrapper-->
<div id="nav"> <!-- nav-->
<div id="logo">
<img src="http://placehold.it/350x150" width="405" height="96" align="bottom" /></div>
<div id="nav_wrapper"> <!-- nav wrapper-->
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li>Home</li>
<li>
About ↓
<ul class="hidden">
<li>Who We Are</li>
<li>What We Do</li>
</ul>
</li>
<li>
Portfolio ↓
<ul class="hidden">
<li>Photography</li>
<li>Web & User Interface Design</li>
<li>Illustration</li>
</ul>
</li>
<li>News</li>
<li>Contact</li>
</ul>
</div>
<!-- end of nav wrapper-->
</div> <!-- end of nav-->
</div> <!-- end of page wrapper-->
Follow these steps:
remove position: fixed; from your ul
use float: left on logo.
use float: right on nav.
code:
#logo {
float: left;
}
#nav_wrapper {
float: right;
}
and for smaller resolution:
#media screen and (max-width : 760px){
#logo, #nav_wrapper {
float: none;
}
}
#logo {
float: left;
}
#nav_wrapper {
float: right;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:0;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #19c589;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #19c589;
color: #fff;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #fff;
background: #19c589;
text-align: center;
padding: 10px 0;
display: none;
}
/*Hide checkbox*/
input[type=checkbox]{
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
display: block;
}
/*Responsive Styles*/
#media screen and (max-width : 760px){
#logo, #nav_wrapper {
float: none;
}
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display:block;
}
}
#nav_wrapper{
}
#logo img{
max-width: 100%;
height: auto;
}
#nav{
background-color: #222;
width: 100%;
top:0px;
left:0px
}
<div id="pageWrapper"> <!-- page wrapper-->
<div id="nav"> <!-- nav-->
<div id="logo">
<img src="http://placehold.it/350x150" width="405" height="96" align="bottom" /></div>
<div id="nav_wrapper"> <!-- nav wrapper-->
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li>Home</li>
<li>
About ↓
<ul class="hidden">
<li>Who We Are</li>
<li>What We Do</li>
</ul>
</li>
<li>
Portfolio ↓
<ul class="hidden">
<li>Photography</li>
<li>Web & User Interface Design</li>
<li>Illustration</li>
</ul>
</li>
<li>News</li>
<li>Contact</li>
</ul>
</div>
<!-- end of nav wrapper-->
</div> <!-- end of nav-->
</div> <!-- end of page wrapper-->
1st of all you have to remove the max-width for the logo and the auto height as well, also you'll need to float the image container "DIV" to the left to float the navigation menu to its right, so you will need to do it like the following
CSS:
#nav_wrapper{
overflow:auto;
}
#logo img{
float:left;
}
the rest will be the same :)

Responsive Nav Bar will not sit centrally in DIV

I've done lots of searching and testing on possible solutions, but can't get this responsive menu to sit central.
/**********HEADER ***************************/
.headerbar {
display:inline-block;
width:100%;
background:#007700;
margin-bottom:0px;
}
/******************/
/*********** NAVBACKER *********************/
.navback {
display:block;
height:32px;
border:2px solid grey;
background:grey;
margin:auto;
}
/***************/
/* START OF NAV STYLES ****************************/
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:0;
position: absolute;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 30px;
text-align: center;
line-height: 30px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #003300;
text-decoration: none;
border:1px solid green;
}
/*Hover state for top level links*/
li:hover a {
background: #006600;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 30px;
line-height: 30px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #006600;
color: #fff;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #fff;
background: #003300;
text-align: center;
padding: 10px 0;
display: none;
}
/*Hide checkbox*/
input[type=checkbox]{
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
display: block;
}
/*Responsive Styles*/
#media screen and (max-width : 760px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display:block;
height:12px;
}
}
/* END OF NAV STYLES ****************
<div class="headerbar">
<h1>Logo and some text</h1>
</div>
<div class="navback">
<!-- START OF NAVIGATION MENU -->
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li>Home</li>
<li>
Latest ↓
<ul class="hidden">
<li>Pictures</li>
<li>Comments</li>
<li>Threads</li>
</ul>
</li>
<li>
Explore ↓
<ul class="hidden">
<li>Counties</li>
<li>Towns</li>
<li>Attractions</li>
<li><a href='#'>Picture Tours</a></li>
</ul>
</li>
<li>Upload</li>
<li>Login</li>
</ul>
</div>
<!-- END OF NAVIGATION MENU -->`enter code here`
Here is where I have been trying to get it to work: http://jsfiddle.net/
Every time I change the position:absolute, the menu when hovered, formats wrong. I've also tried margin:0 auto; etc but nothing I've tried will work, and I've tried removing the float. Any help is hugely appreciated.
Tree
Put
display: table;
margin: 0 auto;
in ul tag, like this:
ul {
list-style-type:none;
margin: 0;
padding:0;
display: table;
margin: 0 auto;
}
Here is fiddle - http://jsfiddle.net/p5oypc72/70/

Center navigation bar

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/