Most demos need a newer browser

They use :has() and container queries. Update your browser, or on an iPhone update iOS to 16.4 or later.

CSS Neumorphism

Soft UI controls with neumorphism box-shadow pairs and a raised button that presses in on click using paired shadows in both states.

Level
Intermediate
Published
Updated
Reading time
5 min

Try it

4 live demos

Your browser can't run this demo

It needs :has(), which this browser doesn't support yet. It works in Chrome 105+, Edge 105+, Firefox 121+ and Safari 15.4+.

Needs :has()

Focus timer

25:00

Press the button, latch the toggle

Other ways to build it

Text alignment

Pick an alignment and this paragraph follows it, while the chosen button stays pressed into the surface.

Icon buttons, one stays selected

See the code
Soft only

Alerts

A sound plays when a session ends.

With contrast fixes

Alerts

A sound plays when a session ends.

Contrast fixes

See the code

Build it

8 snippets to copy

CSS Neumorphism

HTML

<div class="nm-surface">
  <div class="nm-card">
    <p class="nm-label">Focus timer</p>
    <p class="nm-well">25:00</p>
    <div class="nm-row">
      <button class="nm-btn" type="button">Start</button>
      <label class="nm-latch">
        <input type="checkbox">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M224 0c-17.7 0-32 14.3-32 32l0 3.2C119 50 64 114.6 64 192l0 21.7c0 48.1-16.4 94.8-46.4 132.4L7.8 358.3C2.7 364.6 0 372.4 0 380.5 0 400.1 15.9 416 35.5 416l376.9 0c19.6 0 35.5-15.9 35.5-35.5 0-8.1-2.7-15.9-7.8-22.2l-9.8-12.2C400.4 308.5 384 261.8 384 213.7l0-21.7c0-77.4-55-142-128-156.8l0-3.2c0-17.7-14.3-32-32-32zM162 464c7.1 27.6 32.2 48 62 48s54.9-20.4 62-48l-124 0z"/></svg>
        Sound
      </label>
    </div>
  </div>
</div>

CSS

/* one pinned surface: the effect only works when every
   piece sits on this same background color. overflow keeps
   the glow from spilling past its edge. */
.nm-surface {
  --nm-bg: #e0e5ec;
  --nm-hi: #ffffff;
  --nm-lo: #a3b1c6;
  --nm-ink: #2d3748;
  --nm-soft: #56627a;
  --nm-accent: #2f5bd3;
  box-sizing: border-box;
  display: grid;
  place-items: center;
  width: 100%;
  max-width: 34rem;
  padding: 3rem 1.5rem;
  border-radius: 16px;
  overflow: hidden;
  color: var(--nm-ink);
  background: var(--nm-bg);
}

/* raised: light from the top left, shade to the bottom right */
.nm-card {
  box-sizing: border-box;
  display: grid;
  gap: 1.25rem;
  width: 100%;
  max-width: 17rem;
  padding: 1.5rem;
  border-radius: 24px;
  background: var(--nm-bg);
  box-shadow: 10px 10px 24px var(--nm-lo), -10px -10px 24px var(--nm-hi);
}

.nm-label {
  margin: 0;
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--nm-soft);
}

/* the same two shadows turned inward make a well */
.nm-well {
  margin: 0;
  padding: 1rem;
  border-radius: 16px;
  font-size: 2.6rem;
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  font-variant-numeric: tabular-nums;
  box-shadow: inset 6px 6px 12px var(--nm-lo), inset -6px -6px 12px var(--nm-hi);
}

.nm-row {
  display: flex;
  gap: 1rem;
}

/* four shadows in both states, in the same order and with
   inset in the same slots, so the press animates instead
   of jumping */
