1. Home
HTML

Learn HTML: A Comprehensive Tutorial for Beginners | Step-by-Step Guide

Learn HTML from scratch! Our tutorial covers basics to advanced concepts. Start coding websites today with step-by-step guidance.

  • 49
  • 12 Hours
right-top-arrow
5

HTML Attributes: A Comprehensive Overview

Updated on 23/07/2024455 Views

Having worked in web development for a while, HTML attributes style is an indispensable part of my daily toolkit. These seemingly small details play a crucial role in shaping the appearance and functionality of web pages, making them essential in the art of website creation.

Therefore, I’ve brought to you an easy-to-understand tutorial to delve into the heart of the matter relying on firsthand experiences. Having analyzed real-life case studies, I will also provide examples to illustrate the significance of HTML attributes.

Understanding HTML Attributes 

Initially, when I started off HTML projects, I got the first chance to experiment with HTML attributes. Rolling into the very core of the programming, I came to realize the program prerogatives and their diverse features. Among all the features, one is still fresh in my memory - the "href" attribute in anchor tags. Putting on this feature value, and URL value converts that boring text to a clickable link that opens new horizons for user interaction.  

Thus, to clearly put it, HTML attributes are special words used in HTML markup to describe the element. They are set within the opening tag of the element, right after the element's name. Attributes are vital in telling about the features and properties of the web pages and hence the web becomes more realistic and dynamic by this. 

The attribute appears with an opening tag of the element and is typically placed in key-value pairs following a syntax. A syntax is nothing but an attribute name followed by an equals sign and then the attribute value. All of which are enclosed in quote format. For example: 

<!DOCTYPE html>

 <html>

    <head>

       <title>sample title</title>.

    </head>

   <body>

      <a href="https://www.example.com">this link will take you to the example site.</a>

    </body>

</html>

Here, ‘href’ is an attribute of the anchor ‘a’ which is an element. Further, ‘https://www.example .com’ is the value for the attribute. 

Various Types of Attributes in HTML 

HTML attributes are widely divided into two categories namely global attributes and local attributes within which there are various attributes that have different purposes. Let’s discuss the same in detail: 

Global Attributes 

Global attributes are certain attributes that are common to all HTML-based elements. They are used with almost all the elements, even if they have no effect on some. 

Where only standard tags are specified, we still can attach global attributes using HTML markup. That implies that any ad hoc element that is added in a non-standard way shall still have these attributes, but its use will invalidate the HTML5 status of the document. Take HTML5-compliant browsers, they do not display content marked <foo hidden>. Even though <foo> is not a valid HTML element, this means that they are going to see everything.

Further here are some of the commonly used global attributes: 

1. Access key: It is used to define a shortcut element. It can be used to activate and select specific text on a webpage. For example:

<!DOCTYPE html>

<html>

<head>

<title>sample title</title>.

</head>

<body>

<p><a href="/" title="Home" accesskey="h">The link directs to home page with an `accesskey` value of `h`.</a> </p> Press <kbd>control</kbd> + <kbd>C</kbd> (<kbd>Shift </kbd> + <kbd>alt</kbd> + <kbd>H</kbd> on windows) to go home.

Read more: https://upgrad//#ixzz8VhQG0SdK

</body>

</html>

Which will show the following output: 

2. Class: This global attribute indicates a list of separate classes of an element. It is one of the most used elements for handling various HTML codes. Here a great HTML attributes example of body attributes in HTML: 

<!DOCTYPE html>

<html>

<head>

<title>sample title</title>.

</head>

<body>

<div class="somestyle">This is a global element.</div>

<p class="pstyle">This is a sentence.</p>

</body>

</html>

Which as an output would look like: 

Here, different classes are shown in different sentences with the use of the div HTML attribute. 

Here’s a global HTML attribute list with their respective tags: 

Attribute

Description

Applicable Tags (Examples)

href

Defines the destination URL for hyperlinks

<a>

src

Specifies the source of external resources like images or videos

<img>, <script>, <iframe>

target

Defines how a linked document should be opened (e.g., same window, new tab)

<a>

type

Specifies the type of content or input associated with an element

<input>, <script>, <link>

formaction

Defines the action (URL) to which a form submits data

<form>

formenctype

Defines the content type of form data when submitting

<form>

formmethod

Defines the HTTP method used to submit form data (e.g., GET, POST)

<form>

rel

Defines the relationship between a document and the linked resource

<link>

media

