The sequence of HTML attributes

Could there be a subjective angle to this question (or maybe not)... I work on crafting web designs and applications using Visual Studio and typically Bootstrap. When I drag and drop a CSS file into an HTML document, the code generated by Visual Studio looks like this:

<link href="styles.css" rel="stylesheet" />

The attribute order in the Bootstrap template follows a similar pattern.

Preferring to organize my attributes in a certain way for clarity, I choose to position fixed width attributes at the beginning. Let's compare my ordering with Visual Studio & Bootstrap:

Mine

<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="mystyles.css" />

<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="foobar.js"></script>

Theirs

<link href="bootstrap.min.css" rel="stylesheet" />
<link href="mystyles.css" rel="stylesheet" />

<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="foobar.js" type="text/javascript"></script>

Notice how the attributes in my link and script tags are aligned? This cleaner layout facilitates better document maintenance and editing.

Now, the burning question is — is this simply a matter of personal preference or is there a legitimate rationale behind placing rel and type after href and src?

Answer №1

According to the guidelines set forth in the HTML 4.01 specification:

Elements can possess attributes, known as properties, which may be assigned values (either by default, or through authors or scripts). Attribute/value pairs are positioned before the element's closing ">". A variety of attribute value pairs, separated by spaces, can be included within an element's starting tag. These pairs do not have a specific order.

Although the language used in the HTML 5 spec is not as explicit on this matter, the rule remains unchanged.

This is simply a matter of personal preference.'

Answer №2

When it comes to HTML parsers, the sequence of attributes doesn't really have an impact. It all depends on your personal preferences. However, these parsers always organize and display attributes in alphabetical order. Even if you place them in a different order in your source code, the browser will still showcase them in alphabetical order in the developer view.

Answer №3

The specification of HTML5 states that the sequence of attributes does not have a significant impact, as mentioned in the previous responses. However, if this is true, there would be no necessity to align the code properly since programming languages like Java do not emphasize indentation.

It is advisable to adhere to the existing conventions followed in the project repository. For newcomers, the coding guidelines listed below establish an order for elements that numerous individuals are currently adhering to.

Order of Attributes:
According to this guideline, HTML attributes should be arranged in a specific order to enhance code readability.

class
id, name
data-*
src, for, type, href, value
title, alt
role, aria-*

Classes contribute to creating reusable components, hence they take precedence. Ids are more particular and should only be utilized when necessary (e.g., for internal bookmarks), placing them after classes.

Source:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Image caption dynamically updated to match thumbnail caption using jQuery upon click event

My goal is to dynamically load the data-caption of thumbnail images when clicked, and then update the main image's data-caption when the main image is changed with a thumb image. I am currently struggling to make the data-caption update along with the ...

What is the method for creating a rectangle with text inside using HTML and CSS?

Hello, I need help with achieving this design using HTML and CSS. Can you assist me, please? https://i.stack.imgur.com/xOHVX.jpg This is what I have attempted so far: Below is my CSS code: .line-lg{ width:120%; text-align: center; border-bot ...

Can you help me identify the issue with my current Jade for loop implementation?

