Menu

How TO – Responsive Sidebar


Previous


Next

Learn how to create a responsive side navigation menu with CSS.






Home
News
Contact
About

Responsive Sidebar Example

This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.

We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.

Resize the browser window to see the effect.


Try it Yourself

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<style>
body {
margin: 0;
font-family: “Lato”, sans-serif;
}

.sidebar {
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}

.sidebar a {
display: block;
color: black;
padding: 16px;
text-decoration: none;
}

.sidebar a.active {
background-color: #04AA6D;
color: white;
}

.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}

div.content {
margin-left: 200px;
padding: 1px 16px;
height: 1000px;
}

@media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
}

@media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
</style>
</head>
<body>

<div>
<a href=”#home”>Home</a>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>
<a href=”#about”>About</a>
</div>

<div>
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>

</body>
</html>

Create a Responsive Sidebar

Step 1) Add HTML:

Example

<!– The sidebar –>
<div class=”sidebar”>
  <a class=”active” href=”#home”>Home</a>
  <a href=”#news”>News</a>
  <a href=”#contact”>Contact</a>
  <a href=”#about”>About</a>
</div>

<!– Page content –>
<div class=”content”>
  ..
</div>

Step 2) Add CSS:

Example

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<style>
body {
margin: 0;
font-family: “Lato”, sans-serif;
}

.sidebar {
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}

.sidebar a {
display: block;
color: black;
padding: 16px;
text-decoration: none;
}

.sidebar a.active {
background-color: #04AA6D;
color: white;
}

.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}

div.content {
margin-left: 200px;
padding: 1px 16px;
height: 1000px;
}

@media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
}

@media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
</style>
</head>
<body>

<div>
<a href=”#home”>Home</a>
<a href=”#news”>News</a>
<a href=”#contact”>Contact</a>
<a href=”#about”>About</a>
</div>

<div>
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>

</body>
</html>


Previous


Next

Scroll to Top