How to Darken Background to Give Focus

How to Darken Background to Give Focus

Ferenc Almasi β€’ 2020 July 31 β€’ πŸ“– 1 min read

Imagine you want to darken the entire background of your page, except for one element. This can act as a great way to focus the user's attention to the most relevant part of the page, such as an important message, a search bar or a CTA.

You can achieve this effect by using only one CSS property: box-shadow.

// This will mask out everything on the page expect for the element
// that has the box shadow.
.input-focused {
  box-shadow: 0 0 0 9999px #000000b0;
}
mask.scss
Copied to clipboard!

This works by specifying the spread-radius of the box-shadow. By increasing the spread, you increase the size of the box-shadow. The goal is to make it so big, that it covers the whole page.

See how it starts to cover everything as I increase it's value:

undefined
Learn how to make a day & night toggle in CSS

You can also start creating interesting effects by defining an element with no content and changing its position through JavaScript.

undefined
By changing the position of the element, you can create user friendly walkthrough tutorials
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScript

Experiment With Box-Shadow

You can use the Codepen below to interact with the element and see live how box-shadow is created :

Edit box-shadow live

How to Darken Background to Give Focus to Input Elements in CSS
If you would like to see more Webtips, follow @flowforfrank

Resource

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended