Why use .h1 instead of actual h1?
Within the Bootstrap CSS project, styles are provided for your heading tags (H1, H2, H3, H4, H5, H6), but there's also a series of class names based on the headings as well (.h1, .h2, .h3, .h4, .h5, .h6). What is the benefit gained from using the class name of a H1 without properly using the H1 tag? I would think that you always want to make sure your HTML mirrors your visual importance.
Any thoughts explaining a good reason for using .h1 instead of using a h1 tag would be appreciated.
You ask:
Why use
.h1
instead of actualh1
?
Short answer:
The goal is to use both, together.
The usefulness of the .h*
classes comes into play when the size of the typography in the design does not correlate with the semantically appropriate heading levels. By splitting the problem in two, we can cleanly solve for both.
The first bit is the element/tag. The '<h*>
' takes care of semantics, accessibility and SEO.
The second bit is the class. The '.h*
' takes care of visual semantics and typographical hierarchy.
Long answer:
I believe that the origins of these classes come from OOCSS project:
Object-Oriented CSS
The latest iteration of OOCSS has changed a little since I last looked at it, but here's the relevant heading.css
file, from an older commit, that has the .h1
- .h6
classes that I'm familiar with:
6e481bc18f
oocss / core / heading / heading.css
From the comments:
.h1
-.h6
classes should be used to maintain the semantically appropriate heading levels - NOT for use on non-headings
...
if additional headings are needed they should be created via additional classes, never via location dependant styling
Note the emphasis above.
For a good explanation as to why one would use these classes, see:
- stubbornella.org: Don’t Style Headings Using HTML5 Sections (Nicole, the author of this post, is the creator of OOCSS)
- csswizardry.com: Pragmatic, practical font sizing in CSS
- Google Groups › Object Oriented CSS › Headings question: Basic concept/usage? (A question I asked back in September of '12)
Relevant quotes from the above links:
1. stubbornella.org
... [HTML5] Section elements are meant to help the browser figure out what level the heading really is, but not necessarily to decide how to style it.
So, how do we style headings in an HTML5 world?
... We shouldn’t use sectioning elements for styling. We should let them do the job they were designed for, which is sorting out the document tree, and solve styling issues another way that meets our goals better: with simple reusable class names that can be applied to any of our headings no matter how deep they may be in the sectioning content.
I recommend abstracting site wide headings into classes because then they are portable, predictable, and dry. You can call them anything you like.
2. csswizardry.com
Another absolutely stellar nugget of wisdom [Nicole Sullivan has] given us is what I call double-stranded heading hierarchy. This is the practice of defining a class every time you define a heading in CSS.
... By assigning a class along with each heading style we now have those styles attached to a very flexible selector that can be moved anywhere, rather than to a very specific and non-movable one.
3. groups.google.com
Here's a pseudo-html5 usage example (h/t Jamund Ferguson):
<body>
<div class="main">
<h1>Main Heading</h1>
<section>
<h1 class="h2">Section Header</h1>
</section>
</div>
<aside class="side">
<article class="widget">
<h1 class="h3">Sidebar Headings</h1>
</article>
<article class="widget">
<h1 class="h3">Sidebar Headings</h1>
</article>
</aside>
</body>
Please read full articles (and thread), via links above, for more detailed information related to this question/topic.
Most stylesheets have a set of font-sizes that correspond with heading styles 1 through 6. Sometimes, elsewhere on the page, web designers want to use those same font sizes on text which should not be a part of an actual heading element, for semantic reasons. Rather than providing names for each of those sizes like .size-12
, .size-14
, .size-16
, etc, it's easier just to name them all with class names similar to your headings. That way, you know that the text will be the same size as an H1 element, even though it's not actually an H1 element. I've done this a few times myself.
A few reasons I can think of:
- Using div instead of proper tag to get the visual of a header without an impact on SEO
- To avoid complications from browser inconsistencies
- Compatibility with other libraries and frameworks that choose to do the same for reasons 1 and 2
- Design flexibility (as noted by @scrappedcola in the comments)