A few methods I can think of are in this fiddle: http://jsfiddle.net/YS2MQ/3/ and I added a completely different workaround at the end of this answer.
HTML:
<h1 class="ie8plus">abc</h1>
<h1 class="border">abc</h1>
<h1 class="cc">abc<!--[if lte IE 7]> <span>I'm IE7-, let me die please</span><![endif]--><!--[if gt IE 8]><!--> not IE7- here<!--<![endif]--></h1>
<h1 class="textindent"><span>abc</span></h1>
<h1>.after<span class="after"> </span></h1>
CSS:
body {
margin: 10px;
}
h1 {
float: left;
clear: left;
margin-bottom: 5px;
background: red;
color: white;
text-indent: 15px;
}
.ie8plus:after {
content: '';
margin-left: 15px;
}
.border {
border-right: 20px solid blue; /* you want red obviously. 'transparent' won't work in IE7- so you must have a solid background under h1 */
}
.textindent {
text-indent: 35px; /* already 15px and we add 20px */
}
.textindent span {
position: relative;
left: -20px;
}
.cc span { /* HTML conditional content only interpreted by IE7-. Messy on so many levels... Let's keep the use of conditional comments to conditional stylesheets and conditional classes! */
background-color: yellow;
color: black;
}
h1 .after {
display: inline; zoom: 1; /* that's inline-block for IE7-... and inline isn't required as span is already inline */
width: 20px;
height: 1px;
vertical-align: middle;
background-color: darkred;
}
In words:
- You can add a right border,
- you can abuse conditional comments to force IE7- to see HTML content that other browsers won't see (beware: use it more than once per page and it'll be unmanageable),
- you can enclose text into a
span
and play with text-indent
and relative positioning,
- you can add an empty
span.after
to mimick the pseudo `:after (I left a space in my fiddle but I don't think it's necessary). Yes it's extra-code and blah blah blah BUT if you're forced into supporting old browsers with constraints and methods they don't support, well there are not many options left! ;)
Or:
I see in one of your comment that you've a constraint with width
. Your problem would be solved with the use of the CSS property box-sizing (with appropriate vendor prefixes) where padding and borders can be taken into account of the whole width, not added anymore:
* { box-sizing: border-box }
but that's only supported by IE8+. That's the default behavior of IE7-... without doctype. Oh the irony!
But all isn't lost, there exists a polyfill: boxsizing.htc