I am using :after
and :before
CSS pseudo elements and it is working fine in Internet Explorer 8, and all modern browsers but it is not working fine in Internet Explorer 7. Are there known hacks to work around this in Internet Explorer 7?
7 Answers
with any pure CSS hack it's not possible.
Use IE8.js http://code.google.com/p/ie7-js/
It has support for this. http://ie7-js.googlecode.com/svn/test/index.html
test page also there
after - http://ie7-js.googlecode.com/svn/test/after.html
before - http://ie7-js.googlecode.com/svn/test/before.html
Edit after 1st comment
You can just keep this js for IE6 and 7. other browser will not read it.
<!--[if lt IE 8]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE8.js"></script>
<![endif]-->
And if you are already using jQuery in your project than you can use this plugin
jQuery Pseudo Plugin

- 148,487
- 229
- 573
- 852
-
Thanks for reply.This is the way we can solve this problem. I didnt want to load any external java script file. So is there any other way to do this. – Soarabh Nov 15 '10 at 06:59
-
@saorabh - I've updated the answer. with pure CSS I think it's not possible for IE6/7. If you can provide the code it can be useful to understand the scenario – Jitendra Vyas Nov 15 '10 at 07:12
-
Depending on what you need to do with before and after, it may be possible to use CSS only. Answer with Gist posted below. – CoryDorning Jun 07 '12 at 16:12
-
Question to Jitendra...I added The Jquery Pseudo on my jquery...working good but when i added on the my server it works well on homepage (IE7) url-homapage/images/image.jpg....but when i go to another page the pseudo does the path like this: url-homepage/about/images/image.jpg...how can i prevent this..I assume its the pattern on the js.. – Riskbreaker Feb 21 '13 at 19:11
I was using IE8.js (http://code.google.com/p/ie7-js/) in a project and I had to remove it because it blocked IE7 between 10/15 secs.
To mantain the content generated with :after and :before pseudo-elements, without IE8.js, I did the following:
.tabs {
*zoom: expression(
this.runtimeStyle.zoom="1",
this.appendChild( document.createElement("small") ).className="after"
);
}
.tabs:after,
.tabs .after {
content: '';
display:block;
height:10px;
width:100%;
background:red;
}
I prefer this way rather than with javascript, because this will keep all selectors in the same place :)

