How to Darken Background to Give Focus

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;
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:

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.

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


Remove ads
Remove ads

📚 Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. ☕

Get access Support us
Remove ads
Remove ads
Remove ads
🎉 Thank you for subscribing to our newsletter. x 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.