Off-Screen Side Nav Using Only CSS
Master CSS-Only Mobile Navigation Without JavaScript
This tutorial demonstrates how to create fully functional off-screen navigation using only CSS - no JavaScript required. We'll leverage CSS pseudo-classes and HTML form elements for interactive behavior.
Key Concepts You'll Learn
Responsive Off-screen Navigation
Learn to create mobile-friendly navigation that slides in from the side using CSS positioning and transitions.
Checkbox Toggle Technique
Master the CSS-only approach using checkboxes and labels to create interactive navigation without JavaScript.
CSS Transitions
Add smooth animations and transitions to create professional sliding effects for your navigation menu.
What You'll Build
Hidden off-screen by default on mobile devices
CSS transitions for professional user experience
Menu button, close button, and overlay click functionality
Responsive design that adapts to different screen sizes
Mobile vs Desktop Navigation Layout
| Feature | Mobile (max-width: 700px) | Desktop (min-width: 701px) |
|---|---|---|
| Position | Fixed sidebar | Top horizontal bar |
| Display | Vertical stack | Inline-block |
| Width | 12em fixed width | Full width with right alignment |
| Scrolling | Vertical scroll enabled | No scroll needed |
Adding overflow-y: auto and -webkit-overflow-scrolling: touch ensures navigation items remain accessible even on small screens or when held horizontally.
A checkbox has a :checked pseudo-class selector that allows us to style an element based on whether the element is checked or not
Checkbox Toggle Implementation
Add Checkbox Input
Place checkbox with unique ID directly after opening body tag for proper DOM positioning
Style Checkbox Position
Position checkbox temporarily in visible location for testing purposes
Create Checked State Rule
Use sibling selector to target nav element when checkbox is checked
The left position value must be equal to the width of the element to ensure the entire navigation is hidden off-screen. In this case, width: 12em requires left: -12em.
Essential CSS Properties for Off-Screen Navigation
Negative Positioning
Use left: -12em to move the 12em wide navigation completely off-screen to the left.
Z-Index Layering
Set z-index: 100 to ensure navigation appears above all other page content when visible.
Smooth Transitions
Add transition: all .2s ease for professional sliding animation instead of abrupt appearance.
Labels with matching 'for' attributes can control form elements. When you click a label, it focuses or checks the associated form element, enabling our CSS-only toggle functionality.
User Experience Improvements
Provides visual feedback that element is clickable
Use user-select: none to prevent unwanted text highlighting
Display: none in media query for screens wider than 700px
We can have multiple labels for the same checkbox, allowing us to create two different buttons that both control a single checkbox
Overlay Implementation Process
Create Full-Screen Label
Add label element that covers entire viewport with position: fixed and 100% dimensions
Add Conditional Visibility
Use display: none by default, then show with #nav-checkbox:checked ~ .overlay selector
Remove Visual Background
Delete background property to maintain click functionality while keeping overlay invisible
Checkbox Hiding Methods
| Feature | clip: rect(0,0,0,0) | display: none |
|---|---|---|
| Accessibility | Maintains keyboard navigation | Removes from tab order |
| Browser Support | Universal support | Universal support |
| Functionality | Preserves all interactions | Breaks some interactions |
Changing checkbox position to fixed prevents browsers like Chrome from jumping to the top of the page when the checkbox is toggled.
The entire navigation can be flipped to slide in from the right by simply changing 'left' properties to 'right' in two CSS rules. This demonstrates the flexibility of the CSS-only approach.
Key Takeaways
