59

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?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Soarabh
  • 2,910
  • 9
  • 39
  • 57

7 Answers7

69

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

http://jquery.lukelutman.com/plugins/pseudo/

Jitendra Vyas
  • 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
52

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 :)

vieron
  • 991
  • 7
  • 11
4

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;
}
atiruz
  • 2,782
  • 27
  • 36
3

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>');
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
3

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;
}
kapa
  • 77,694
  • 21
  • 158
  • 175
Jens
  • 304
  • 1
  • 2
  • 10
  • @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
  • @d4n3 Thanks. Imported the most important parts from the archive. – kapa Jan 26 '12 at 09:34
3

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/

kapa
  • 77,694
  • 21
  • 158
  • 175
  • css expression are black list at yslow rules – Jitendra Vyas Jun 07 '12 at 17:37
  • 12
    I 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
    Correct! That's why we're supporting IE7 in the first place. – whitehat101 Oct 07 '12 at 07:13
  • 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
2

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"
  );
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
CoryDorning
  • 1,854
  • 4
  • 25
  • 36