Treehouse

Get Pseudo-Element Properties with JavaScript

By on  

CSS pseudo-elements are incredibly useful -- they allow us to create CSS triangles for tooltips and perform a number of other simple tasks while preventing the need for additional HTML elements.  To this point, these pseudo-element CSS properties have been unreachable by JavaScript but now there's a method for getting them!

Assume your CSS looks like:

.element:before {
	content: 'NEW';
	color: rgb(255, 0, 0);
}

To retrieve the color property of the .element:before, you could use the following JavaScript:

var color = window.getComputedStyle(
	document.querySelector('.element'), ':before'
).getPropertyValue('color')

Passing the pseudo-element as the second argument to window.getComputedStyle allows access to said pseudo-element styles!  Keep this snippet in your toolbox for years to come -- pseudo-elements will only grow more useful with broader browser support!

ydkjs-2.png

Recent Features

  • From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • Full Width Textareas

    Working with textarea widths can be painful if you want the textarea to span 100% width.  Why painful?  Because if the textarea's containing element has padding, your "width:100%" textarea will likely stretch outside of the parent container -- a frustrating prospect to say the least.  Luckily...

  • HTML5 Input Types Alternative

    As you may know, HTML5 has introduced several new input types: number, date, color, range, etc. The question is: should you start using these controls or not? As much as I want to say "Yes", I think they are not yet ready for any real life...

Discussion

  1. MaxArt

    Yeah, too bad this doesn’t work with IE8 and previous.
    currentStyle is good for some things, but… not for this one.

  2. MaxArt

    By the way, I forgot to say that it’s usually recommended to use document.defaultView.getComputedStyle instead of window.getComputedStyle, because there are cases where document.defaultView !== window.

    I don’t know which ones, though. I guess it’s not the case of “common” web pages.

    • Martin

      From the MDN documentation:

      In many code samples online, getComputedStyle is used from the document.defaultView object. In nearly all cases, this is needless, as getComputedStyle exists on the window object as well. It’s likely the defaultView pattern was some combination of (1) folks not wanting to write a spec for window and (2) making an API that was also usable in Java. However there is a single case where the defaultView’s method must be used: when using Firefox 3.6 to access framed styles.

      https://developer.mozilla.org/en-US/docs/DOM/window.getComputedStyle

  3. Very useful! I ran across this scenerio many times where i wanted to access the psedo element props.

  4. Nice!

    Long time a go I was looking for a way to bind events on pseudo elements, is that posible today?

    Thanks!

    • I don’t believe that’s possible yet Toni!

  5. Thanks for this solution. Was looking for this!

  6. Hal D.

    With content:url(“some image”) as a property Firefox correctly gets that string using any of these three methods:

    getComputedStyle(someelement,"::after").getPropertyValue("content")
    getComputedStyle(someelement,"::after").content
    getComputedStyle(someelement,"::after")..getPropertyCSSValue("content").cssText
    

    None of these methods work in Chrome, they either return null or undefined. What gives?

  7. gaurav

    Is there a way, we can set the pseudo style value back in css?

  8. sgaawc

    Pseudo Element’s integration with Javascript is new to me. I will make sure to give it a try and see how it works with diff browsers. but I wonder if there is any way to associate handlers to these elements.

    Thanks.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!