Get Pseudo-Element Properties with JavaScript
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!
![Responsive and Infinitely Scalable JS Animations]()
Back in late 2012 it was not easy to find open source projects using requestAnimationFrame()
- this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...
![Write Simple, Elegant and Maintainable Media Queries with Sass]()
I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...
![Style Textarea Resizers]()
Modern browsers are nice in that they allow you to style some odd properties. Heck, one of the most popular posts on this blog is HTML5 Placeholder Styling with CSS, a tiny but useful task. Did you know you can also restyle the textarea resizer in WebKit...
![CSS :target]()
One interesting CSS pseudo selector is :target
. The target pseudo selector provides styling capabilities for an element whose ID matches the window location's hash. Let's have a quick look at how the CSS target pseudo selector works!
The HTML
Assume there are any number of HTML elements with...
Yeah, too bad this doesn’t work with IE8 and previous.
currentStyle is good for some things, but… not for this one.
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.
From the MDN documentation:
In many code samples online,
getComputedStyle
is used from thedocument.defaultView
object. In nearly all cases, this is needless, asgetComputedStyle
exists on thewindow
object as well. It’s likely the defaultView pattern was some combination of (1) folks not wanting to write a spec forwindow
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
Very useful! I ran across this scenerio many times where i wanted to access the psedo element props.
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!
Thanks for this solution. Was looking for this!
With content:url(“some image”) as a property Firefox correctly gets that string using any of these three methods:
None of these methods work in Chrome, they either return null or undefined. What gives?
Never mind, I found the issue is the element must not be display:none to access it according to https://code.google.com/p/chromium/issues/detail?id=236603
Is there a way, we can set the pseudo style value back in css?
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.
How would you make a function that could reference
self
?When I place this in a jQuery function and try to replace
$('.element')
with($(this))
, I have issues.Did you ever find a way to use
($(this))
?How about when the desired element is a child of an :active element? Like:
:active
is a pseudo class, not a pseudo element, so you can’t grab it withgetComputedStyle
Great Post! However, the code gives me the certain values like height and top as px in Chrome and as % in Firefox and IE. Anyway to get a consistent result for all browsers?
I am looking to bind event to pseudo classes with vanilla javascript. Isn’t there a hack already
like
getPropertyValue
, is there anysetProperty
methods that can replace the contents?