CSS3 Responsive Menu
Today I’m going to tell how to create a responsive navigation menu using only CSS3.
Join the DZone community and get the full member experience.
Join For Free
today i’m going to tell how to create a responsive navigation menu using only css3. why responsive? i think it is important and essential. as you know, today, many people browse the internet via mobile devices (such as ipad, iphone or android). and it is important that mobile members could navigate through your website. some solutions which you can find in internet offer you to use jquery or javascript to achieve a necessary behavior. but today i will give you a solution without the use of javascript.
by default, this is usual ul-li drop-down menu. but, if our screen is small (in case of mobile browsers), this menu turns into a click-based menu. in this case your visitors will be able to click to top elements of menu to open submenus. please look how it works:
live demo
download in package
step 1. html
here is the markup for the demo page with our responsive menu:
index.html
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta charset="utf-8" />
<meta name="author" content="script tutorials" />
<title>responsive menu | script tutorials</title>
<!-- add styles -->
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<ul id="nav">
<li><a href="#">home</a></li>
<li><a href="#s1">menu 1</a>
<span id="s1"></span>
<ul class="subs">
<li><a href="#">header a</a>
<ul>
<li><a href="#">submenu x</a></li>
<li><a href="#">submenu y</a></li>
<li><a href="#">submenu z</a></li>
</ul>
</li>
<li><a href="#">header b</a>
<ul>
<li><a href="#">submenu x</a></li>
<li><a href="#">submenu y</a></li>
<li><a href="#">submenu z</a></li>
</ul>
</li>
</ul>
</li>
<li class="active"><a href="#s2">menu 2</a>
<span id="s2"></span>
<ul class="subs">
<li><a href="#">header c</a>
<ul>
<li><a href="#">submenu x</a></li>
<li><a href="#">submenu y</a></li>
<li><a href="#">submenu z</a></li>
</ul>
</li>
<li><a href="#">header d</a>
<ul>
<li><a href="#">submenu x</a></li>
<li><a href="#">submenu y</a></li>
<li><a href="#">submenu z</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">menu 3</a></li>
<li><a href="#">menu 4</a></li>
<li><a href="#">menu 5</a></li>
<li><a href="http://www.script-tutorials.com/css3-responsive-menu/">back to responsive menu tutorial</a></li>
</ul>
</div>
</body>
</html>
you can notice here one trick – meta with name=’viewport’. this tag is required to scale page content inside your screen properly. at can be any screen – of your monitor or a screen of your mobile device. rest code should be easy to understand – multilevel ul-li menu.
step 2. css
now – styles. in the most beginning i defined base styles for our page:
css/main.css
* {
margin: 0;
padding: 0;
}
html {
background-color: #fff;
height: 100%;
}
body {
color: #333333;
font: 0.75em/1.5em arial,sans-serif;
}
.container {
margin-left: auto;
margin-right: auto;
margin-top: 30px;
width: 96%;
}
now, we can define styles for top level elements:
/* common and top level styles */
#nav span {
display: none;
}
#nav, #nav ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
#nav {
background-color: #f5f5f5;
border-bottom: 5px solid #333333;
float: left;
margin-left: 1%;
margin-right: 1%;
position: relative;
width: 98%;
}
#nav ul.subs {
background-color: #ffffff;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
color: #333333;
display: none;
left: 0;
padding: 2%;
position: absolute;
top: 54px;
width: 96%;
}
#nav > li {
border-bottom: 5px solid transparent;
float: left;
margin-bottom: -5px;
text-align: left;
-moz-transition: all 300ms ease-in-out 0s;
-ms-transition: all 300ms ease-in-out 0s;
-o-transition: all 300ms ease-in-out 0s;
-webkit-transition: all 300ms ease-in-out 0s;
transition: all 300ms ease-in-out 0s;
}
#nav li a {
display: block;
text-decoration: none;
-moz-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-ms-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-o-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
-webkit-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
white-space: normal;
}
#nav > li > a {
color: #333333;
display: block;
font-size: 1.3em;
line-height: 49px;
padding: 0 15px;
text-transform: uppercase;
}
#nav > li:hover > a, #nav > a:hover {
background-color: #f55856;
color: #ffffff;
}
#nav li.active > a {
background-color: #333333;
color: #ffffff;
}
and now – styles for drop down (submenu):
/* submenu */
#nav li:hover ul.subs {
display: block;
}
#nav ul.subs > li {
display: inline-block;
float: none;
padding: 10px 1%;
vertical-align: top;
width: 33%;
}
#nav ul.subs > li a {
color: #777777;
line-height: 20px;
}
#nav ul li a:hover {
color: #f55856;
}
#nav ul.subs > li > a {
font-size: 1.3em;
margin-bottom: 10px;
text-transform: uppercase;
}
#nav ul.subs > li li {
float: none;
padding-left: 8px;
-moz-transition: padding 150ms ease-out 0s;
-ms-transition: padding 150ms ease-out 0s;
-o-transition: padding 150ms ease-out 0s;
-webkit-transition: padding 150ms ease-out 0s;
transition: padding 150ms ease-out 0s;
}
#nav ul.subs > li li:hover {
padding-left: 15px;
}
well done. now, the most interesting part – how can we apply responsive rules? actually, this is easy, we can use media queries (css3) which is made to define custom styles for certain screen resolutions (breakpoints). as you know, different mobile devices have different screen resolution, so – we can use it to define our own custom styles. i’m going to turn our dropdown menu into click-action menu using another one simple trick. did you notice hidden span elements which i put right after top level menu elements? i’m going to pass active state to them (:target pseudo element) by clicking on top level elements, it will open sub-levels then. please look what i did:
/* responsive rules */
@media all and (max-width : 980px) {
#nav > li {
float: none;
border-bottom: 0;
margin-bottom: 0;
}
#nav ul.subs {
position: relative;
top: 0;
}
#nav li:hover ul.subs {
display: none;
}
#nav li #s1:target + ul.subs,
#nav li #s2:target + ul.subs {
display: block;
}
#nav ul.subs > li {
display: block;
width: auto;
}
}
live demo
download in package
conclusion
we have just made the great responsive menu for your website from scratch. i’m sure that it will be very useful for you. good luck and welcome back
Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Understanding Dependencies...Visually!
-
The SPACE Framework for Developer Productivity
-
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
-
Database Integration Tests With Spring Boot and Testcontainers
Comments