Logo

dev-resources.site

for different kinds of informations.

How to order attributes on HTML elements

Published at
1/6/2025
Categories
html
webdev
ruby
rails
Author
railsdesigner
Categories
4 categories in total
html
open
webdev
open
ruby
open
rails
open
Author
13 person written this
railsdesigner
open
How to order attributes on HTML elements

This article was originally published on Rails Designer


The order of attributes on HTML elements may seem like a thing you have never thought or cared about. But having a guideline helps keep your code consistent and have yet one other thing you don't have to think about.

Below is the order I am using. Share it with your team and use it as a guideline. I won't say this is the only right way, but it's the one I've been using successfully with numerous projects over the last decade (👨‍🦳).

I use the following grouping order:

  1. identifier
  2. functional
  3. styles

In terms of the actual attributes per group:

Identifier

This is only the id-attribute. I always put it first. By definition it has to be unique, making it important enough. Using Turbo Streams you also want to keep easily track of the id-attributes.

Potentially the name attribute would also qualify.

Functional

This is a big group of attributes and the most fuzzy as they depend on the kind of HTML attribute. But within this group a distinction can be made between attributes within a form that all have different values and those who have not.

The variable attributes go before the static attributes.

Examples for variable attributes:

  • value
  • checked
  • selected

And examples static attributes:

  • type
  • for
  • required
  • required
  • readonly

Data attributes

As you likely use Stimulus, I want to highlight the order of my data-attributes as well.

  1. controller
  2. targets
  3. outlets
  4. values
  5. actions

This is based on the amount of needing to look up a value for an attribute.

Styles

Anything that is for visuals of the element.

  1. class
  2. style

Style comes after class as it could “override” styling. It's not really overriding, as the actual rendering and application of styles depends on CSS specificity rules.

Automate

I personally don't use a tool like erb-lint, but I guess it could be added as a linter. Reach out if you know, would be interested to see if a PR makes sense (or is possible at all).

And that's it. As often with such articles, you may disagree or not think this is worth it at all. That's cool too. For me, like Rails, anything I don't have to think about, leaves me with mental space to think about the business logic of my apps.

rails Article's
30 articles in total
Favicon
Rails Self-Join Tables - Parent-Child Magic
Favicon
Ruby on Rails 8 API not allowing mobile phone connection
Favicon
Unable to find Ruby class that definitely exists
Favicon
Ruby on Rails: Your Service Layer is a Lie
Favicon
Devise not accepting JSON Token
Favicon
Ruby on Rails - Calculating pricing based user's purchasing power parity
Favicon
Ruby on Rails 8 - Frontend Rápido Usando Tailwind como um Frameworks CSS Classless
Favicon
Use cases for Turbo's Custom Events
Favicon
Docker in development: Episode 4
Favicon
[Part 1] Rails 8 Authentication but with JWT
Favicon
Easy Custom Pagination: Paginator Fancinator!
Favicon
Just committed to learning ruby for sonic pi and rails https://dev.to/highcenburg/2025-roadmap-mastering-ruby-for-sonic-pi-and-rails-696 wish me luck!
Favicon
Best Tech Learnings of 2024
Favicon
Rails 8 CRUD: Modern Development Guide 2025
Favicon
When Controllers Take on Too Much Responsibility
Favicon
Ruby on Rails 8 - Frontend Rápido com Frameworks CSS Classless ou Class-Light sem CDN
Favicon
Docker in development: Episode 3
Favicon
Brakeman LSP Support
Favicon
Release 0.4 Release
Favicon
Ruby on Rails for AI Chatbot Development: Why it is Ideal Choice in 2025?
Favicon
A Deep Dive into append_view_path and prepend_view_path in Ruby on Rails
Favicon
Rails Testing for Financial Operations
Favicon
Docker in development: Episode 2
Favicon
Rails transactional callbacks beyond models
Favicon
Deploying Rails 8 Applications: A Complete Guide with Docker, Kamal, and Cloudflare
Favicon
Add Invite to Rails 8 Authentication
Favicon
Vaga Desenvolvedor Jr - Ruby on Rails - Híbrido
Favicon
Kamal 2 Quick Start - the missing tutorial
Favicon
How to order attributes on HTML elements
Favicon
Release 0.4 Progress

Featured ones: