Prevent content form moving - html

I need some help with my code. I created a drop-down navigation menu. But when I hover over the sub-menus it pushes the main content of my website down. I don't want that. I want to be able to look at the sub-menus without infecting any of my main content. If I hover the menu it is pushing the other parts of the menu down. I like that, but when I tried to use position absolute, it isn't moving the other parts of the menu down anymore.
(Sorry for my bad English)
A part of the html code:
<nav class="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>Trajecten
<ul class="submenu">
<li>Sport</li>
<li>Techniek</li>
<li>Moderne Menia</li>
<li>Fast Lane English</li>
</ul>
</li>
<li>2017/18
<ul class="submenu">
<li>Examenreis Berlijn</li>
<li>Examenreis Londen</li>
<li>Examenreis Parijs</li>
<li>Introductie</li>
</ul>
</li>
<li>2016/17
<ul class="submenu">
<li>Diploma uitreiking Havo</li>
<li>Diploma uitreiking Mavo</li>
<li>Introductie</li>
<li>Open Dag</li>
</ul>
</li>
</ul>
</nav>
<center>
<p id="content">2017/18</p>
<img id="picture" src="images/2017,18/Berlijn.jpg">
</center>
Css code:
#content{
font-size: 25px;
position:relative;
top: 25px;
}
#picture{
width: 285px;
position:relative;
top: 30px;
}
html, body {
font-family: Arial, Helvetica, sans-serif;
}
/* Navigatie */
.navigation {
position: relative;
top: 100px;
width: 230px;
}
.mainmenu, .submenu {
list-style: none;
padding: 0;
margin: 0;
}
.mainmenu a {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
.mainmenu a:hover {
background-color: #C5C5C5;
}
.mainmenu li:hover .submenu {
display: block;
max-height: 420px;
}
.submenu a {
background-color: #999;
text-align: center;
}
.submenu a:hover {
background-color: #666;
}
.submenu {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.8s ease-out;
}

body{
display:flex;
flex-direction:row;
}
#content{
font-size: 25px;
}
#picture{
width: 285px;
}
html, body {
font-family: Arial, Helvetica, sans-serif;
}
/* Navigatie */
.navigation {
width: 230px;
}
.mainmenu, .submenu {
list-style: none;
padding: 0;
margin: 0;
}
.mainmenu a {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
.mainmenu a:hover {
background-color: #C5C5C5;
}
.mainmenu li:hover .submenu {
display: block;
max-height: 420px;
}
.submenu a {
background-color: #999;
text-align: center;
}
.submenu a:hover {
background-color: #666;
}
.submenu {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.8s ease-out;
}
center{
width:calc(100% - 230px);
display:flex;
flex-grow:1;
text-align:center;
flex-direction:column;
align-items: center;
}
<nav class="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>Trajecten
<ul class="submenu">
<li>Sport</li>
<li>Techniek</li>
<li>Moderne Menia</li>
<li>Fast Lane English</li>
</ul>
</li>
<li>2017/18
<ul class="submenu">
<li>Examenreis Berlijn</li>
<li>Examenreis Londen</li>
<li>Examenreis Parijs</li>
<li>Introductie</li>
</ul>
</li>
<li>2016/17
<ul class="submenu">
<li>Diploma uitreiking Havo</li>
<li>Diploma uitreiking Mavo</li>
<li>Introductie</li>
<li>Open Dag</li>
</ul>
</li>
</ul>
</nav>
<center>
<p id="content">2017/18</p>
<img id="picture" src="images/2017,18/Berlijn.jpg">
</center>
made a few changes,
now it's better centerd
you can make one of the menus open if you use js

Try this:
CSS
nav, center{
display: inline-block;
vertical-align: top;
}
center{
position: relative;
top: 100px;
}
Note: <center> tag is obsolete in HTML5
DEMO HERE

Related

Sub-menu doesn't appear on mouse hover

I am new to web design. I am trying to create a site where in some menus in menu bar have sub menus. I want on mouse hove it should display submenu which is not happening. This is my code:
#charset "UTF-8";
body {
margin: 0;
}
. wrapper {
height: 100vh;
}
nav {
height: 44px;
background: #323232;
text-align: center;
/* to center the UL in the nav */
}
nav ul {
display: flex;
max-width: 1200px;
justify-content: space-around;
align-items: center;
padding: 0;
margin: 0 auto;
/* 0 auto allows it to self-center in the nav */
list-style-type: none;
}
nav li {}
nav a {
display: inline-block;
height: 44px;
line-height: 44px;
color: white;
font-size: 15px;
font-weight: 100;
text-decoration: none;
transition: 0.3s;
}
nav a:hover {
color: #B8B8B8;
}
.dropdown ul {
position: absolute;
top: 43px;
z-index: 100;
visibility: hidden;
}
.dropdown ul li a {
background: none;
text-align: left;
display: block;
}
li li {
width: 100%;
}
.dropdown li:hover>ul {
display: block;
}
<div class="wrapper">
<nav>
<ul>
<li>Home</li>
<li class="dropdown"><a>Drinks</a>
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Special Diet</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
<div class="fft">Food For Thought</div>
<br>
<br>
<img src="Indian_Spices.jpg" alt="Spices" class="main_wrapper">
<!--<div class="main_wrapper" ></div>-->
On mouse hover on 'Drinks' nothing comes up. I want when I move mouse on 'Drikns' sub menus 'Pan Shots' and 'Tea' should be visible and should hide when mouse is not on 'Drinks'.
Your example is kinda messy and there's a lot of unnecessary code, i'm gonna present you with an example that can you work from.
* {
margin: 0;
padding: 0;
}
ul {
display: flex;
list-style: none;
}
ul>li {
flex: 1;
background: dodgerblue;
height: 45px;
text-align: center;
}
ul>li>a {
text-align: center;
line-height: 45px;
text-decoration: none;
color: #fff;
}
ul>li>ul {
display: none;
}
ul>li:hover>ul {
display: block;
}
.dropdown>a:after{
content:'▿';
font-weight:bold;
}
<ul>
<li>Home</li>
<li class="dropdown">Drinks
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Contact Us</li>
</ul>
You are mixing display and visibility. Your selector is wrong as well.
.dropdown li:hover>ul
Means that CSS is looking for an li child element of .dropdown to be hovered before something is done with the > ul
Since CSS properties are inherited your text is still white in a child element. Therefor you don't see the text.
Try the following:
#charset "UTF-8";
body {
margin: 0;
}
. wrapper {
height: 100vh;
}
nav {
height: 44px;
background: #323232;
text-align: center;
/* to center the UL in the nav */
}
nav ul {
display: flex;
max-width: 1200px;
justify-content: space-around;
align-items: center;
padding: 0;
margin: 0 auto;
/* 0 auto allows it to self-center in the nav */
list-style-type: none;
}
nav li {}
nav a {
display: inline-block;
height: 44px;
line-height: 44px;
color: white;
font-size: 15px;
font-weight: 100;
text-decoration: none;
transition: 0.3s;
}
nav a:hover {
color: #B8B8B8;
}
.dropdown ul {
position: absolute;
top: 43px;
z-index: 100;
visibility: hidden;
}
.dropdown ul li a {
background: none;
text-align: left;
display: block;
}
li li {
width: 100%;
}
.dropdown:hover ul {
visibility: visible;
}
.dropdown ul a {
color: black;
}
<div class="wrapper">
<nav>
<ul>
<li>Home</li>
<li class="dropdown"><a>Drinks</a>
<ul>
<li>Pan Shots</li>
<li>Tea</li>
</ul>
</li>
<li>Snacks</li>
<li>Desert</li>
<li>Special Diet</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
<div class="fft">Food For Thought</div>
<br>
<br>
<img src="Indian_Spices.jpg" alt="Spices" class="main_wrapper">
<!--<div class="main_wrapper" ></div>-->

Adjusting the CSS menu

Here is my menu test page with all the css:
body {
margin: 0;
}
.z-depth-0 {
box-shadow: none !important
}
nav {
width: 100%;
height: 56px;
color: #fff;
line-height: 56px;
background-color: rgb(238, 110, 115);
}
.dark-blue {
background-color: darkblue;
}
nav .nav-wrapper {
height: 100%;
position: relative;
top: 0;
}
.right {
float: right !important
}
#nav-mobile li {
display: inline-block;
margin: 0 1.5em 1.5em 1.5em;
}
#nav-mobile li a {
text-decoration: none;
color: white;
}
#nav-mobile li .dropdown-content {
display: none;
}
#nav-mobile li:hover .dropdown-content {
display: block;
}
#nav-mobile .dropdown-button .dropdown-content li a {
display: none;
}
#nav-mobile .dropdown-button:hover .dropdown-content li a {
display: block;
}
<nav class="dark-blue z-depth-0">
<div class="nav-wrapper container">
<ul id="nav-mobile" class="right">
<li><a class='dropdown-button' href='#'>Links</a>
<ul id="quicklinkdrop" class="dropdown-content">
<li>Home</li>
<li>Access</li>
<li>Zone</li>
</ul>
</li>
<li><a class='dropdown-button' href='#'><span>User</span></a>
<ul id="userdrop" class="dropdown-content">
<li>My Profile</li>
<li>Log Off</li>
</ul>
</li>
</ul>
</div>
</nav>
I want the first ul menu to be right aligned on the page and I want the dropdown menu to be displayed with items one under the other when user hovers over Links or User. Instead, the dropdown content gets displayed in line. Can anyone help me fix the menu?
Also I want Links and User to be displayed a little higher
You need to position your items as relative and absolute. Parent items get a position: relative; and children get a position: absolute;
Best of luck!
<!DOCTYPE html>
<html>
<head>
<title>Test Menu</title>
<style>
body {
margin: 0;
}
.z-depth-0{box-shadow:none !important}
nav {
width: 100%;
height: 56px;
color: #fff;
line-height: 56px;
background-color: rgb(238, 110, 115);
}
.dark-blue {
background-color: darkblue;
}
nav .nav-wrapper{
height: 100%;
position: relative;
top: 0;
}
.right {float: right!important}
#nav-mobile li {
display: inline-block;
margin: 0 1.5em 1.5em 1.5em;
}
#nav-mobile li a{
text-decoration: none;
color: white;
}
#nav-mobile li .dropdown-content{
display: none;
}
#nav-mobile li:hover .dropdown-content{
display: block;
}
#nav-mobile .dropdown-button .dropdown-content li a {
display: none;}
#nav-mobile .dropdown-button:hover .dropdown-content li a {
display: block;
}
ul#userdrop {
/* right: 0; */
/*left: 0;*/
margin-left: -100px;
}
#nav-mobile li .dropdown-content {
display: none;
position: absolute;
background: #000000;
}
#nav-mobile li a {
text-decoration: none;
color: white;
position: relative;
}
.right {
float: right!important;
/* position: absolute; */
/* right: 0; */
margin-top: 0px;
}
</style>
</head>
<body>
<nav class="dark-blue z-depth-0">
<div class="nav-wrapper container">
<ul id="nav-mobile" class="right">
<li><a class='dropdown-button' href='#'>Links</a>
<ul id="quicklinkdrop" class="dropdown-content">
<li>Home</li>
<li>Access</li>
<li>Zone</li>
</ul>
</li>
<li><a class='dropdown-button' href='#'><span>User</span></a>
<ul id="userdrop" class="dropdown-content">
<li>My Profile</li>
<li>Log Off</li>
</ul>
</li>
</ul>
</div>
</nav>
</body>
</html>
Ok to get the options to display top down you want to add
#quicklinkdrop li{
display:list-item;
}
#userdrop li{
display:list-item;
}
This should fix them being inline and put items on top of each other

