HTML Attributes Explained – A Complete Guide by HQLEduTech

HTML attributes are powerful tools used to provide additional information about HTML elements. They define properties, behaviors, and styling, making web pages more functional and accessible.

What Are HTML Attributes?

  • Every HTML element can have attributes.
  • Attributes are always written in the start tag.
  • They are usually written as name/value pairs, like:
HTML
name="value"
  • They enhance the meaning and behavior of elements.

Commonly Used HTML Attributes

href Attribute

Used with the <a> tag to define the destination URL of a link.

Example:

HTML
<a href="https://hqledutech.com">Explore HTML Tutorials on HQLEduTech</a>

src Attribute

Used with the <img> tag to specify the source path of an image.

Example:

HTML
<img src="assets/images/html-logo.png" alt="HTML Logo">

URL Types for src:

  1. Absolute URL – Full web address:
HTML
src="https://example.com/logo.png"

2. Relative URL – Relative to your page location:

HTML
src="images/logo.png"

width and height Attributes

Define the size of an image in pixels:

HTML
<img src="images/html.jpg" width="400" height="300">

alt Attribute

Provides alternative text for an image if it fails to load or for screen readers.

HTML
<img src="missing.jpg" alt="Image not found">

Important for SEO and accessibility!

style Attribute

Adds inline CSS styles directly to HTML elements.

HTML
<p style="font-size:18px; color:blue;">Styled paragraph</p>

lang Attribute

Specifies the language of the HTML document.

HTML
<html lang="en-US">

Helps screen readers and search engines understand your content better.

title Attribute

Gives additional tooltip-style information when a user hovers over an element.

HTML
<p title="This is a tooltip">Hover over this text</p>

Best Practices for Writing HTML Attributes

Always Use Lowercase

Although HTML is case-insensitive, it’s best practice to use lowercase for attribute names.

Good:

HTML
<img src="img.jpg">

Avoid:

HTML
<IMG SRC="img.jpg">

Always Use Quotes Around Attribute Values

Even though quotes are optional in some cases, they help avoid syntax errors, especially when values include spaces.

Good:

HTML
<a href="https://hqledutech.com">Visit HQLEduTech</a>

Avoid:

HTML
<a href=https://hqledutech.com>Visit HQLEduTech</a>

Single vs Double Quotes

  • Use double quotes by default.
  • Use single quotes when values contain double quotes, and vice versa.
HTML
<p title='John "HQLEduTech" Dev'></p>
<p title="John 'HQLEduTech' Dev"></p>

Summary Table of Key HTML Attributes

AttributeDescriptionUsed With
hrefURL of a link<a>
srcPath to image or media<img>, <video>, <audio>
altAlt text for images<img>
styleInline CSS stylingAny element
langLanguage declaration<html>
titleTooltip textMost elements
width, heightImage or video dimensions<img>, <video>
typeData type specification<input>, <script>, <button>

Additional Unique Attributes Worth Knowing

AttributeDescription
idUnique identifier for an element
classAssigns one or more CSS classes
data-*Custom data attributes for JavaScript use
readonlyMakes an input field read-only
disabledDisables an element
placeholderProvides a hint inside input fields
requiredSpecifies that input must be filled out
checkedPre-selects a checkbox or radio button

Learn More at HQLEduTech

Start Learning HTML Now


Scroll to Top