How can I inspect and tweak :before and :after pseudo-elements in-browser?
Solution 1
In Chrome's Dev tools, the styles of a pseudo-element are visible in the panel:
Otherwise, you can also input the following line in the JavaScript console, and inspect the returned CSSStyleDeclaration
object:
getComputedStyle(document.querySelector('html > body'), ':before');
Solution 2
As of Chrome 31 pseudo elements show in the elements panel as child elements of their parent as shown in the following image:
You can select them as you would a normal element but if you remove the content
style then the pseudo element will also be removed and the devtools focus will change to it's parent.
It appears that inherited CSS styles are not viewable and you can't edit CSS content from the elements panel.
Solution 3
Chrome won't show :before and :after pseudo elements in the DOM-tree, if they miss "content" attribute. It should be set, even if it is set to nothing.
This won't show up:
:after {
background-color: red;
}
This will show up in the inspector:
:after {
content: "";
background-color: red;
}
Hope it helps.
Solution 4
At least since Chrome 62 there's a setting in DevTools to 'Show user agent shadow DOM' which displays additional pseudo-elements like input placeholders, which wouldn't show up in the DOM tree otherwise.
More information: https://stackoverflow.com/a/26853319/3963594
Solution 5
After a lot of frustration, I figured out that firefox doesn't show the pseudo elements in the document tree at all, but if you select the exact element which has pseudo element(s) defined, then the styles for its pseudo element(s) are shown in the style rules section on the right side. This is true for both firebug and the built-in inspect ("Q"), and I am shocked that nobody bothered to explain this clearly before.
Clearly, chrome/chromium's handling of pseudo elements is vastly superior, as they can be selected (both in the document tree and directly on the page) and inspected just like regular elements, with layout, properties and everything else, independent of their "owner".
Browser versions I'm using currently: Chromium 40.0.2214.91, Firefox 31.3.0.
majackson
I'm Matt Jackson. I'm a Software Developer currently based in south London, UK. My Computing field interests include functional programming, compiler design and artificial intelligence, although professionally I work primarily on large-scale dynamic web applications, with Python, Tornado, Django, jQuery and nginx being my primary tools of choice. I am also well-versed in PostgreSQL tuning and mapreduce querying in various NoSQL solutions. Outside of Computing, I'm interested in politics, cinema, theatre, cycling, climbing and literature.
Updated on October 23, 2020Comments
-
majackson over 3 years
I have created some fairly elaborate DOM elements with an :after pseudo-element, and I'd like to be able to inspect and tweak them in either Chrome Inspector or Firebug or equivalent.
Despite this feature being mentioned in this WebKit/Safari blog post (dated 2010), I can't find this feature at all in either Chrome or Safari. Chrome does at least have checkboxes to inspect :hover, :visited and :active states, but :before and :after are nowhere to be seen.
Additionally, this blog post (dated 2009!) mentions this capability exists in the IE dev tools, but I'm currently using Mac OS, so this is no help to me. Additionally, IE is not a browser I'm primarily targeting.
Is there any way of inspecting these pseudo-elements?
EDIT: In addition to being wrong about Firebug being unable to inspect these elements, I've found Opera to be pretty good at Inspecting :before and :after elements out of the box.
-
majackson about 12 yearsI don't see this. What element are you inspecting in order to see this appear in the panel? Also, what version of Chrome are you using?
-
Rob W about 12 years@majackson Chrome 18.0 Ubuntu 11.10. Inspect the
x
in this demo: jsfiddle.net/2M6JA -
majackson about 12 yearsI see the pseudoclasses in your demo, but not in my code, which makes me suspect I'm having another, different problem. In any case, it's obvious you are correct so I'll mark this as solved - many thanks!
-
Fabien Quatravaux over 11 yearsIt seams the :pseudo styles cannot be inspected in some cases. Check out this fork of @Rob W fiddle : jsfiddle.net/RQsY6 => the pseudo-element on the a tag cannot be inspected.
-
Rob W over 11 years@FabienQuatravaux At least in Chrome 23, I see
div::before
withcontent: 'x';
- screenshot: i.stack.imgur.com/nQ2TZ.png. EDIT: Our fiddles do not differ at all. Did you want to replacediv
witha
? Still, the pseudo-element can still be inspected: jsfiddle.net/RQsY6/1 (just select the anchor tag in the DOM tree). -
Fabien Quatravaux over 11 yearsSorry @Rob W, I must have forgotten to update it. Here is the new one: jsfiddle.net/RQsY6/2. I think that when the css rule is too complex, it does not work (here : section p.test1.test2 a:before). When I simplify the rule (by removing one class), I can inspect the pseudo element.
-
sameers over 8 yearsI don't see the
:after
and:before
pseudo elements in the "native" inspector tool - toggling the pseudo elements gives mehover
,active
andfocus
only. I can see them in Firebug though. -
NoBugs over 8 years@sameers Are you talking about the toggles on right click menu? Because :before and :after aren't something you toggle there, they should always show up in the css inspector.
-
Coderer over 3 yearsThis has been expanded in newer Chrome if you turn on "Show user agent shadow DOM" in the dev tools settings, under Elements. You can even see pseudo elements inside various specialized
input
tags, which is really helpful. Credit to this (huge) page for the tip! -
CPHPython over 2 yearsYou opened up a whole new world for me: pseudo elements now showing up in
<div pseudo="...">
tags inside the element's#shadow-root
. Setting is on Preferences > Elements > Show user agent shadow DOM in Edge.