Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to inspect the "computed" styles for ::after and ::before pseudo elements?

Chrome shows me the ::after and ::before definitions (and inherited definitions etc..) in the Matched CSS Rules pane when I inspect an element. But the Computed Styles pane displays only the computed styles for the root element.

I have a whole CSS class hierarchy for the pseudo elements with some properties inherited and some overridden etc, and would like to know if there is any way I can see the set of styles that actually got applied (i.e. what the computed styles pane shows for the root element)

If anyone can shed any light on this, that'll be extremely helpful.

Edit - 13th Feb, 2014

Recent versions of Chrome indeed has this feature built-in Perhaps this little thread had something to do with them including it?? :-)

like image 472
techfoobar Avatar asked Dec 16 '12 09:12

techfoobar


People also ask

When would you use the :: before or :: after pseudo-element in your CSS?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.

What property must have a value if you use the pseudo elements :: before and :: after?

The content attribute. This property is used with the :before and :after pseudo-elements to generate content in a document.

What is :: before in inspect element?

::before. ::before is a generated content element that represents a styleable abstract first child of the respective element. The content inserted using ::before is inserted before other content inside the element and is displayed inline by default. The value of the content is specified using the content property.

What is :: before and :: after used for?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.


1 Answers

window.getComputedStyle( element[, pseudoElt] )

pseudoElt Optional

A string specifying the pseudo-element to match. Must be omitted (or null) for regular elements. Pseudo-classes can be specified like ":focus".

I'm possibly misunderstanding what you're trying to accomplish, but here's some JavaScript to play with. I hope it's useful.

It iterates through every element in the document and attempts to get the content of its :befores and :afters.
If it finds any, it outputs the details to the console.
Simple, but demonstrable.

document.querySelectorAll( "*" ).forEach( ( e ) => {
  const ebc = window.getComputedStyle( e, ":before" ).content,
        eac = window.getComputedStyle( e, ":after" ).content;
  if ( ebc || eac ) {
    console.log( e );
    console.log( ( ebc ? "Before content = " + ebc : "No :before content" ) );
    console.log( ( eac ? "After content = " + eac : "No :after content" ) );
  }
} );
div:before {
  content: "This is a test.";
}
p:after {
  content: attr( data-content );
}
span:after {
  content: " dolor";
}
<div>
  <p data-content="&hellip;">Lorem <span>ipsum</span></p>
</div>
like image 195
Fred Gandt Avatar answered Oct 30 '22 11:10

Fred Gandt