Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - HTML Attributes


HTML Attributes

HTML attributes give extra information about an element. They are written inside the opening tag as name="value".

Think of attributes as small settings that change how an element works or looks.


Basic Syntax

Some HTML elements need attributes to do their job. For example, the <a> element creates a link, and the href attribute tells the browser where the link should go:

Example

Using the href attribute on a link:

<a href="https://www.w3schools.com">Visit W3Schools</a>
Try it Yourself »
  • Attributes go inside the opening tag.
  • Most attributes use name="value" format.
  • Always use quotes around values (double quotes are most common).

Common HTML Attributes

Here are some attributes you will use often:

Attribute Used On Purpose
href <a> Link destination (URL)
src, alt, width, height <img> Image file, fallback text, and size
lang <html> Language of the document (for example: en)
title Most elements Tooltip text shown on hover
style Most elements Inline CSS styling
id, class Most elements Unique styling / group styling

Attributes in Action

Images often use several attributes together. src selects the picture, alt shows helpful text if the image cannot load, and width/height set the size.

Images

<img src="avatar.png" alt="Picture of Me" width="120" height="120">
Try it Yourself »

Quoting Attribute Values

Attribute values should always be wrapped in quotes. Double quotes are most common, but single quotes also work:

Example

<p title="This is a greeting">Hello</p>
<p title='This is also a greeting'>Hello</p>
Try it Yourself »

Inline Styles

The style attribute lets you change how an element looks. You will learn much better ways to style pages later, but this is useful for small tests:

Example

<p style="color: green; font-size: 50px;">Styled text</p>
Try it Yourself »

id and class

Use id for one special element, and class for elements that share the same style.

These two attributes are very important when you start working with CSS.

Example

<h1 id="site-title">My Site</h1>
<p class="lead">Welcome!</p>
<p class="lead">Enjoy your stay.</p>
Try it Yourself »

Tip: Use an id only once per page. Choose clear names like main-nav or hero.

Note: You will use class a lot when you learn CSS.


Summary

  • Attributes go inside the opening tag.
  • They follow the name="value" format.
  • Some attributes work only with certain elements (like href for links).
  • Many attributes work on almost everything (like id, class, title, style).

Next, you will learn how to change colors, fonts, and alignment using HTML Styles.

Next » HTML Styles


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.