How to Work with Attributes in HTML

Tags often have attributes for further defining the element. In this how-to, you'll learn how to use attributes.

Attributes are name-value pairs that can be added to the starting tag in an element. For example, one of the most commonly used attributes is the id attribute, which looks like this:

<div id="mostImportantDiv"></div>

The HTML specification defines a set of global attributes that can be used with most HTML elements. The global attributes include the following:

  • accesskey
  • class
  • contenteditable
  • contextmenu
  • dir
  • draggable
  • dropzone
  • hidden
  • id
  • lang
  • spellcheck
  • style
  • tabindex
  • title
  • title
  • translate

Other attributes are specific to different groups of elements, or to just a single element. For example, the <img> element has a src attribute, which specifies the path to the image file.

Elements can have multiple attributes, separated by spaces, like so:

<tagname att1="value" att2="value">Element content</tagname>

The order of attributes is not important.

Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.