Specifies the media types on which a linked resource should be applied (e.g., screen, print)

<link>

charset

Defines the character encoding of the document

<meta>

Local Attributes

These are very specific elements that apply to a select range of HTML attributes. Some of the more frequently used local attributes are specified below: 

1. Title Attribute: It is an HTML tag and attribute that simply signifies the title of the web page. It is a mandatory attribute for all web pages. For instance, the code is used in the following manner: 

<!DOCTYPE html> 

<html> 

    <head>

        <title>HTML Attribute Tutorial </title>

    </head> 

    <body> 

        <h1>Heading No. 1 </h1> 

        <p title="paragraph1"> Paragraph Text </p> 

    </body> 

</html>

Which would result in the following output: 

Here the text ‘Heading no.1’ is the text used with the title attribute to indicate the title of a web page. 

2.  Lang Attribute: This attribute is used to specify a particular language of a web page. For instance, consider the following code: 

<!DOCTYPE html> 

<html> 

    <head>

        <title>Example - lang attribute</title>

    </head> 

    <body> 

        <h1>English Language</h1> 

        <p lang="en" >English is considered to be a universal langauge.</p> 

    </body> 

</html>

Here, the “lang= ‘en’’ attribute denoted the use of English language on the website. 

Here’s a local HTML tags and attributes list with examples and with their respective tags: 

Attribute

Description

Applicable Tags (Examples)

id

Unique identifier

Any HTML element

class

Assigns CSS classes

Any HTML element

lang

Specifies the language of the element's content

Any element containing text content

title

Provides tooltip text on hover

Any element

style

Defines inline styles

Any element

dir

Defines the direction of text content (LTR or RTL)

Any element containing text content

hidden

Hides the element from view

Any element

translate

Specifies whether content should be translated

Any element

accesskey

Defines a keyboard shortcut for accessing an element

Any element

tabindex

Sets the tab order for keyboard navigation

Any element

In Conclusion 

HTML attributes serve as foundational elements in web development, enabling the enhancement of element functionalities, styling, interactivity, and accessibility across the web. They empower developers to customize web content and behavior, ultimately enriching the user experience with engaging and dynamic features. Mastery of HTML attributes is essential for those aiming to thrive in web development.

To deepen your comprehension of HTML attributes and other vital web technologies, consider exploring courses offered by upGrad. This platform delivers accredited certificate programs crafted by industry experts, covering a spectrum from basic web development to advanced programming. With its industry-aligned curriculum, seasoned faculty, and hands-on project opportunities, upGrad stands out as an ideal choice for aspiring developers seeking to elevate their skills and progress in the tech field.

Frequently Asked Questions 

  1. What are HTML form attributes?

HTML form attributes provide specific functionality and behavior to form elements on a web page. There are different types of attributes like type, name, value, required, img attributes in HTML, and more. 

  1. What is the type attribute in HTML?

The type attribute is a versatile attribute used across various HTML elements. Its specific meaning depends on the element it's applied to. 

  1. What are HTML data attributes?

Data attributes, starting with data-, provide a way to store custom data associated with an element. This data isn't displayed on the page but can be accessed and manipulated using JavaScript, enabling dynamic behavior and interaction.

  1. What are the 16 HTML attributes?

There are far more than 16 HTML attributes! The exact number depends on the specific HTML version, but there are dozens of commonly used attributes. We've covered many in this guide.

  1. Can HTML attributes be customized?

No, the core set of HTML attributes is pre-defined and cannot be directly customized. However, you can achieve custom functionality by combining attributes with JavaScript and CSS. For example, you can use data attributes to store custom data and then use JavaScript to manipulate that data and create dynamic effects.

  1. How to add attributes in HTML?

Attributes are added directly within the opening tag of an HTML element. They follow a key-value pair format, separated by an equal sign (=) and enclosed in quotation marks (either double or single). Here's an example: 

<img src="banner.jpg" alt="Font attributes in HTML" width="300" height="200">

In this example, the img element has several attributes. The "src" value is the image source path, the "alt" value is the alternative text description, the "width" value defines the image width and the "height" value defines the image height.

  1. Can we create attributes in HTML?

Technically, no. You cannot invent entirely new HTML attributes. However, as mentioned earlier, you can achieve custom functionality using data attributes (data-*) and JavaScript. By storing custom data and manipulating it with HTML, you can create effects and interactions that feel like custom HTML tags and attributes.

Pavan Vadapalli

Pavan Vadapalli

Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and l…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...