.nm-btn,
.nm-latch {
  position: relative;
  box-sizing: border-box;
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 3rem;
  padding: .75rem 1rem;
  border: 0;
  border-radius: 999px;
  font: inherit;
  font-weight: 700;
  color: var(--nm-ink);
  background: var(--nm-bg);
  cursor: pointer;
  box-shadow: 6px 6px 12px var(--nm-lo), -6px -6px 12px var(--nm-hi),
    inset 0 0 0 var(--nm-lo), inset 0 0 0 var(--nm-hi);
  transition: box-shadow .2s ease, color .2s ease;
}

/* pressed while held, or for as long as the box is checked */
.nm-btn:active,
.nm-latch:has(:checked) {
  box-shadow: 0 0 0 var(--nm-lo), 0 0 0 var(--nm-hi),
    inset 5px 5px 10px var(--nm-lo), inset -5px -5px 10px var(--nm-hi);
}

.nm-latch:has(:checked) {
  color: var(--nm-accent);
}

/* the checkbox stays focusable, it is only moved out of sight */
.nm-latch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.nm-latch svg {
  width: 1rem;
  height: 1rem;
}

.nm-btn:focus-visible,
.nm-latch:has(:focus-visible) {
  outline: 2px solid var(--nm-accent);
  outline-offset: 4px;
}

@media (prefers-reduced-motion: reduce) {
  .nm-btn,
  .nm-latch {
    transition: none;
  }
}

A dark palette

The same card and controls on a dark surface. Only the six custom properties change, set by one class on the surface. On a dark surface the highlight shadow is a lighter shade of the surface color instead of white, and the shade shadow is darker. Pick this palette for a dark interface.

HTML

<div class="nm-surface nm-dark">
  <div class="nm-card">
    <p class="nm-label">Focus timer</p>
    <p class="nm-well">25:00</p>
    <div class="nm-row">
      <button class="nm-btn" type="button">Start</button>
      <label class="nm-latch">
        <input type="checkbox">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M224 0c-17.7 0-32 14.3-32 32l0 3.2C119 50 64 114.6 64 192l0 21.7c0 48.1-16.4 94.8-46.4 132.4L7.8 358.3C2.7 364.6 0 372.4 0 380.5 0 400.1 15.9 416 35.5 416l376.9 0c19.6 0 35.5-15.9 35.5-35.5 0-8.1-2.7-15.9-7.8-22.2l-9.8-12.2C400.4 308.5 384 261.8 384 213.7l0-21.7c0-77.4-55-142-128-156.8l0-3.2c0-17.7-14.3-32-32-32zM162 464c7.1 27.6 32.2 48 62 48s54.9-20.4 62-48l-124 0z"/></svg>
        Sound
      </label>
    </div>
  </div>
</div>

CSS

/* adds to the main example: every shape and shadow comes
   from there, and only the six colors change */
.nm-surface.nm-dark {
  --nm-bg: #292d34;
  --nm-hi: #353a43;
  --nm-lo: #1a1d21;
  --nm-ink: #e4e8ee;
  --nm-soft: #a3adbd;
  --nm-accent: #8fb0ff;
}

Icon buttons, one stays selected

Four icon buttons made from radios in a <fieldset>. The checked one stays pressed in, and :has([value="center"]:checked) on the wrapper sets the paragraph alignment below it. Clicking an icon checks its radio, which both presses the icon and changes the text alignment. Pick this for any one-of-many toolbar: alignment, view mode, or a tab-like switch.

HTML

