Lesson 68 +5 XP

HTML Attribute Reference

HTML Attribute Reference

Attributes are extra settings you put inside a start tag. Here are the ones you'll use most, plus every global attribute.

Global attributes (work on EVERY element)

AttributeWhat it does
idA unique name for one element.
classA shared name for styling groups.
styleInline CSS for this element.
titleExtra info shown on hover.
langThe language of the content.
dirText direction: ltr or rtl.
hiddenHides the element.
tabindexKeyboard focus order (0 = tabbable).
contenteditableMakes text editable.
spellcheckEnables spell checking.
draggableMakes an element draggable.
data-*Stores your own custom data.
autofocusFocus this element on page load.
accesskeyA keyboard shortcut to the element.
slotAssigns an element to a slot.
translateno stops translation tools.
inertMakes an area unclickable/unfocusable.
popoverMakes an element a pop-up panel.
nonceA one-time security token.
part / exportpartsStyle web component internals.
isCustomized built-in elements.

Link attributes (<a>)

AttributeWhat it does
hrefWhere the link goes (URL or #id).
targetWhere to open: _blank = new tab.
relRelationship (noopener, noreferrer, external).
downloadMakes the link download a file.
hreflangThe language of the linked page.
typeThe MIME type of the linked content.

Image attributes (<img>)

AttributeWhat it does
srcThe image file path.
altText shown if the image fails (accessibility!).
width / heightSize in pixels.
loading="lazy"Load only when scrolled near.
srcsetMultiple sizes for responsive images.
sizesWhich size to pick at which width.
usemapLinks to an image <map>.
ismapServer-side image map.

Media attributes (<audio>, <video>)

AttributeWhat it does
srcThe media file path.
controlsShows play/pause buttons.
autoplayStarts playing by itself.
loopPlays over and over.
mutedStarts silent.
posterCover image for a video.
preloadHow much to load upfront.
playsinlinePlay without fullscreen (phones).

Input attributes (<input>, <textarea>)

AttributeWhat it does
typeText, email, password, number, and more.
nameThe field's name (sent with the form).
valueThe current value.
placeholderA hint shown before typing.
requiredThe field must be filled in.
readonlyCan read but not edit.
disabledTurned off and not submitted.
checkedPre-selects a checkbox/radio.
min / maxSmallest/biggest allowed value.
stepHow much each click changes the value.
patternA regex the value must match.
multipleAllow several values.
maxlength / minlengthLength limits.
autocompleteTurns autofill on/off.
listLinks to a <datalist> of suggestions.
acceptWhich file types a file input accepts.
formJoin a form from outside it.

Form attributes (<form>)

AttributeWhat it does
actionWhere the form's data goes.
methodget (visible in URL) or post (hidden).
enctypeHow data is encoded (needed for files).
targetWhere to show the response.
novalidateSkip validation.
autocompleteAutofill on/off for the form.

Table attributes

AttributeWhat it does
colspan="2"A cell stretches across 2 columns.
rowspan="2"A cell stretches across 2 rows.
scopeWhich cells a header describes (a11y).
headersLinks a cell to its header(s).
spanOn <col>: how many columns.

Iframe attributes (<iframe>)

AttributeWhat it does
srcThe page to embed.
titleDescription for screen readers (important!).
sandboxLimits what the embedded page can do.
allowAllows features (camera, fullscreen).
allowfullscreenLets the page go fullscreen.
loading="lazy"Load when scrolled near.
srcdocInline HTML to show instead of a URL.

The aria-* attributes (accessibility)

AttributeWhat it does
aria-labelA name for things with no text.
aria-labelledbyPoints to the element that names this one.
aria-hidden="true"Hides from screen readers.
roleWhat an element's job is (button, alert...).

TL;DR

  • id and class are the two you'll use constantly.
  • src, href, alt, title are the everyday essentials.
  • Form attributes (required, placeholder, type) make forms great.
  • Global attributes work everywhere; most others only on certain tags.