Fixing YUI Reset CSS Issues: How to Keep Your <em>
Italic and <strong>
Bold
When working with web development, you may find yourself using YUI’s Reset CSS for a consistent starting point across browsers. While this can be beneficial, it may also lead to unexpected behavior in your text formatting. A common issue arises with how the Reset CSS modifies the <em>
and <strong>
tags, making them not display as intended.
This blog post will walk you through the issue step-by-step and provide a clear solution so you can keep your web content looking sharp.
Understanding the Problem
YUI’s Reset CSS overrides default browser styling, which can sometimes lead to complications:
What’s Happening?
The specific line in the YUI Reset CSS that causes trouble is:
address,caption,cite,code,dfn,em,strong,th,var {
font-style: normal;
font-weight: normal;
}
As a result, both the <em>
and <strong>
tags lose their default formatting:
<em>
tags, which are supposed to be italicized, appear as normal text.<strong>
tags, meant to be bold, also display in a standard font weight.
The Conflict
While you may be able to manually reset the formatting for these tags in your own stylesheet using:
strong, b {
font-weight: bold;
}
em, i {
font-style: italic;
}
The problem intensifies when you use both tags together. For instance:
<strong>This is bold, <em>and this is italic, but not bold</em></strong>
Here, even though your styling rule for <strong>
works, the Universal Reset CSS rule resets the <em>
and negates the italic effect.
The Solution
To ensure that your custom styles take precedence over the YUI Reset CSS, you will need to adjust your CSS rules. Here’s how you can do that:
Step 1: Override with Specificity
You can increase the specificity of your styling rules as follows:
strong, b, strong *, b * {
font-weight: bold;
}
em, i, em *, i * {
font-style: italic;
}
This modification ensures that not only the <strong>
and <b>
elements are styled appropriately, but any nested elements within them will also carry the same style.
Step 2: Forcing Styles with !important
If you are still supporting older browsers like IE7, you may need to force your styles using !important
. This can be done as such:
strong, b, strong *, b * {
font-weight: bold !important;
}
em, i, em *, i * {
font-style: italic !important;
}
Step 3: Test Your Styles
Once you’ve implemented these changes, ensure you test your styles both in modern and legacy browsers to confirm that everything displays as expected.
Here’s a quick example for you to try:
Example CSS
/* YUI styles */
address,caption,cite,code,dfn,em,strong,th,var {
font-style: normal;
font-weight: normal;
}
/* Your styles */
strong, b, strong *, b * {
font-weight: bold;
}
em, i, em *, i * {
font-style: italic;
}
Example HTML
<strong>Bold</strong> - <em>Italic</em> - <strong>Bold and <em>Italic</em></strong>
Conclusion
By following the steps outlined in this blog, you can successfully override the default behavior introduced by YUI’s Reset CSS, keeping your <em>
and <strong>
tags styled correctly.
If you experience issues like this in the future, remember that specificity and the !important
directive can be powerful tools in your CSS styling arsenal.
Happy coding!