惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
How to create a responsive sidebar menu using CSS
2022-12-03 · via CSS Articles by Temani Afif

How to create a responsive sidebar menu using CSS

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In a previous article, we explored different techniques to create a responsive menu where we only used CSS. We build the hamburger and close icons and used a checkbox trick to toggle the visibility of the menu on small screens.

Here is one of the demos we did (Reduce your screen size to test the responsive part)

In this article, we are going to build a responsive sidebar menu. I will be reusing the same code structure as the previous article so it’s highly recommended to read it before this one to fully understand all the tricks.

The HTML markup

<nav>
  <input type="checkbox" role="button" aria-label="Display the menu" aria-expanded="false" aria-controls="menu">
  <ul id="menu">
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/shop">Shop</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

The HTML structure is the same as the previous article. The idea is to be able to choose between different designs while keeping the same markup. All we have to do is to change the CSS.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

Let’s start with the basic structure of our menu.

Below is the relevant part of the code. I am creating a two-column grid configuration inside the body element then we style the <nav> element in a way to have the menu items centered under each other.

body {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 20px;
  margin: 0;
}
ul {
  padding: 20px 0;
  margin: 0;
  min-height: 100vh;  
  min-width: 180px;
  background: teal;
  display: flex;
  flex-direction: column;
  align-items: center;
  list-style: none;
}
ul li a {
  padding: 10px 20px;
  display: block;
}

Nothing complex so far and at this step the <input> element is hidden.

Now, we will add the responsive part. Under a specific screen width, we hide the menu and turn our layout into a single-column layout. Then we show the <input> as a fixed element at the bottom right. Also, note that most of the code is taken from the previous article especially the one related to styling the <input> element.

If you reduce the screen size, the menu will disappear and you will have the hamburger icon to toggle its visibility. Here is the relevant code:

nav input {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 40px;
  aspect-ratio: 1;
  padding: 20px;
  display: none;
  overflow: hidden;
  box-sizing: content-box;
  background: teal;
  border-radius: 50%;
}
@media (max-width:600px) {
  body {
    grid-template-columns: 1fr; /* one column setting */
  }
  nav {
    position: fixed; /* make the menu fixed position */
    top: 0;
    left: 0;
  }
  nav ul{
    transform: translateX(-100%); /* we slide the menu to the left */
    transition: .5s;
  }
  nav input:checked + ul {
    transform: translateX(0); /* we show it on checked */
  }
  nav input {
    display: grid; /* input visible only on small screen */
  }
}

That’s it! We already have our responsive sidebar menu. We only need a few updates to turn a classic menu into a sidebar menu.

Starting from this code we can do different adjustments to create variations of the same menu and add a few more effects.

We can for example add a semi-transparent overlay above the content when the menu is opened. For this, we can consider a simple box-shadow applied to the <ul> element like the below:

nav ul{
  box-shadow: 0 0 0 100vmax #0000; /* a big transparent box-shadow */
}
nav input:checked + ul {
  box-shadow: 0 0 0 100vmax #0005; /* we update color on checked to a semi transparent one */
}

We can also consider a different kind of animation where we make the menu appear from the top and cover the screen.

All I did is little updates to the code to get a new animation:

nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0; /* we add right to cover the whole screen */
}
nav ul{
  transform: translateY(-100%); /* we consider translateY instead of X */
  justify-content: center; /* we center the content vertically */
  transition: .5s;
}
nav input:checked + ul {
  transform: translateY(0);
}

Here is another idea where this time I will update the position of the hamburger icon to make it on the bottom left side and it will slide with the menu.

Here is a figure to understand the adjustments I made to the icon:

Illustration of the close menu icon

Using left/right/bottom and height I make the input full width of the <nav> element but it will extend by 70px from the right side. Then I define a two-column layout inside it. One on the right with a dimension equal to 40px (where I will place the icons) and the left one will fill the remaining space.

Similar to the way I animate the menu, I do the same with the <input> element using translate:

nav input {
  position: absolute;
  bottom: 20px;
  left: 0;
  z-index: 2;
  right: -70px;
  height: 40px;
  grid-template-columns: 1fr 40px;
}
nav input:before,
nav input:after {
  grid-area:1/2; /* first row, second column */
}
@media {
  nav input {
    display: grid;
    transform: translateX(calc(80px - 100%)); /* keep 80px of width to show the icon */
    transition: .5s;
  }
  nav input:checked {
    transform: translateX(0);
  }
}

Here is another variation where I will not completely hide the menu on small screens but I will keep a small bar visible on the left.

For this variation, in addition to showing the menu, I am also sliding the content with it. I am keeping the same two-column structure and I am playing with the width of the menu to increase/decrease the size of the sidebar. To this, I add an opacity transition and the effect is perfect!

nav ul{
  opacity: 0;
  transition: .5s linear;
  min-width: 0;
  max-width: 0;
  overflow: hidden;
}
nav input:checked + ul {
  max-width: 180px;
  min-width: 180px;
  opacity: 1;
}

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX SHARED HOSTING PLANS

with the discount code

AWESOME

Conclusion

In the previous article, we styled a checkbox input to switch between the hamburger and the close icon. Then we build a simple responsive menu where we used that input to toggle the visibility of the menu on small screens.

In this article, we took a step further and updated our menu to make a sidebar menu still with a responsive feature. We kept the same code structure and only updated the CSS. It means that we can easily switch between a classic menu and a sidebar menu without a lot of hassle. We also made a lot of variations by simply doing small updates to the CSS. You can imagine all the possible combinations we can get from the same code structure.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif