HTML

HTML Attributes: Syntax, Usage, and Examples

HTML attributes contain important information about elements that influence their appearance or behavior. Examples include the path to an image file or the destination URL of a link.

How to Use HTML Attributes

HTML attributes belong inside the opening tag of an HTML element. Most attributes have a name and a value, formatted as name="value".

<a href="<https://www.example.com>" title="Example Website">Visit Example</a>
  • <a>: The HTML tag to define a link (or hyperlink).
  • href: The attribute to specify the destination URL of a link.
  • title: Provides additional information about the element as a tooltip when the mouse hovers over the element.

When to Use HTML Attributes

HTML attributes are essential for defining the appearance and behavior of most HTML tags.

Specifying Input Types

You can use attributes to define the type of data an <input> element should receive. By setting input types, you can make forms more interactive and user-friendly.

<input type="email" name="email" placeholder="Enter your email">

Linking Stylesheets

You need the rel and href attributes to link web pages with CSS stylesheets through a tag.

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

Image Embedding

With <img>-tag attributes like src and alt, you can provide the source of the image and alternative text.

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

Examples of Using HTML Attributes

Navigation Bar

A services company’s website might use the class attribute to style navigation links with CSS.

<ul class="navbar">
    <li><a href="#home" class="nav-item">Home</a></li>
    <li><a href="#services" class="nav-item">Services</a></li>
    <li><a href="#about" class="nav-item">About Us</a></li>
    <li><a href="#contact" class="nav-item">Contact</a></li>
</ul>

Newsletter Signup Form

An e-commerce website might use attributes like action and method in a newsletter signup form to define how to submit form data.

<form action="/submit_form" method="post">
    <label for="name">Email:</label>
    <input type="email" id="email" name="email">
    <input type="submit" value="Sign up">
</form>

Video Controls

A video platform might use attributes such as controls, autoplay, and loop enhance the experience with video content.

<video src="movie.mp4" controls autoplay loop>
    Your browser does not support the video tag.
</video>

Learn More About HTML Attributes

Global Attributes in HTML

Global attributes, like id, class, and style work with any HTML element. Using global attributes, you can identify, style, or provide metadata on elements.

  • id: With the id attribute, you can assign a unique identifier to an element. With a unique identifier, you can easily access it through CSS or JavaScript.

<div id="content">Unique Element</div>
  • class: By defining classes, you can group elements for styling or scripting purposes.

<p class="highlight">Highlighted Text</p>
  • style: The style attribute enables inline styling of an element directly within the HTML page.

<span style="color: red;">Styled Inline</span>
  • title: You can display additional information as a tooltip when hovering over the element using the title attribute.

<img src="logo.png" alt="Logo" title="Company Logo">
  • lang: The lang attribute specifies the language of an element's content. Specifying the language can help search engines and screen readers understand the context.

<p lang="fr">Bonjour le monde!</p>
  • tabindex: The tabindex attribute controls the tab order of elements for keyboard navigation. A negative value like tabindex="-1" makes an element focusable but removes it from the tab order.

<button tabindex="1">First</button>
<button tabindex="2">Second</button>
<button tabindex="3">Third</button>

Boolean Attributes in HTML

Boolean attributes are unique in that they don't require a value to function. Their presence alone on an element indicates true, and their absence indicates false. Common boolean attributes are:

  • disabled: With disabled, you can make an element non-interactive and unresponsive to user inputs.

    <button disabled>Submit</button>
    
  • checked: The checked attribute allows you to check input types like checkboxes and radio buttons by default.

    <input type="checkbox" checked>
    
  • readonly: With the readonly attribute, you can prevent users from modifying the value of an input element.

    <input type="text" readonly value="Cannot Change">
    
  • required: Including the required attribute indicates that an input element is necessary for form submission.

    <input type="text" required>
    
  • autoplay: By adding autoplay to media elements, you can allow media to start playing when a web page loads.

    <video autoplay>
        <source src="video.mp4" type="video/mp4">
    </video>
    
  • controls: With the controls attribute, you can display media controls like play, pause, and volume with media elements.

    <audio controls>
        <source src="audio.mp3" type="audio/mpeg">
    </audio>
    

Width and Height Attributes in HTML

The width and height attributes define the dimensions of certain HTML elements like images and videos in pixels.

Defining width and height is a quick and simple way to set static sizes in specific cases. However, for responsive designs, dynamic changes, and advanced styling, CSS is typically the better choice.

<img src="logo.png" alt="Company Logo" width="200" height="100">

Data Attributes in HTML

Custom data-* attributes store extra information on standard HTML elements, accessible by CSS and JavaScript.

<article data-author="John Doe" data-date="2023-01-01">
    Welcome to my blog.
</article>

ARIA Attributes in HTML

Accessible Rich Internet Applications (ARIA) attributes improve web accessibility for screen readers and assistive technologies.

  • aria-label: Using the aria-label attribute, you can provide an accessible name for an element.

<button aria-label="Search Button">🔍</button>
  • aria-hidden: To hide an element from assistive technologies, you can set aria-hidden to "true".

<div aria-hidden="true">Hidden from Screen Readers</div>
  • aria-live: Using the aria-live attribute, you can announce dynamic content updates.

<div aria-live="polite">Loading...</div>

Role Attribute in HTML

The role attribute defines the role of an element within web interactions and document structure.

<nav role="navigation">
    <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
    </ul>
</nav>

Common roles include:

  • navigation: Defines a section of navigation links.
  • banner: Identifies the main header of a page.
  • main: Denotes the main content area of a page.
  • complementary: Identifies content that complements the main content.
  • contentinfo: Identifies the footer section of a page.
Learn HTML for Free
Start learning now
button icon
To advance beyond this tutorial and learn HTML by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2024 Mimo GmbH