<div class="nm-surface">
  <div class="nm-editor">
    <fieldset class="nm-tools">
      <legend>Text alignment</legend>
      <label class="nm-icon">
        <input type="radio" name="nm-align" value="left" aria-label="Align left" checked>
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M288 64c0 17.7-14.3 32-32 32L32 96C14.3 96 0 81.7 0 64S14.3 32 32 32l224 0c17.7 0 32 14.3 32 32zm0 256c0 17.7-14.3 32-32 32L32 352c-17.7 0-32-14.3-32-32s14.3-32 32-32l224 0c17.7 0 32 14.3 32 32zM0 192c0-17.7 14.3-32 32-32l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 224c-17.7 0-32-14.3-32-32zM448 448c0 17.7-14.3 32-32 32L32 480c-17.7 0-32-14.3-32-32s14.3-32 32-32l384 0c17.7 0 32 14.3 32 32z"/></svg>
      </label>
      <label class="nm-icon">
        <input type="radio" name="nm-align" value="center" aria-label="Align center">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M352 64c0-17.7-14.3-32-32-32L128 32c-17.7 0-32 14.3-32 32s14.3 32 32 32l192 0c17.7 0 32-14.3 32-32zm96 128c0-17.7-14.3-32-32-32L32 160c-17.7 0-32 14.3-32 32s14.3 32 32 32l384 0c17.7 0 32-14.3 32-32zM0 448c0 17.7 14.3 32 32 32l384 0c17.7 0 32-14.3 32-32s-14.3-32-32-32L32 416c-17.7 0-32 14.3-32 32zM352 320c0-17.7-14.3-32-32-32l-192 0c-17.7 0-32 14.3-32 32s14.3 32 32 32l192 0c17.7 0 32-14.3 32-32z"/></svg>
      </label>
      <label class="nm-icon">
        <input type="radio" name="nm-align" value="right" aria-label="Align right">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M448 64c0 17.7-14.3 32-32 32L192 96c-17.7 0-32-14.3-32-32s14.3-32 32-32l224 0c17.7 0 32 14.3 32 32zm0 256c0 17.7-14.3 32-32 32l-224 0c-17.7 0-32-14.3-32-32s14.3-32 32-32l224 0c17.7 0 32 14.3 32 32zM0 192c0-17.7 14.3-32 32-32l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 224c-17.7 0-32-14.3-32-32zM448 448c0 17.7-14.3 32-32 32L32 480c-17.7 0-32-14.3-32-32s14.3-32 32-32l384 0c17.7 0 32 14.3 32 32z"/></svg>
      </label>
      <label class="nm-icon">
        <input type="radio" name="nm-align" value="justify" aria-label="Justify">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M448 64c0-17.7-14.3-32-32-32L32 32C14.3 32 0 46.3 0 64S14.3 96 32 96l384 0c17.7 0 32-14.3 32-32zm0 256c0-17.7-14.3-32-32-32L32 288c-17.7 0-32 14.3-32 32s14.3 32 32 32l384 0c17.7 0 32-14.3 32-32zM0 192c0 17.7 14.3 32 32 32l384 0c17.7 0 32-14.3 32-32s-14.3-32-32-32L32 160c-17.7 0-32 14.3-32 32zM448 448c0-17.7-14.3-32-32-32L32 416c-17.7 0-32 14.3-32 32s14.3 32 32 32l384 0c17.7 0 32-14.3 32-32z"/></svg>
      </label>
    </fieldset>
    <p class="nm-sample">Pick an alignment and this paragraph follows it, while the chosen button stays pressed into the surface.</p>
  </div>
</div>

CSS

/* adds to the main example: .nm-surface and its six colors
   come from there */
.nm-editor {
  display: grid;
  gap: 1.5rem;
  width: 100%;
  max-width: 20rem;
}

.nm-tools {
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.nm-tools legend {
  margin-bottom: 1rem;
  padding: 0;
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--nm-soft);
}

.nm-icon {
  position: relative;
  display: grid;
  place-items: center;
  width: 3.25rem;
  height: 3.25rem;
  border-radius: 14px;
  color: var(--nm-soft);
  background: var(--nm-bg);
  cursor: pointer;
  box-shadow: 5px 5px 10px var(--nm-lo), -5px -5px 10px var(--nm-hi),
    inset 0 0 0 var(--nm-lo), inset 0 0 0 var(--nm-hi);
  transition: box-shadow .2s ease, color .2s ease;
}

/* the checked radio stays pressed in */
.nm-icon:has(:checked) {
  color: var(--nm-accent);
  box-shadow: 0 0 0 var(--nm-lo), 0 0 0 var(--nm-hi),
    inset 4px 4px 8px var(--nm-lo), inset -4px -4px 8px var(--nm-hi);
}

