← Back toFront-end Tips

Visualize elements on page with the outline style

Written byPhuoc Nguyen
Created
23 Feb, 2021
Last updated
02 Apr, 2021
Category
Trick
Tags
DOM, JavaScript
Contributors
ApolloRisky
The outline property is useful when you want to visualize elements on the page. In the following sample code, we iterate over all the elements and set the `outline` property with a random hex color:
const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;

[].forEach.call(document.querySelectorAll('*'), (ele) => (ele.style.outline = `1px solid ${randomColor()}`));
Of course, you will need an opposite command to reset the `outline` property:
[].forEach.call(document.querySelectorAll('*'), (ele) => ele.style.removeProperty('outline'));
You can change the selector from `*` to whatever you want to match the set of particular elements, for example:
// Set the outline for links only
[].forEach.call(
document.querySelectorAll('a'),
...
);

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.