views:

50

answers:

2

I am trying to style a element with the :after CSS property

a.someSelector{position:relative; z-index:1;}
a.someSelector:after{position:relative; z-index:0; content:" "; position:absolute; width:100px; height:100px;}

It seems like the :after element can not be lower then the element itself.

Is there a way to have the pseudo element lower then the element itself?

+1  A: 

There are two issues are at play here:

  1. The CSS 2.1 specification states that "The :before and :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element." Given the way z-indexes are implemented in most browsers, it's pretty difficult (read, I don't know of a way) to move content lower than the z-index of their parent element in the DOM that works in all browsers.

  2. Number 1 above does not necessarily mean it's impossible, but the second impediment to it is actually worse: Ultimately it's a matter of browser support. Firefox didn't support positioning of generated content at all until FF3.6. Who knows about browsers like IE. So even if you can find a hack to make it work in one browser, it's very likely it will only work in that browser.

The only thing I can think of that's going to work across browsers is to use javascript to insert the element rather than CSS. I know that's not a great solution, but the :before and :after pseudo-selectors just really don't look like they're gonna cut it here.

Gabriel Hurley
+1 Thanks, especially for this nice explanation!
adardesign
+1  A: 

Speaking with regard to the spec (http://www.w3.org/TR/CSS2/zindex.html), since a.someSelector is positioned it creates a new stacking context that its children can't break out of. Leave a.someSelector unpositioned and then child a.someSelector:after may be positioned in the same context as a.someSelector.

cweider
Thanks, Good point!
adardesign