DEV Community

Berra
Berra

Posted on

1

Highlight heading levels

Headings in HTML are important for many reasons. For SEO, accessibility and overall information structure.

The hierarchy is very important as well. First you must have the h1. And if you need another heading the following must be h2 and so on.

This is an example structure of heading levels.

h1 - Main title  
  h2 - Section title A.
  h2 - Section title B. 
    h3 - Subtitle for Section title B. 
      h4 - Subtitle for the previous h3. 
    h3 - Subtitle for Section title B
  h2 Section title C. 
  h2 Section title D.

Enter fullscreen mode Exit fullscreen mode

How you style the headings are of course up to you. There's nothing to say that the h4 can't be the biggest heading. Or the h1 the smallest. Or that different h2 headings can have different design.

And that is why the following CSS can be used to highlight in your development code what level every heading has.

h1:before, h2:before, h3:before, h4:before, h5:before, h6:before {
  color: red;
  opacity: 1;
  background-color: white;
  z-index: 1;
  position: relative;
  top: -2px;
  left: -2px;
  border: 1px solid red;
  font-size: small;
  padding: 0 1px;
  line-height: 1;
  margin-right: 0;
}
h1:before { content: 'H1'; }
h2:before { content: 'H2'; }
h3:before { content: 'H3'; }
h4:before { content: 'H4'; }
h5:before { content: 'H5'; }
h6:before { content: 'H6'; }
Enter fullscreen mode Exit fullscreen mode

The idea is to add an element before every heading and spell out the heading level to make it visible on the page without checking the source code or dev-tools.

Hope you find this useful.

Until next time!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more