BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

HTML attribute: What it is and how to write one

An HTML attribute is a setting inside an element's start tag, like href on a link or alt on an image, that changes what the element does or means.

Monthly searches · html attribute
18.1k
Keyword research data
html attribute
HTML attribute: What it is and how to write one
Click the image to view it full size

An attribute adds information to an HTML element. It sits inside the element's start tag, as a name and usually a value:

<a href="/pricing" rel="nofollow">See prices</a>

Here the <a> element has two attributes. href says where the link goes; rel says how the page relates to it. Without href, the <a> isn't a working link at all, so attributes often decide what an element actually does.

Attributes only ever go in the start tag, never in the end tag. That's also why the common phrase "alt tag" is a slip: alt is an attribute of the <img> tag. For the elements themselves, see HTML tags.

How to write an attribute

The HTML standard allows four ways: WHATWG

Syntax Example Limit
Name only <input disabled> The value is empty.
Unquoted <input value=yes> The value can't contain spaces, quotes, =, <, > or backticks.
Single quotes <input type='checkbox'> The value can't contain '.
Double quotes <input name="be bold"> The value can't contain ".

Use double quotes as your habit. Values with spaces then just work, and you avoid a security trap: if your site prints user input into an unquoted attribute, an attacker can add an attribute of their own, such as a script handler, even when the input is otherwise escaped. MDN

Separate attributes with a space. Attribute names aren't case-sensitive in HTML, but lowercase is the convention. And a tag can't carry the same attribute twice: two class attributes on one element is an error, so list all the classes in one.

Boolean attributes: present means true

Some attributes are on or off, like disabled, required, checked or hidden. For these, being there at all means true. The values true and false aren't allowed; to switch one off, leave it out. WHATWG

That catches people out:

<button disabled="false">Buy now</button>  <!-- still disabled -->
<button>Buy now</button>                    <!-- enabled -->

Global and element-specific attributes

Global attributes work on every element. The ones you'll meet most:

  • id: a unique name for the element, used for links to that spot (#reviews), CSS and scripts
  • class: one or more names for styling and scripting
  • lang: the language of the element's content
  • title: extra advisory text, often shown as a tooltip
  • hidden: hides the element
  • data-*: your own data for scripts, such as data-product-id="123"

Element-specific attributes only make sense on certain elements: href on links, src and alt on images, type and name on form fields.

Two more families sit alongside these. Event handler attributes, such as onclick, run JavaScript. ARIA attributes, such as aria-label, give assistive technology information the HTML alone doesn't; see accessibility.

The attributes search engines read

Google says it supports only a limited number of HTML attributes for indexing. These are the ones that matter:

Attribute On What it does for search
href <a>, <link> How Google finds pages: it crawls links written as <a> with an href.
src <img> How Google finds images and other resources a page loads.
alt <img> Describes the image, and acts as anchor text when the image is a link. See alt attribute.
rel <a> Qualifies a link: sponsored for paid links, ugc for user content, nofollow otherwise. Values can be combined, like rel="ugc nofollow".
rel="canonical" <link> Names your preferred URL. See canonical tag.
hreflang <link> Points to other language versions. See hreflang.
data-nosnippet <div>, <span>, <section> Keeps that text out of Google's snippets.

Two attributes that people expect to matter, and mostly don't: Google doesn't use lang to work out a page's language, it reads the text instead. And the title attribute on a link is only a fallback, used as anchor text when the link has no text of its own.

Attributes and JavaScript properties

If you write JavaScript, you'll notice most attributes have a twin. The attribute in the HTML is always a string, read with getAttribute(). The matching property on the element object, like input.maxLength, has a real type and returns the value actually in use, which can differ: an <input type="foobar"> reports its type property as "text". MDN

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.