I’m writing a jQuery script that helps make old sites responsive by applying a fluid grid system to them.
It’s all working pretty well, except for a few things. Firstly, the plugin and stylesheet are called at the end of the page so the CSS styles SHOULD override whatever else is on the page. However (I think this is due to specificity) when I have:
<div id="header" class="span4">
#header {
float: right;
width: 960px;
margin-left: -5em;
}
[class*="span"] {
display: block;
float: left;
width: 100%;
margin-left: 2.564102564102564%;
}
The #header styles take precedence, even though the .span styles are lower in CSS. I know it’s generally seen as bad form, but is the best/only way to combat this to add !important to each style from my plugin’s stylesheet? They are important and the whole point of using the plugin is to override your current stylesheet, but I’m wondering if there’s a better way.
An imperfect solution would be to wrap the inner html of the body tag with 2-4 div’s with different id’s, so that the css from the fluid css file is more specific:
HTML:
CSS:
Here’s a demo: http://jsfiddle.net/j5gt7/
For more info you can read http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
This solutions isn’t perfect because the element could have inline styles, or the main css may have selectors with even more then 2-4 IDs which will override the fluid css (or rules set with !important).