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
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
JavaScript Course

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


JavaScript Course Dashboard

Tired of looking for tutorials?

You are not alone. Webtips has more than 400 tutorials which would take roughly 75 hours to read.

Check out our interactive course to master JavaScript in 5 hours.

Learn More

📚 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
🎉 Thank you for subscribing to our newsletter. x