CSS Animation on Sidebar Submenu

I've been trying to figure this out, but I'm honestly lost. I'm making a sidebar that has three levels: a main menu, with two more submenus when you hover over the menu items(so it's main menu item > submenu item > another submenu). The thing is, I'm not very good with CSS animations, but I'm trying to learn. And was wondering if this could be done with it?
Here's my code so far. Initially, I started this out with JS, but stackoverflow told me it'd be easier to do it with CSS animation. And honestly, this looks way cleaner. Anyway, currently, it shows the icon titles beside it when you hover over a sidebar. But how exactly should I go about making the submenu appear when you hover over each menu item separately? Should I do it by having a whole new div appear on hover, or should I do it by expanding the existing one(this doesn't seem like it'd be a good idea)?
What's the correct way? And on which elements should I add CSS transitions to achieve that smooth slidein sidebar?
/* .subnav {
position: fixed;
background-color: red;
margin-top: 0;
top: 75px;
left: 75px;
height: 100vw;
}
my poor attempt at submenu */
.sidebar {
margin-top: 75px;
position: fixed;
width:75px;
height:100vw;
top:0;
left:0;
z-index:100;
background-color: #292a2c;
color: #000;
overflow: hidden;
transition: width .5s;
}
.sidebar:hover {
width: 160px;
}
.sidebar ul {
width:200px;
}
.sidebar a i {
font-size: 30px;
}
.sidebar a {
color:#fff;
font-size:14px;
text-decoration:none;
}
.nav-item {
padding-left: 8px;
}
.nav-item:hover {
background-color: $primary-color;
}
span {
display: none;
font-size: 14px;
}
.sidebar:hover span {
display: initial;
}
<div class="sidebar">
<ul class="nav flex-column m-0">
<li class="nav-item py-2">
<a class="nav-link" href="#">
<i class="fa fa-envelope-open" aria-hidden="true"></i>
<span class="pl-1">
Item
</span>
</a>
<ul class="nav subnav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#">
Overview
<span class="sr-only">
(current)
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Reports</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Analytics</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Export</a>
</li>
</ul>
</li>
</ul>
</div>
Thank you!
I hope this will help you. Here is some modification in your previous code.
You can also check it on Jsfiddle
*
{
color: #fff;
}
/* .subnav {
position: fixed;
background-color: red;
margin-top: 0;
top: 75px;
left: 75px;
height: 100vw;
}
my poor attempt at submenu */
.sidebar {
margin-top: 75px;
position: fixed;
width:20px;
height:100vw;
top:0;
left:0;
z-index:100;
background-color: #292a2c;
color: #000;
overflow: hidden;
transition: width .5s;
}
.sidebar:hover {
width: 250px;
}
.sidebar ul {
width:200px;
}
.sidebar a i {
font-size: 30px;
}
.sidebar a {
color:#fff;
font-size:14px;
text-decoration:none;
}
.nav-item {
padding-left: 8px;
}
.nav-item:hover {
background-color: $primary-color;
}
span {
display: none;
font-size: 14px;
}
.sidebar:hover span {
display: initial;
}
/* reset our lists to remove bullet points and padding */
.mainmenu, .submenu, .submenu1 {
padding: 0;
margin: 0;
}
/* make ALL links (main and submenu) have padding and background color */
.mainmenu a {
display: block;
text-decoration: none;
padding: 10px;
}
/* add hover behaviour */
.mainmenu a:hover {
background-color: #C5C5C5;
}
.mainmenu li:hover .submenu {
display: block;
max-height: 200px;
}
.submenu li:hover .submenu1 {
display: block;
max-height: 200px;
}
.submenu a
{
}
.submenu li
{
padding-left: 15px;
}
.submenu1 a
{
}
.submenu1 li
{
padding-left: 20px;
}
/* hover behaviour for links inside .submenu */
.submenu a:hover {
background-color: #666;
}
/* this is the initial state of all submenus.
we set it to max-height: 0, and hide the overflowed content.
*/
.submenu,
.submenu1{
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.5s ease-out;
}
<div class="sidebar">
<ul class="nav flex-column m-0">
<li class="nav-item py-2">
<a class="nav-link" href="#">
<i class="fa fa-envelope-open" aria-hidden="true"></i>
<span class="pl-1">Item</span>
</a>
<nav class="navigation">
<ul class="mainmenu">
<li><span class="sr-only"></span>Home</li>
<li>About
<ul class="submenu">
<li>Team</li>
<li>Contact Us
<ul class="submenu1">
<li>Phone</li>
<li>Email</li>
<li>Fax</li>
</ul>
</li>
</ul>
</li>
<li>Our Products</li>
</ul>
</nav> </li>
</ul>
</div>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Multi-Level Drop Down Menu with Pure CSS</title>
<style>
ul {
list-style: none;
padding: 0;
margin: 0;
background: #1bc2a2;
}
ul li {
display: block;
position: relative;
float: left;
background: #1bc2a2;
}
/* This hides the dropdowns */
li ul { display: none; }
ul li a {
display: block;
padding: 1em;
text-decoration: none;
white-space: nowrap;
color: #fff;
}
ul li a:hover { background: #2c3e50; }
/* Display the dropdown */
li:hover > ul {
display: block;
position: absolute;
}
li:hover li { float: none; }
li:hover a { background: #1bc2a2; }
li:hover li a:hover { background: #2c3e50; }
.main-navigation li ul li { border-top: 0; }
/* Displays second level dropdowns to the right of the first level dropdown */
ul ul ul {
left: 100%;
top: 0;
}
/* Simple clearfix */
ul:before,
ul:after {
content: " "; /* 1 */
display: table; /* 2 */
}
ul:after { clear: both; }
</style>
</head>
<body>
<ul class="main-navigation">
<li>Front End Design
<ul>
<li>HTML</li>
<li>CSS
<ul>
<li>Resets</li>
<li>Grids</li>
<li>Frameworks</li>
</ul>
</li>
<li>JavaScript
<ul>
<li>Ajax</li>
<li>jQuery</li>
</ul>
</li>
</ul>
</li>
</ul>
<h1>Multi-Level Drop Down Menu with Pure CSS</h1>
<div class="css-script-ads"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2783044520727903";
/* CSSScript Demo Page */
google_ad_slot = "3025259193";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46156385-1', 'cssscript.com');
ga('send', 'pageview');
</script>
</body>
</html>
Hope it helps you. Please check it out and let me know.
Just modified your css a bit. Check if this helps. jsfiddle
.subnav {
position: fixed;
background-color: red;
margin-top: 0;
top: 75px;
left: 75px;
height: 100vw;
opacity:0;
overflow:hidden;
}
.sidebar {
margin-top: 75px;
position: fixed;
width:75px;
height:100vw;
top:0;
left:0;
z-index:100;
background-color: #292a2c;
color: #000;
overflow: hidden;
transition: width .5s;
}
.sidebar:hover {
width: 160px;
}
.sidebar .subnav
{
transform:75px;
transition: transform .5s;
}
.sidebar:hover .subnav{
transform: translateX(85px)
}
.sidebar a i {
font-size: 30px;
}
.sidebar a {
color:#fff;
font-size:14px;
text-decoration:none;
}
.nav-item {
padding-left: 8px;
}
.nav-item:hover {
background-color: $primary-color;
}
span {
display: none;
font-size: 14px;
}
.sidebar:hover span {
display: initial;
}
.nav-item:hover .subnav {
opacity: 1;
}

CSS Vertical dropdown menu, When Hover to other menu the submenu shows

What is the proper or best way to do this? I can't fix it :3
The submenu of members shows when you hover to the other menus
What's the problem with my code? I can't figure it out :3
you can see my codes in the link
http://codepen.io/anon/pen/MaxmvO
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header>
<div class="logo">WORKOUT <span>FITNESS CENTER</span></div>
</header>
<div id="container">
<nav>
<ul>
<li>Walk-In</li>
<li>Members</li>
<ul>
<li>List of Members
<li>Subscr</li>
<li>asdasd</li>
</ul>
</li>
<li>Sales</li>
<li>Inventory</li>
<li>Suppliers</li>
<li>Reports</li>
</ul>
</nav>
<div id="content">
SOME CONTENT YAY
</div>
</div>
</body>
</html>
CSS
#import url(https://fonts.googleapis.com/css?family=Open+Sans:400italic,400,300italic,300);
*{
margin: 0;
padding: 0;
}
body {
font-family: 'Open Sans';
}
a{
text-decoration: none;
}
header{
width: 100%;
height: 50px;
border-bottom: 1px solid #858585;
}
.logo {
float: left;
margin-top: 9px;
margin-left: 15px;
}
.logo a{
font-size: 1.3em;
color: #070807;
}
.logo a span{
font-weight: 300;
color: #1AC93A;
}
nav{
width: 250px;
height: calc(100% - 50px);
background-color: #171717;
float: left;
}
#content {
width: :auto;
margin-left: 250px;
height: calc(100% - 50px);
padding: 15px
}
nav li{
list-style: none;
}
nav li a{
color: #ccc;
display: block;
padding: 10px;
font-size: 0.8em;
border-bottom: 1px solid #0a0a0a;
-webkit-transition: 0.2s;
-moz-transition: 0.2s;
-o-transition: 0.2s;
transition: 0.2s;
}
nav li a:hover{
background-color: #030303;
color: #fff;
padding-left: 80px;
}
nav ul ul {
display: none;
}
nav ul:hover ul{
display: block;
}
Try this http://codepen.io/anon/pen/gaEWJw
HTML
<nav>
<ul>
<li>Walk-In</li>
<li>Members</li>
<li>List of Members
<ul>
<li>Subscr</li>
<li>asdasd</li>
</ul>
</li>
<li>Sales</li>
<li>Inventory</li>
<li>Suppliers</li>
<li>Reports</li>
</ul>
</nav>
CSS
nav ul > li > ul {
display: none;
}
nav ul li:hover ul{
display: block;
}
You need to target the specific element and then show the sub menu. I added an id to this li to make it easier to target and got rid of the </li> so the ul is inside li#members.
<li id="members">Members
<ul>
<li>List of Members</li>
<li>Subscr</li>
<li>asdasd</li>
</ul>
</li>
nav #members:hover ul{
display: block;
}

how to add sub menus on a responsive css menu

I have this html for my css menu:
<nav class="clearfix">
<ul class="clearix">
<li>Homepage</li>
<li>Services</li>
<li>Project Gallery</li>
<li>Contact Us</li>
</ul>
Menu
</nav>
nav {
height: 50px;
width: 100%;
background: #F00;
font-size: 14pt;
font-family: Arial;
position: relative;
border-bottom: 5px solid #FFFFFF;
}
nav ul {
padding: 0;
margin: 0 auto;
width: 100%;
height: 50px;
text-align: center;
}
nav li {
display: inline;
}
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
nav a {
color: #FFFFFF;
display: inline-block;
width: auto;
text-align: center;
text-decoration: none;
line-height: 50px;
}
nav li a {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
padding-left: 10px;
padding-right: 10px;
}
nav li:last-child a {
border-right: 0;
}
nav a:hover, nav a:active {
background-color: #000000;
color:#FFFFFF;
}
nav a#pull {
display: none;
}
#media screen and (max-width: 600px) {
nav {
height: auto;
}
nav ul {
width: 100%;
display: block;
height: auto;
}
nav li {
width: 50%;
float: left;
position: relative;
}
nav li a {
border-bottom: 1px solid #FFFFFF;
border-right: 1px solid #FFFFFF;
}
nav a {
text-align: left;
width: 100%;
text-indent: 25px;
}
}
#media only screen and (max-width : 480px) {
nav {
border-bottom: 0;
}
nav ul {
display: none;
height: auto;
}
nav a#pull {
display: block;
color:#FFFFFF;
background-color: #F00;
width: 100%;
position: relative;
}
nav a#pull:after {
content:"";
background: url('nav-icon.png') no-repeat;
width: 30px;
height: 30px;
display: inline-block;
position: absolute;
rightright: 15px;
top: 10px;
}
}
#media only screen and (max-width : 320px) {
nav li {
display: block;
float: none;
width: 100%;
}
nav li a {
border-bottom: 1px solid #FFFFFF;
}
}
I am looking for a way to add sub menus and then second sub menus on on the first ones but still keep it as responsive as it is.
How can I do this?
http://jsfiddle.net/EYjnG/
JSFIDDLE DEMO
logic is just simple and have with this code
#submenu,#submenu2,#submenu3{
visibility:hidden; /*turn all the submenus visibility hidden */
}
#top_menu li.first:hover #submenu,#submenu li.second:hover #submenu2,#submenu2 li.second:hover #submenu3{
visibility:visible; /*On hover turn on visibility visible */
}
Complete code :
HTML:
<div id="top_menu"> <!--MAIN MENU -->
<ul>
<li class="first">menu1
<div id="submenu"> <!--First Submenu -->
<ul class="abc">
<li class="second">item1
<div id="submenu2"> <!--Second Submenu -->
<ul class="abc">
<li class="second">item1_1
<div id="submenu3"> <!--Third Submenu -->
<ul class="abc">
<li class="second">item1_1_1</li>
<li class="second">item1_1_2</li>
<li class="second">item1_1_3</li>
</ul>
</div> <!--third Submenu Ends here-->
</li>
<li class="second">item1_2</li>
<li class="second">item1_3</li>
</ul>
</div> <!--Second Submenu Ends here-->
</li>
<li class="second">item2
<div id="submenu2">
<ul class="abc">
<li class="second">item2_1</li>
<li class="second">item2_2</li>
<li class="second">item2_3</li>
</ul>
</div>
</li>
<li class="second">item3
<div id="submenu2">
<ul class="abc">
<li class="second">item3_1</li>
<li class="second">item3_2</li>
<li class="second">item3_3</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="first">menu2
<div id="submenu">
<ul class="abc">
<li class="second">item1</li>
<li class="second">item2</li>
<li class="second">item3</li>
<li class="second">item4</li>
</ul>
</div>
</li>
</ul>
</div>
CSS:
ul{
padding:10px;
padding-right:0px;
}
li.first{
display:block;
display:inline-block;
padding:5px;
padding-right:25px;
padding-left:25px;
cursor:pointer;
}
li.second{
list-style:none;
margin:0px;
padding:5px;
padding-right:25px;
margin-bottom:5px;
cursor:pointer;
}
#submenu li.second:hover{
background:red;
border-radius:5px;
}
#submenu2 li.second:hover{
background:green;
border-radius:5px;
}
/*********MAIN LOGIC***************/
#submenu,#submenu2,#submenu3{
visibility:hidden;
}
#top_menu li.first:hover #submenu,#submenu li.second:hover #submenu2,#submenu2 li.second:hover #submenu3{
visibility:visible;
}
/**********STYLING SUBMENUS**************/
#submenu{
padding-right:0px;
text-align:left;
position:absolute;
background:white;
box-shadow:0px 0px 5px;
border-radius:5px;
}
#submenu2{
text-align:left;
position:absolute;
left:70px;
top:0px;
background:red;
box-shadow:0px 0px 5px;
border-radius:5px;
}
#submenu3{
text-align:left;
position:absolute;
left:80px;
top:0px;
background:green;
box-shadow:0px 0px 5px;
border-radius:5px;
}
just understand the logic behind this code and you can made as many submenus as you want.
There are many ways to go ahead about this.
I usually hide the sub menu uls with display: none and take them out of the content flow with position: absolute. Give the li containing the sub menu position: relative so that the sub menus are relative to their direct parents, then position the sub menus however you please using the top, right, bottom and left properties. Finally, change the sub menu to display: block through a :hover or whatever.
Here's a bare-bones example of this:
Markup:
<nav>
<ul>
<li><a>Link</a>
<ul>
<li><a>Sub link</a></li>
<li><a>Sub link</a></li>
<li><a>Sub link</a></li>
</ul>
</li>
</ul>
</nav>
CSS:
nav li {
position: relative;
}
nav li > ul {
position: absolute;
top: 100%;
display: none;
}
nav li:hover > ul {
display: block;
}
Here's a pen with this example. It looks like crap but you get the drill.
You can just keep nesting more sub-menus, but you'll probably want to use different positioning for second-and-lower-levels of sub menus.
However, please note that mobile browsers don't really support :hover. At least they don't treat it the same. You shouldn't make your sub menus accessible only on :hover. Consider adding some sort of class name toggle on click with javascript instead.
use hover in css like:
a:hover
or if your div id is "div1":
#div1:hover
I am not 100% sure if your asking how to make the id menu have a menu functionality or just a sub menu for your main nav.
If it is pertaining to a sub menu for your main nav then this will work just fine. If it's for the mobile menu then let me know and I'll work something out for you. (SOLVED)
This fiddle has the sub menu working while still being responsive the entire time. You can style it to your needs but it is a solid start.
nav ul li ul {
display: none;
position: absolute;
width: 100%;
top: 100%;
background: #000;
color: #fff;
}
nav ul li:hover ul {
display: block;
}
nav ul li ul li {
display: block;
-webkit-transition: .6s ease;
-moz-transition: .6s ease;
-ms-transition: .6s ease;
-o-transition: .6s ease;
}
nav ul li ul li:hover {
background: #c1c1c1;
color: #2b2b2b;
}
JSFIDDLE
JSFIDDLE with relative sized sub menu
Here is the mobile navigation working and the biggest problem was you had no jQuery library selected for the fiddle to run off of.
Mobile Nav
HTML
<div id="pull"><span>Menu</span>
</div>
CSS
div span {
color: #FFFFFF;
display: inline-block;
width: auto;
text-align: center;
text-decoration: none;
line-height: 50px;
padding-left: 10px;
padding-right: 10px;
}
I changed the id pull to a div because when it was an anchor tag all of the navs would lose their text color.
I have made a drop-down with a drop-down in it while still being responsive! Take a peak at this jsfiddle.
JSFIDDLE Drop-Down with a nested Drop-Down
Here's my take: http://codepen.io/teodragovic/pen/rmviJ
HTML
<nav>
<input type="checkbox" id="nav-toggle-1" />
<label for="nav-toggle-1" class="pull sub"><a>Menu</a></label>
<ul class="lvl-1">
<li>Homepage</li>
<li>
<input type="checkbox" id="nav-toggle-2" />
<label for="nav-toggle-2" class="sub"><a>Services</a></label>
<ul class="lvl-2">
<li>Service 1</li>
<li>Service 2</li>
<li>
<input type="checkbox" id="nav-toggle-3" />
<label for="nav-toggle-3" class="sub"><a>Service 3</a></label>
<ul class="lvl-3">
<li>Service 3 a</li>
<li>Service 3 b</li>
</ul>
</li>
<li>Service 4</li>
</ul>
</li>
<li>Project Gallery</li>
<li>Contact Us</li>
</ul>
</nav>
CSS
#import "compass";
/* globals */
* {box-sizing:border-box;}
ul {
margin: 0;
padding: 0;
}
input {
position: absolute;
top: -99999px;
left: -99999px;
opacity: 0;
}
nav {
height: 50px;
background: #F00;
font: 16px/1.5 Arial, sans-serif;
position: relative;
}
a {
color: #FFFFFF;
display: inline-block;
text-decoration: none;
line-height: 50px;
padding: 0 20px;
&:hover,
&:active {
background-color: #000000;
color:#FFFFFF;
}
}
/* nav for +600px screen */
ul.lvl-1 {
text-align: center;
#include pie-clearfix;
li {
display: inline;
}
}
ul.lvl-2,
ul.lvl-3 {
position: absolute;
width: 100%;
background: lighten(red, 15%);
display:none;
}
ul.lvl-3 {background: lighten(red, 30%);}
#nav-toggle-2:checked ~ ul.lvl-2,
#nav-toggle-3:checked ~ ul.lvl-3 {
display: block;
}
.pull {display: none;}
/* arrow thingy - crappy positioning, needs tinkering */
.sub {
position: relative;
cursor: pointer;
&:after {
position: absolute;
top: 40%;
right: 0;
content:"";
width: 0;
height: 0;
border-right: 6px solid transparent;
border-left: 6px solid transparent;
border-top: 6px solid white;
}
}
/* medium-ish nav */
#media screen and (max-width: 600px) {
nav {height: auto;}
a {
text-align: left;
width: 100%;
text-indent: 25px;
border-bottom: 1px solid #FFFFFF;
}
ul > li {
width: 50%;
float: left;
&:nth-of-type(odd) {
border-right: 1px solid white;
}
}
li:nth-of-type(even) ul.lvl-2,
li:nth-of-type(even) ul.lvl-3 {
position: relative;
width: 200%;
left: -100%;
}
li:nth-of-type(odd) ul.lvl-2,
li:nth-of-type(odd) ul.lvl-3 {
position: relative;
width: 200%;
left: 1px;
}
ul.lvl-2 li {background: lighten(red, 15%);}
ul.lvl-3 li {background: lighten(red, 30%);}
}
/* small-ish nav */
#media only screen and (max-width : 480px) {
.pull {
display: block;
width: 100%;
position: relative;
}
ul {
height: 0;
> li {
width: 100%;
&:nth-of-type(odd) {
border-right: none;
}
}
}
#nav-toggle-1:checked ~ ul.lvl-1 {
height: auto;
}
#nav-toggle-2:checked ~ ul.lvl-2,
#nav-toggle-3:checked ~ ul.lvl-3 {
//reverting stuff from previous breakpoint
left: 0;
width: 100%;
}
}
Markup is little modified from original since I find it easier to use classes than general selectors, especially when nesting lists.
It's CSS only (I'm using SASS+compass). :checked pseudo-class are used for toggling menus on and off. I removed link for services assuming that it will be used just for opening sub-menu (content-wise, you could always add something like "all services" in submenu if you want to keep that page in navigation).
Biggest challenge was styling middle breakpoint. Depending on position of parent list item (odd or even), child list is stretched to 200% (because parent is 50% wide) and positioned so it floats from left side. Small bug appears on level 3 regarding width of the list causing color bleed on edges.
Additionaly, display: block and display:none selectors could be replaced with max-height to add some cool css animation effects