You read that right. I said override inline styles, and no, I have not been drinking. For the longest time it has bugged me that there was no way to override what someone else had done with inline styles. I understand they’re supposed to be the override, and I get why, to a certain extent, but I also think there should be an override to certain overrides when those overrides are abused. Make sense?
Why the need to override them at all?
Mostly it has to do with my own curiosity and the “can it be done” approach with which I come at everything, but I have had a few clients over the years who (bless their hearts) love to get a little creative styling their articles. One will add a bright green paragraph, centered in some font that might as well be comic sans, or another will use an h1 to make the text really big and then style it bold florescent something. All of this, of course, is done within the confines of my nicely styled template I just built for them (and which they paid for).
To be fair, in most of these cases, it’s the CMS doing the dirty work. They’re just clicking a button to make something bigger or more colorful and Wordpress (typically) or another CMS engine is working behind the scenes, adding a span usually, with a style applied. Occasionally, the CMS will add a style to a heading or a paragraph, but in most cases, the following will solve the problem (or prevent the problem):
/* override inline styles */
span[style^=""]{
color: inherit !important;
font-size: inherit !important;
}
UPDATE: Stuart and John say a simple [style] will work and include IE7&8 too! - check out the comments for more suggestions and improvements to this solution. Nathan had a good idea for covering all the bases.
/* override inline styles */
span[style]{
color: inherit !important;
font-size: inherit !important;
}
Explanation
The first line tells the browser (any browser that’s listening, that is) to look for any span element span with a style attribute [style containing anything ^=""] and then stop and do the following {.
Then, I tell it to inherit the color and font-size from its parent element. The browser (a smart one, mind you) will work backwards up the cascade and find the last styled element with a color or font size applied and use that instead. By declaring it !important I’ve told the browser to ignore anything except this declaration I’ve just made.
Of course, change span to any other element that may have a style to it. For that matter, you could likely control any attribute of any element on the page using this same technique, no?
How this came about?
I have been using this technique on links for a couple of years now. I forget now where I picked it up, but it was originally used to style external links: a[href^="http://"] will allow you to style external links separately so long as your internal links are relative urls.
Then I saw the technique again around the same time, being used by A List Apart to call out the href contents in a print stylesheet (print this page and you’ll see I’ve done this on my site as well):
.storycontent a[href]:after {
content: “<” attr(href) “>”;
font-size: 85%;
text-decoration:none;
}
So today, while I was watching the Chronicles of Narnia (of all things) with the girls, it occurred to me that this could possibly be used to fix my inline style override issue. During a commercial I tried it out and by the end of the commercial I was on my way to this blog post.
Browsers?
Of course, just like the link trick mentioned above, this works in nearly every browser except IE 6 - any IE browser. I’m told IE 7 & 8 will work with the change above.
Good to go
- Safari 3 Mac
- Firefox 1.5 Mac and PC
- Opera 9.5
- IE 7
- IE 8
No dice, no surprise
- IE 6
On the bright side…
This isn’t something that is necessary in every browser anyway, it’s just an extra thing that can come in handy, and I hope you’ll find some (productive) use for it yourselves.
And if this technique happens to offend you or scream in the face of all you hold dear in web standards, please forgive me, I’m a bit of a control freak. :)





