Often when writing an HTML page we resort to div and span, whereas there are many other HTML tags that we just tend to overlook or forget.

Writing semantic HTML means actually using the right HTML tags for different parts of a page.


Semantic HTML is important for accessibility, screen readers, SEO. And just looks more professional ;)

Good resources on writing semantic HTML:

Example of utilizing semantic HTML in a Ruby on Rails app:

  • <header> for logao and navigation
  • <main> for <%= yield %>
  • <footer> - sitemap, copyright, author, contact, sitemap, back-to-top
# app/views/layouts/application.html.erb

    <%= render "shared/navbar" %>
    <%= render "shared/flash" %>
    <%= render "shared/sidebar" %>
    <%= yield %>
    <%= render "shared/footer" %>
  • <nav> to define navbar
  • unstiled list of links inside the <nav> for navigation
# app/views/shared/_navbar.html.erb
      <%= link_to "Home", root_path %>
      <%= link_to "Posts", posts_path %>
  • <aside> for sidebar (related links, advertisements)
# app/views/shared/_sidebar.html.erb
  • <section> should usually have a heading <h1>-<h6>
# app/views/posts/index.html.erb
  <%= render @posts %>
# app/views/posts/show.html.erb
  <h1>Post <%= @post.id %><h1>
  <%= render @post %>
# app/views/posts/new, edit
  <h1>Create/Edit post<h1>
  • <article> - an object that makes sence outside of other context
# app/views/posts/_post.html.erb
  <%= @post.title %>
  <%= @post.body %>

The above is my current-best approach.

If you have any suggestions on how to make it better, please tell me!