.nm-icon input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.nm-icon svg {
  width: 1.1rem;
  height: 1.1rem;
}

.nm-icon:has(:focus-visible) {
  outline: 2px solid var(--nm-accent);
  outline-offset: 4px;
}

.nm-sample {
  margin: 0;
  padding: 1.1rem 1.25rem;
  border-radius: 16px;
  line-height: 1.6;
  box-shadow: inset 5px 5px 10px var(--nm-lo), inset -5px -5px 10px var(--nm-hi);
}

/* the checked radio sets the paragraph's alignment */
.nm-editor:has([value="center"]:checked) .nm-sample {
  text-align: center;
}

.nm-editor:has([value="right"]:checked) .nm-sample {
  text-align: right;
}

.nm-editor:has([value="justify"]:checked) .nm-sample {
  text-align: justify;
}

@media (prefers-reduced-motion: reduce) {
  .nm-icon {
    transition: none;
  }
}

Contrast fixes

The same controls side by side, soft only and with contrast fixes. The fix adds four changes: a 1px border at 3.58 to 1 so the control boundary is visible, darker text, a filled checked state that changes color instead of relying only on shadow, and a heavier 3px focus ring.

HTML

<div class="nm-surface nm-a11y">
  <div class="nm-card">
    <p class="nm-label">Alerts</p>
    <p class="nm-note">A sound plays when a session ends.</p>
    <div class="nm-row">
      <button class="nm-btn" type="button">Save</button>
      <label class="nm-latch">
        <input type="checkbox">
        <svg viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M224 0c-17.7 0-32 14.3-32 32l0 3.2C119 50 64 114.6 64 192l0 21.7c0 48.1-16.4 94.8-46.4 132.4L7.8 358.3C2.7 364.6 0 372.4 0 380.5 0 400.1 15.9 416 35.5 416l376.9 0c19.6 0 35.5-15.9 35.5-35.5 0-8.1-2.7-15.9-7.8-22.2l-9.8-12.2C400.4 308.5 384 261.8 384 213.7l0-21.7c0-77.4-55-142-128-156.8l0-3.2c0-17.7-14.3-32-32-32zM162 464c7.1 27.6 32.2 48 62 48s54.9-20.4 62-48l-124 0z"/></svg>
        Sound
      </label>
    </div>
  </div>
</div>

CSS

/* adds to the main example: the card, shadows and controls
   come from there */
.nm-note {
  margin: 0;
  font-size: .95rem;
  line-height: 1.5;
  color: var(--nm-soft);
}

/* darker text, and an edge color for the border */
.nm-surface.nm-a11y {
  --nm-ink: #1a202c;
  --nm-soft: #3b4556;
  --nm-edge: #6b778a;
}

/* a border marks the control without the shadows */
.nm-a11y .nm-btn,
.nm-a11y .nm-latch {
  border: 1px solid var(--nm-edge);
}

/* on is a color change too, not only a shadow change */
.nm-a11y .nm-latch:has(:checked) {
  border-color: var(--nm-accent);
  color: #ffffff;
  background: var(--nm-accent);
}

/* a heavier ring in the darkest ink */
.nm-a11y .nm-btn:focus-visible,
.nm-a11y .nm-latch:has(:focus-visible) {
  outline: 3px solid var(--nm-ink);
  outline-offset: 3px;
}

Understand it

How it works and 7 properties

Neumorphism makes controls look like they sit on or into a surface using paired shadows instead of borders. A raised button has one light shadow and one dark shadow, cast from opposite corners. Press it and both shadows flip inward, which gives the pressed state. The whole effect depends on every piece sitting on the same background color, so the surface here is pinned rather than themed.

How it works

A raised button has two shadows: 6px 6px 12px for the shade and -6px -6px 12px for the highlight, both cast from opposite corners on the same background. Press it and the outer shadows shrink to zero while two inset shadows grow from zero, which reads as the button sinking into the surface. Both states list four shadows in the same order, with the unused slots set to 0 0 0 so they paint nothing.

