Use hidden check boxes to control the menu status; 2. Position the menu off-screen by default through CSS; 3. Use checked and brother selectors to develop the effect; 4. Add transition to achieve smooth animation; 5. Optional push content or add masks to improve the user experience. This method can achieve a lightweight and efficient CSS sidebar menu without JavaScript, which is suitable for simple websites or learning scenarios, complete and effective.
Creating an off-canvas menu with CSS is a clean and efficient way to save space—especially on mobile devices—while still offering easy navigation. You can build it using just HTML and CSS (no JavaScript required for basic functionality), leveraging transforms and checkboxes for toggling.

Here's how to do it step by step:
? 1. Basic HTML Structure
Start with a simple layout: a toggle button, a main content area, and the off-canvas sidebar.

<input type="checkbox" id="menu-toggle" class="menu-toggle"> <label for="menu-toggle" class="menu-button">?</label> <nav class="off-canvas-menu"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <main class="main-content"> <h1>Your Website Content</h1> <p>Click the menu icon to open the off-canvas navigation.</p> </main>
We use a hidden checkbox (
menu-toggle
) to control the menu state via CSS. The label acts as the toggle button.
? 2. Styling the Off-Canvas Menu with CSS
The key idea is to position the menu off-screen by default and slide it in using transform
.

/* Reset and base styles */ body { margin: 0; font-family: Arial, sans-serif; overflow-x: hidden; /* Prevent horizontal scroll */ } .menu-toggle { display: none; /* Hidden but functional */ } .menu-button { position: fixed; top: 10px; left: 10px; background: #333; color: white; padding: 10px; cursor: pointer; border-radius: 4px; z-index: 3; } /* Off-canvas menu */ .off-canvas-menu { position: fixed; top: 0; left: 0; width: 250px; height: 100%; background: #222; color: white; transform: translateX(-100%); /* Hidden by default */ transition: transform 0.3s ease; z-index: 2; padding-top: 60px; } .off-canvas-menu ul { list-style: none; padding: 0; margin: 0; } .off-canvas-menu a { display: block; padding: 15px 20px; color: white; text-decoration: none; border-bottom: 1px solid #444; } .off-canvas-menu a:hover { background: #444; } /* Main content */ .main-content { padding: 20px; transition: transform 0.3s ease; }
? 3. Toggle the Menu Using the Checkbox Hack
Use the :checked
pseudo-class to trigger the menu appearance when the checkbox is toggled.
/* Show menu when checkbox is checked */ .menu-toggle:checked ~ .off-canvas-menu { transform: translateX(0); /* Slide in */ } /* Optional: Push main content when menu is open */ .menu-toggle:checked ~ .main-content { transform: translateX(250px); }
The
~
(general sibling combiner) lets us style elements that come after the checkbox in the DOM.
? 4. Optional: Add Overlay or Responsive Behavior
For better UX, you might want to dim the background when the menu is open.
/* Add a semi-transparent overlay */ .menu-toggle:checked ~ .main-content::before { content: ''; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); z-index: 1; }
This overlay also helps close the menu when clicked (since clicking outside the menu triggers the label or body, unchecking the input).
? Summary of Key Points
- Use a hidden checkbox as a toggle state.
- Position the menu off-screen with
transform: translateX(-100%)
. - Use
:checked
and sibling selectors to trigger the slide-in effect . - Add smooth transitions with
transition
. - Optionally push content or add an overlay for better UX.
This method is lightweight and works without JavaScript. While modern apps often use JS for more complex behavior, this pure-CSS solution is perfect for simple sites or learning purposes.
Basically, it's all about positioning, transforms, and clever use of the checkbox hack. Not flashy, but effective.
The above is the detailed content of How to create an off-canvas menu with CSS?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

CSS,orCascadingStyleSheets,isthepartofwebdevelopmentthatcontrolsawebpage’svisualappearance,includingcolors,fonts,spacing,andlayout.Theterm“cascading”referstohowstylesareprioritized;forexample,inlinestylesoverrideexternalstyles,andspecificselectorslik