Here is my full loop code written in Jade: block content div(class='row') div(class='col-lg-12') h1(class='subject') 로또라이 div(class='row') div(class='col-lg-8') - for (var ...

Navigating with Angular Material and md-nav-bar for efficient routing

Currently, I am delving into Angular and have opted to utilize the Angular Material library for my initial application. After tweaking some basic code borrowed from this source, which I adjusted to suit my requirements, I encountered difficulties with rout ...

What are the steps to include a string into Vue and then assess its value?

Where should I define a function in a SPA using the options API that will be called from an HTML href? Check out this demo on Codepen where everything works fine: CODEPEN However, when I try to implement it in my single-page application: <templat ...

Discover the secret to creating a seamless looping effect on text using CSS gradients, giving the illusion of an endless loop

Could use some assistance with looping this gradient smoothly over the text without any annoying jumps appearing during the animation. Is there a way to achieve a seamless movement across the text? Any suggestions on how to approach this? Here is a liv ...

retrieving attribute values from JSON objects using JavaScript

I am struggling to extract certain attribute values from a JSON output and use them as input for a function in JavaScript. I need assistance with this task! Below is the JSON data that I want to work with, specifically aiming to extract the filename valu ...

React/Redux encountered a roadblock when attempting to iterate through an array stored in the state

Currently, I am delving into the world of React/Redux and have encountered a stumbling block while transitioning one of my single-page web applications to this framework. What I aim to achieve is to populate an array in the initial state of the web app wit ...

Tips for detecting parallel processes using Node/JS programming language

Many software packages claim to execute their methods in parallel, such as the async npm package. They assert that even for functions like concat, they are processed concurrently. How can we verify if their assertion holds true? I have written code to te ...

Is it necessary to have n_ if I've already set up lodash?

After some research, I came across a recommendation to install lodash. However, upon visiting the lodash website, they suggest that for NodeJS, n_ should be installed instead. Are both necessary? Is one more comprehensive than the other? Do I even need eit ...

How do I define the specific icon to display on the splash screen for progressive web apps?

In my Progressive Web App (PWA), I have set icons at sizes 144 and 512. Although both icons appear in the application tab in Chrome, the splash screen displays a really small icon (I assume it's using the 144 icon). Is there a method to indicate which ...

Extracting null data from web scraping using Python and Beautiful Soup

I'm currently facing challenges with extracting the correct values from a website that provides prices for Silver, Gold, Palladium, and Platinum. You can find the website here. Below is the HTML structure of the website: <div id="header-tab ...

Is it possible to display multiple qTips for the same target using jQuery qTip2?

I am currently utilizing the jquery qtip2 plugin to generate a mouseover tooltip. Here is the code snippet I am using: $(document).ready(function() { $("#optionalProdsImgPreview_1").qtip({ content: "<img src='http://mysite.com/myimg.jpg&ap ...

Exporting stylesheets in React allows developers to separate

I am trying to figure out how to create an external stylesheet using MaterialUI's 'makeStyles' and 'createStyles', similar to what can be done in React Native. I'm not sure where to start with this. export const useStyles = m ...

Developing a progress bar with jQuery and Cascading Style Sheets (

Below is the code I'm currently using: <progress id="amount" value="0" max="100"></progress> Here is the JavaScript snippet I have implemented: <script> for (var i = 0; i < 240; i++) { setTimeout(function () { // this repre ...

Utilizing data attributes for storing and selecting dual prices on an option

I need help creating a form with options that have two potential values. The user will first choose between "Low Price" (lp) or "High Price" (hp), and then select either Type 1 or Type 2, both of which have data attributes for "hp" and "lp". My goal is to ...

I am having trouble with my Vue nested For loop as it is only returning the first value of the second array. What could be

I am currently utilizing a nested For loop to retrieve data from JSON and then returning a variable for Vue frontend access. Oddly enough, I am only able to retrieve values from the initial element of the nested array. Can anyone assist with this issue? It ...

Using a dashed border stroke creates a unique look when combined with border-radius properties

I have been experimenting with different methods to increase the distance between border strokes on my element. So far, I have tried various approaches without success. For example, I explored this resource Unfortunately, the issue arises when I incorpor ...

Creating a cascade of falling balls with a single click: Here's how!

I'm currently working on a project where I have a ball dropping from the cursor location and redropping when the cursor moves to another position. However, I want to be able to create a new ball every time I click the mouse. I attempted the following ...

Encountering difficulties when attempting to store files using mongoose in a node express.js program

I encountered an error while attempting to save a document to the MongoDB using Mongoose in my Node Express.js project. Below is the code snippet: exports.storeJob = async (req, res, next) => { const { name, email, password, title, location, descri ...