How to create a pure CSS hamburger menu icon?
A reliable CSS-only hamburger uses <input type="checkbox"> paired with a

What HTML structure works reliably for a CSS-only hamburger
A minimal, accessible structure is key—no JavaScript means the toggle state must rely on <input type="checkbox">. The checkbox controls visibility via sibling combinators, and its label becomes the clickable icon. Avoid <div>-only approaches: they’re not keyboard-focusable or screen-reader friendly without ARIA.<ul>
<li>Use <code><input id="menu-toggle" type="checkbox"> <label for="menu-toggle"></label> pairing
<label></label>, not the input<input> with position: absolute; opacity: 0; pointer-events: none; — don’t use display: none (breaks :checked logic)How to draw three responsive bars with pure CSS
You don’t need extra <span></span> elements if you use pseudo-elements. Each bar is a ::before or ::after on the <label></label>, layered with position: absolute and controlled by transform for animation.
- Base label: set
display: block, fixedwidthandheight, relative positioning - First bar:
label::before, centered vertically, full width, height ~2px - Second bar: main
labelcontent (e.g., empty<span></span>or just background), same size - Third bar:
label::after, same as first but offset down - For responsiveness: use
emorremunits, and scale all dimensions together in a media query
Why :checked sibling selectors are safer than :focus or :hover
:checked gives you a persistent, script-free toggle state — essential for mobile menus that stay open until explicitly closed. Relying on :focus breaks when users click elsewhere; :hover fails entirely on touch devices.
- Target menu content with
#menu-toggle:checked ~ .nav-menu(note the general sibling~) - Avoid adjacent sibling
unless the menu is *immediately* after the input — fragile in real layouts - If your menu is deeper in the DOM, you’ll need JavaScript — pure CSS can’t traverse up or across unrelated branches
Common visual bugs and how to fix them
The most frequent issues aren’t about drawing bars — they’re about stacking context, transform origins, and timing mismatches during animation.
- Bars disappear during rotation: set
transform-origin: centeron each pseudo-element, not defaulttop left - Animation jumps or skips: declare
transitionon the base label *and* its pseudo-elements — don’t rely only on the label’s transition to cover them - Icon misaligned on iOS Safari: add
-webkit-appearance: noneto the<input>and force hardware acceleration withtransform: translateZ(0)on the label - Click area too small: expand the label’s
paddingor use::beforeto create invisible hit area — never shrink the checkbox itself
CSS hamburger icons work cleanly only when the toggle state, layout, and animation layers are all aligned — it’s less about “drawing three lines” and more about keeping the checkbox’s :checked signal connected to every visual and structural change.
The above is the detailed content of How to create a pure CSS hamburger menu icon?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
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
20521
7
13634
4
CSS mobile performance optimization_Use will-change to inform transition attributes in advance
Mar 12, 2026 am 11:15 AM
Will-change should only be declared for transform and opacity attributes that will change frequently and can trigger synthesis; avoid abusing invalid attributes such as all, left, top, etc., which must be added/removed dynamically, and be used with caution in scrolling containers. Mobile terminals need to take into account compatibility and memory limitations.
How to make a simple fixed bottom toolbar with CSS_Set bottom:0 through position:fixed
Mar 10, 2026 pm 02:12 PM
The main reason why bottom:0 does not take effect is that the ancestor element triggers transform/will-change/filter to create a new containing block, so that the fixed element is positioned relative to it rather than the viewport; dynamic changes in the iOS Safari address bar cause occlusion; fixed elements need to be given way with padding-bottom; z-index failure is often caused by the parent creating a cascading context.
How to avoid content overflow when using CSS with float_Set the box-sizing of the box model and ensure that the sum of the percentages does not exceed 100%
Mar 12, 2026 pm 12:00 PM
If the floating element cannot open the parent container, BFC needs to be triggered. Overflow:hidden is commonly used; box-sizing:border-box must be set on the floating element itself; the percentage exceeds 100% due to whitespace characters, border/padding and pixel rounding; in IE, pay attention to the box-sizing prefix and margin parsing bugs.
How to set a transparent frosted sidebar with CSS_using rgba color through background-filter
Apr 03, 2026 pm 03:57 PM
In Safari, the background-filter needs to be prefixed with -webkit- and the background is opaque to take effect; the blur value cannot be 0; the size of the blurred area affects performance more than the numerical value; text needs to be enhanced with contrast and strokes to ensure readability.
How to make a simple jitter tip with CSS_using keyframes and rotation
Apr 03, 2026 pm 03:42 PM
Shake animation should use translateX() to achieve left and right displacement instead of rotate(); it needs to be combined with @keyframes to define a 0%/25%/50%/75%/100% offset sequence, and the offset is controlled within ±2px~±6px; animation-fill-m must be added ode:forwards (or use both), select cubic-bezier(.36,.07,.19,.97) for easing; the old animation must be cleared and forced to rearrange before JS is triggered; use will-change with caution on the mobile terminal, and only add translateZ(0) to ensure hardware acceleration.
How to adjust the picture display ratio in CSS_define the object-fit attribute in style
Mar 13, 2026 pm 01:09 PM
object-fit:cover is the most common choice - it maintains the aspect ratio, crops excess parts, and ensures the container is completely filled.
How does CSS exclude specific types of elements_Use the not pseudo-class selector to filter css
Mar 10, 2026 pm 02:57 PM
:not() only supports a single simple selector, disabling composite writing methods such as descendants/descendants; to exclude multiple conditions, you need to write repeatedly, such as: not(.a):not([b]); when mixed with :nth-child, it will still match the original sequence number, and will be rearranged after non-filtering.
How CSS solves the absolute positioning offset bug under IE_Fixed by setting the hasLayout attribute
Apr 03, 2026 pm 03:48 PM
The essence of the inaccurate offset of position:absolute in IE6–8 is that the parent container does not trigger hasLayout, causing the absolute positioning reference to the wrong containing block; it needs to be actively triggered by reliable methods such as zoom:1, rather than relying solely on position:relative.





