H

HTML Handbook

Clean • Professional

HTML Attributes for Styling and Accessibility

1 minute

HTML Attributes & Global Attributes 

HTML attributes provide additional information about HTML elements. They define properties, behavior, and appearance of elements.

  • Global attributes can be applied to almost all HTML elements, like class, id, style, title, and data-*.

  • Attributes help in styling, scripting, accessibility, and data handling.

  • They make HTML more meaningful, interactive, and maintainable.

Identification & Styling

These attributes are mainly used to identify elements for CSS styling or JavaScript manipulation, and to apply inline styles directly.

class

Assigns one or more class names to an element.

Classes are reusable, meaning multiple elements can share the same class for consistent styling or JS behavior.

Example:

<p class="highlight">This text will be styled using CSS.</p>

<style>
  .highlight {
    color: blue;
    font-weight: bold;
  }
</style>

Output :

learn code with durgesh images

id

Assigns a unique identifier to an element.

Useful for targeting a specific element with CSS, JavaScript, or internal links.

Example:

<h2 id="section1">Section 1</h2>
<a href="#section1">Go to Section 1</a>

<style>
  #section1 {
    color: green;
  }
</style>

Output :

learn code with durgesh images

style

Allows adding inline CSS directly to an element.

Good for quick styling, but external CSS is recommended for larger projects.

Example:

<p style="color: red; font-size: 18px;">This text is styled inline.</p>

Output :

learn code with durgesh images

Information & Accessibility

title

Shows additional information as a tooltip when hovering over an element.

Example:

<p title="This is a tooltip!">Hover over this text to see the tooltip.</p>

Output :

learn code with durgesh images

alt

Provides alternative text for images. Important for accessibility and SEO.

Example:

<img src="logo.png" alt="Company Logo">

Output : 

learn code with durgesh images

lang

Specifies the language of an element’s content. Helps screen readers and search engines.

Example:

<p lang="en">Hello, world!</p>
<p lang="es">¡Hola, mundo!</p>

Output : 

learn code with durgesh images

Global Attributes (Work with All HTML Elements)

These attributes can be used with any HTML tag.

AttributeDescriptionExample
idUnique identifier<div id="header">...</div>
classAssigns CSS class<p class="note">Note here</p>
titleTooltip text<abbr title="World Health Organization">WHO</abbr>
styleInline CSS<p style="color:red;">Red text</p>
hiddenHides element<p hidden>This is hidden</p>
draggableAllows drag & drop (true/false)<img src="drag.png" draggable="true">
contenteditableMake element editable<p contenteditable="true">Edit me!</p>
spellcheckEnable/disable spell checking<p spellcheck="true">Check spelling</p>
tabindexControl tab navigation order<input tabindex="1">
translateAllow translation<p translate="no">BrandName</p>
data-*Custom data attributes<div data-user="123">User</div>
Article 0 of 0