πŸ’‘ This page contain affiliate links. By making a purchase through them, we may earn a commission at no extra cost to you.
How to Darken Background to Give Focus

How to Darken Background to Give Focus

Ferenc Almasi β€’ 2020 July 31 β€’ Read time 1 min read
  • twitter
  • facebook
CSS

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.

Copied to clipboard!
// 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

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

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

  • twitter
  • facebook
CSS
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.