Josh H. left a comment on May 5, 2008 at 2:17 pm | #
I wonder if using some unobtrusive javascript from jQuery could overcome the browser issue (Just thinking out loud, mind you). Perhaps just search for all the spans with style=”" and strip them out? Of course they may have other styles associated with the span so maybe not. Either way good tip!
Natalie left a comment on May 5, 2008 at 4:22 pm | #
Hey Josh that would work, I’m sure, though you’re right about it limiting all inline styles and that’s taking it too far even for me. ;) That’s what I love about the cascade and the inherit value. You don’t have to strip the inline styles of style completely and you don’t have to specify anything at all. In my experimentation I started out trying to specify the color and font-size of these spans but in a “duh” moment I remembered the inherit value which would just grab the color/size from its parent, so much nicer.
Thanks for the comment - it’s good to think about these things.
christa left a comment on May 6, 2008 at 5:23 am | #
I have absolutely no idea what this post is about, but I’m so intrigued by the language. That you can have these conversations and they are as clear to you as 2+2=4 is to me, well, I’m amazed.
Stuart W left a comment on May 8, 2008 at 1:49 pm | #
Would this be sufficient for the selector?
span[style]Natalie left a comment on May 8, 2008 at 2:02 pm | #
Stuart thanks, that would make sense, but I don’t know if it would work because I didn’t try it. Why don’t you and let us know. It seems logical enough.
John Lascurettes left a comment on May 8, 2008 at 2:47 pm | #
To echo what Stuart said:
span[style]should be all you need and it will support IE7 and 8.That said, I can’t tell you how many times I run into “!important” in inline styles and those cannot be overwritten.
Natalie left a comment on May 8, 2008 at 7:41 pm | #
Thanks, John, I’m glad to hear that solution works — and solves part of the IE issue. Although, it’s strange they would go to the trouble to get [style] to work and not expand it to include specific styles.
Sean left a comment on May 8, 2008 at 8:17 pm | #
Most text editors in a CMS have the ability to filter tags. Set it up so that span tags are removed, and you should be good to go.
Nathan Smith left a comment on May 8, 2008 at 8:37 pm | #
I wonder if that could be written with a greater level of descendency, to heavy-handedly override all inline styles…
html body *[style] { … !important; }
Anyway, haven’t tested it, just thinking aloud.
John Lascurettes left a comment on May 8, 2008 at 8:57 pm | #
The reason why you’d want to do a simple attribute selector of
span[style]instead of one of the advanced attribute selectors is that you don’t know in what order and what other CSS properties will be in that span. For example:span[style^=foo]would match a span withstyle="foo bar"but would not matchstyle="bar foo"And in your case, since you were doing
span[style^=""]you were asking for (to put it in natural language) “a span with a style attribute that starts with [blank].” I say “blank” because you had two double quotes. IE was probably seeing that as “a span with a style attribute that starts with a double-quote.” You essentially were just simply trying to say, “a span with a style attribute.” That’s the syntax Stuart gave.If you want a little more schooling on attribute selectors, see these:
Daniel Marino left a comment on May 9, 2008 at 7:25 am | #
It’s funny because I recently wrote a journal entry about how I hate it when CMS’s allow users to markup their articles using inline styles (or invalid/depreciated tags). Now I can finally put a stop to my client’s ruining their websites! Thanks for the great tip!
Natalie left a comment on May 9, 2008 at 7:25 am | #
Nathan I wondered that last night but don’t know when I’ll have time to try it. It was a fluke I had the 5 minutes to escape to get as far as I did! :) It would make sense not to have to do this for every element that could have a style attached, but for now dealing with spans is enough for me just because I’m mostly dealing with CMS’ that put styles on spans before anything else. Although I saw some code recently in a consulting job - this was for some text she wanted to highlight, but not really semantically in any hierarchy:
<h1 style="text-align:center; color: #0000ff;">Of course, using Eric Meyer’s reset made the size normal on the h1, but then it was bright blue right in the middle and looked really strange against the color scheme. I left the text-align, but was able to make the color match the text around it.
If I can get some time I’ll try it, but if you do, please do email me or come back here and let me know. That seems like it would be the best solution to me.
Natalie left a comment on May 9, 2008 at 7:30 am | #
Hey, Sean, thanks for that, I didn’t know about the option to strip tags, though I can’t think of anywhere in Wordpress specifically that allows that. I’ll have to look into it sometime.
The trouble I see in stripping tags though is in cases where:
style="text-align:center"to something and for the end user there’s no better way to center something.I’d much rather see that than
<center>tags.So I want to leave spans in, and I want to leave them able to be styled. I just want to be able to decide which properties will be inherited on which elements.
Natalie left a comment on May 9, 2008 at 7:38 am | #
Thanks again, John for more clarification. I appreciate you taking the time to expand on this for us. Everything you said makes sense and had I not been doing this during a commercial in a movie, I might have come up with better. And I absolutely want more schooling… always! :)
links for 2008-05-10 « tscpannex left a comment on May 9, 2008 at 6:49 pm | #
[...] Standards for Life by Natalie Jost » Override Inline Styles from the Stylesheet Override Inline Styles from the Stylesheet (tags: CSS hacks) [...]
Max Design - standards based web design, development and training » Some links for light reading (13/5/08) left a comment on May 13, 2008 at 6:15 am | #
[...] Override Inline Styles from the Stylesheet [...]
Richard Morton - QM Consulting Ltd left a comment on May 13, 2008 at 7:39 am | #
Interesting, the battles between developers and the CMS. The answer is obviously for the two to work together in some way (but that is probably just cloud cuckoo land). Some CMSs are more standards compliant than others and will avoid using deprecated elements and attributes, but even if they do, there is still scope for overriding styles. Do you use this code for elements other than span as well, or do you find that just isn’t necessary?
Parrfolio left a comment on May 13, 2008 at 9:16 am | #
This is a big pet peeve of mine when using .NET controls. Form validation controls in .NET produce nasty inline styles for required fields. Example:
So I have been using this declaration to make them somewhat less nasty but still never could overwrite these inline styles.
I never thought of using inheritance. This is great! I added this to my reset style sheet but changed the span to a universal selector.
Nice job! Thanks for sharing.
Natalie left a comment on May 13, 2008 at 9:24 am | #
Richard, I have used this on h1-h6 in some cases where a particular user has a habit of using them to enlarge text-size because in many cases the user is also adding a color or other style to the “larger” text.
Parrfolio, thanks for sharing that. There was other discussion about using
*[style]but I still haven’t been able to test it so I’m glad to hear it has worked for you.