The surface has to be a known, fixed color because the shadows depend on it. A light shadow over a light surface paints faintly, and the same shadow over a darker surface reads differently. That is why the demo sets six custom properties and ignores the site theme.

The latch toggle uses :has(:checked) to stay pressed after the click, and the icon button group does the same to keep the selected one inset. Without :has() the inputs still work as form controls but nothing shows which one is on, so the visual state is lost. The paragraph alignment demo depends on it too: clicking an icon checks its radio and :has([value="center"]:checked) on the wrapper applies text-align: center to the paragraph.

Accessibility notes

The main examples use real buttons and real form controls. The hidden input is moved out of sight with position: absolute and opacity: 0, but it stays in the tab order and keeps its role.

Text contrast in the main demo is above 4.5 to 1 and the focus ring is above 4.5 to 1, but the control edge is faint. The contrast-fixed variant addresses that with a real border at 3.58 to 1, darker text, a heavier ring, and a checked state that fills with color instead of relying only on shadow.

The icon buttons are in a real <fieldset> with a <legend>, which is the correct markup for a group of radios. Each input has an aria-label because the visible text is only an icon. The accessibility tree shows a group named "Text alignment" with four radios, and keyboard navigation works: tab into the group, arrow between the choices, tab out. One stop for the whole group, which is standard radio behavior.

Mistakes worth avoiding

  • Putting an inset shadow and an outer shadow in the same position. The transition jumps instead of animating.
  • Theming the surface. The shadows depend on the background color, so a theme swap changes how the effect reads rather than recoloring it.
  • Skipping the contrast fixes. The edge of a soft button measures around 1.6 to 1 and the shadow-only on state is faint. Add borders and a fill.
  • Forgetting box-sizing: border-box on the controls. The label's min-height applies to its content box instead of its border box, and it stretches taller than the button.
  • Using :hover on touch. Soft UI has no hover style because there is no hover state to lose on a phone, and the effect reads the same with or without a pointer.

Ship it

7 features in 4 browsers, 5 questions

Browser support

Feature Chrome Firefox Safari Edge
box-shadow43.5512
border-radius433.112
:has()10512115.4105
:focus-visible86415.486
custom properties49311016
transition455.112
prefers-reduced-motion746310.179

The parts are old and well supported. :has() is the newest dependency. Without it the checkboxes and radios still function as form controls, but the visual pressed state is not shown, so the latch stays looking unpressed and the icon buttons give no sign of which one is selected.

What you can build with it

  • Focused work timers. Session timers where the pressed button shows the timer is running.
  • Media player controls. Play, pause, skip buttons and a volume toggle, where the on state stays visibly pressed.
  • Icon toolbars. Alignment, view mode or tool selection where one button at a time stays selected.
  • Settings panels. Toggle rows for notifications, sounds or features, each a checkbox with a label wrapped around it.
  • Dashboard cards. Stat cards on a matching surface, where the raised shadow gives a layered look without hard edges.

Frequently asked questions

How do you animate a neumorphism button press?
List four shadows in both the rest state and the pressed state, with inset in the same slots in both. The rest state has two outer shadows and two 0 0 0 inset shadows. The pressed state has two 0 0 0 outer shadows and two inset shadows.
Why does my neumorphism button snap instead of animating?
An inset shadow and an outer shadow probably meet in the same position. Browsers cannot animate that, so the change is instant. Add placeholder 0 0 0 shadows so every slot is inset in both states or outer in both.
Can neumorphism work in dark mode?
Yes, but the palette changes. The first variant shows a dark palette that fits a dark interface.
Why is the control edge faint in neumorphism?
The boundary between a control and the surface is only shadow, and that measures around 1.6 to 1 against the surface. Add a real border, darker text and a heavier focus ring to make the controls more visible.
Does neumorphism need JavaScript?
No. The shadows are CSS, the press is :active, and the latch is :has(:checked). Nothing here depends on script.