- 991
- 7
- 11
-
1
-
The one thing that doesn't seem to work if you are targeting an `
` tag with this. The expression doesn't run for some reason. – dmackerman Jan 31 '14 at 15:16
-
tag doesn't work because this is putting the after inside the tag (appendChild), which you can't for things like img. – fanfavorite Sep 23 '14 at 16:38
To before and after you can use this:
.tabs {
*zoom: expression(
this.runtimeStyle.zoom="1",
this.insertBefore(
document.createElement("div"),
this.childNodes[0]
).className="before",
this.appendChild(
document.createElement("div")
).className="after"
);
}
...
.tabs::before, .tabs .before {
display: block;
width: 10px;
height: 20px;
background-color: #eee;
float: left;
}
.tabs::after, .tabs .after {
display: block;
width: 10px;
height: 20px;
background-color: #c0c;
float: left;
}

- 2,782
- 27
- 36
If you're already using Modernizr, it has a core detect for "CSS Generated Content".
You can then fill in the missing :before
or :after
using JavaScript. For example:
.selector:before, .selector-before {
content: "Hello, world!";
color: red;
}
jQuery to insert generated content directly into the DOM:
if (!Modernizr.generatedcontent) {
$('.selector').prepend('<span class="selector-before">Hello, world!</span>');
}

- 90,923
- 26
- 142
- 180
Well there is a pure CSS hack that works, sort of. It's black magic but is sometimes handy when used scarsely.
It's explained here: http://nanobox.chipx86.com/blog/2006/07/before-and-after-in-ie7-and-below.php http://web.archive.org/web/20080706132651/http://nanobox.chipx86.com/blog/2006/07/before-and-after-in-ie7-and-below.php
HTML fragment:
<div id="container">
<!-- -->
<div class="mainContent">
<p>Blah blah</p>
<p>Blah blah</p>
<p>Blah blah</p>
<!-- -->
</div>
</div>
CSS:
#container:before
{
background: url("corners-top.png");
content: "";
display: block;
height: 24px;
}
#container .mainContent:after
{
background: url("corners-bottom.png");
content: "";
display: block;
height: 28px;
}
IE-specific stylesheet:
#container *
{
background: url("corners-top.png");
display: list-item;
font-size: 24px;
line-height: 24px;
list-style: none;
}
#container .mainContent
{
background: none;
display: block;
font-size: 1em;
line-height: 1.25;
}
#container .mainContent *
{
background: url("corners-bottom.png");
font-size: 28px;
line-height: 28px;
}
/*
Now, still in the IE-specific stylesheet, remove the styles for all
element descendants of .mainContent. Refer to each element by tag name.
*/
#container .mainContent p, #container .mainContent div, (And so on...)
{
background: none;
display: block;
font-size: 1em;
line-height: 1.25;
}
-
@bazmegakapa Here's the [wayback machine archive.](http://web.archive.org/web/20080706132651/http://nanobox.chipx86.com/blog/2006/07/before-and-after-in-ie7-and-below.php) – d4n3 Jan 26 '12 at 09:01
-
You can use CSS expressions to do this. For example you could plug a ♣ symbol via:
expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = '♣'+this.innerHTML)
I wrote an article about this on Smashing Magazine, see http://coding.smashingmagazine.com/2011/03/19/styling-elements-with-glyphs-sprites-and-pseudo-elements/

- 77,694
- 21
- 158
- 175

- 201
- 1
- 4
-
-
12I think using IE7 would be a greater cause of slowness. CSS expressions are only supported by IE, and they were dropped in IE8. If you are concerned about speed, get a better browser. If you want to support ancient clients, use slow hacks. – whitehat101 Jul 03 '12 at 07:18
-
5@Jeremy You shouldn't make slower browsers even slower. Your users are not your enemies. – Jon Raasch Sep 20 '12 at 22:10
-
6
-
This method seems to create multiple characters in IE7, as if IE7 is looping the content being set with `this.innerHTML`. Anyone having this same problem with this code? – Jasdeep Khalsa Nov 21 '12 at 16:54
When needing support for :before and :after I use the following gist that I wrote. It's pure CSS (to the point were you just write CSS) but does include a CSS expression. Works in most cases.
https://gist.github.com/2362483
/* =============================================================================
CSS Declarations
========================================================================== */
/* ==|== The Standard Way =================================================== */
.foo::before {
/* ...css rules... */
}
.foo::after{
/* ...css rules... */
}
/* ==|== The IE Way =================================================== */
/* NOTE: a comma separated IE & Standard rule won't work. *
* IE doesn't understand ::before or ::after & ignores the declaration */
.lt-ie9 .foo:before,
.lt-ie8 .foo .ie-before {
/* ...css rules... */
}
.lt-ie9 .foo:after,
.lt-ie8 .foo .ie-after {
/* ...css rules... */
}
/* =============================================================================
IE6 & IE7 polyfills
========================================================================== */
.lt-ie8 .foo {
zoom: expression(
this.runtimeStyle.zoom="1",
/* ::before polyfill - creates <i class="ie-before"></i> */
this.insertBefore( document.createElement("i"), this.firstChild ).className="ie-before",
/* ::after polyfill - creates <i class="ie-after"></i> */
this.appendChild( document.createElement("i") ).className="ie-after"
);
}

- 90,923
- 26
- 142
- 180

- 1,854
- 4
- 25
- 36
-
-
point taken...but the OP is looking for 'hacks' to solve the issue in IE. – CoryDorning Jun 12 '12 at 16:58