You've already used a few attributes, like href on links and src on images. In this lesson, let's look at them properly and meet the rest of the family.
flag
What you'll learn
What an attribute is, and how to write one as a name and a value.
The attributes you will use most: src, alt, href, title, id, class and style.
The attributes that control input boxes and forms.
waving_handLet's add some details
A tag says what something is. An attribute adds details about it: which picture to show, where a link goes, what a box should say before someone types.
Attributes are always written inside the opening tag, never in the closing tag.
badge
Think of an ID card. The card is the tag. Each detail on it has a label and a value: ID No.: FPB-0153, Section: Tech, Photo: ana.jpg.
Attributes work the same way. Each one has a name and a value, written as name="value".
Front Page Bytes · Press IDtitle="Reporter since 2024"
src="ana.jpg"alt="Photo of Ana Reyes"
Name
Ana Reyes
ID No.
FPB-0153 id="fpb-0153"
Section
Tech class="tech"
Language
English lang="en"
Now let's see the attributes ↓
Front Page Bytes · Press IDtitle="Reporter since 2024"
src="ana.jpg"alt="Photo of Ana Reyes"
Name
Ana Reyes
ID No.
FPB-0153 id="fpb-0153"
Section
Tech class="tech"
Language
English lang="en"
Only one reporter has ID number FPB-0153, but many reporters share the Tech section. That's the difference between id (one of a kind) and class (a group).
How an attribute is written
<imgtag name
srcattribute name
=equals sign
"ana.jpg"value, in quotes
alt="Photo of Ana Reyes"another attribute
>end of opening tag
searchA closer look
The rules
Attributes go inside the opening tag, after the tag name, with a space between each one. Write the value inside quotes, like alt="WebCore logo".
The order of the attributes doesn't matter.
Some attributes have no value
A few attributes work like a switch: just writing the name turns them on. required means "this box must be filled in", disabled means "this box can't be used", and checked means "this checkbox starts ticked".
id, class and style
id gives one element a unique name, so use each id only once per page. class puts elements into a group, and many elements can share a class.
You'll use both a lot in the CSS and JavaScript lessons. style changes how one element looks right away. CSS has a much better way to style many elements at once, which you'll learn soon.
Attributes for forms
A <form> collects what people type. action says where to send it, and method says how: get or post.
On each box, type sets the kind of box, name labels the answer, value fills in a starting value, and placeholder shows a grey hint.
A form that uploads files also needs enctype="multipart/form-data".
constructionLet's build it
Let's add details to parts of the Front Page Bytes page. Each step shows the whole page, and the new lines are highlighted.
src says which picture to show and alt describes it. title shows a small tooltip: hover your mouse over the logo in the mini browser to see it.
width sets the size in pixels.
2Step 2: A link with href and title
Your code
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <img src="/storage/assets/WebcoreIcon.png" alt="WebCore logo" title="Front Page Bytes logo" width="100"> <p><a href="/news/phones" title="Opens the full story">Read the full story</a></p> </body></html>
What you see
description(no title)
href is where the link goes. title works on links too: hover over the link to see "Opens the full story".
3Step 3: A heading with id, class and style
Your code
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <img src="/storage/assets/WebcoreIcon.png" alt="WebCore logo" title="Front Page Bytes logo" width="100"> <h2 id="top-story" class="headline" style="color: #1E6FA0;">Phones Go on Sale</h2> <p><a href="/news/phones" title="Opens the full story">Read the full story</a></p> </body></html>
What you see
description(no title)
style turns this heading blue right away. id="top-story" and class="headline" don't change anything you can see yet. They are names that CSS and JavaScript will use later.
action and method say where and how the form sends its answers. On the email box, type, name, placeholder and required each add one detail.
The second input's value sets the button's text. Forms can't send anything from these mini browsers, so clicking Subscribe does nothing here.
checked makes the checkbox start ticked. disabled greys out the Plan box so it can't be changed. Try clicking both in the mini browser.
<br> simply starts a new line, and like <img> it has no closing tag.
lang says which language a piece of text is in, so screen readers pronounce it correctly. dir="rtl" makes text run right to left, which languages like Arabic need. Notice the Arabic line sits on the right.
sticky_note_2Quick notes
srcWhich file to show
<img src="photo.jpg">
How to use
<img>The picture file to show.
<iframe>The page to show inside the window.
photo.jpgA file in the same folder.
images/photo.jpgA file inside a folder named images.
Examples
A picture in the same folder as the page.
<img src="photo.jpg" alt="Photo">
A picture inside a folder named images.
<img src="images/photo.jpg" alt="Photo">
A page shown in a small window.
<iframe src="/weather"></iframe>
altWords that describe an image
<img src="photo.jpg" alt="A red phone">
How to use
<img>Describe what the picture shows in a few words.
alt=""Leave it empty for pictures that are only decoration.
Examples
Describe what the picture shows.
<img src="ana.jpg" alt="Ana Reyes smiling at her desk">
A picture used only for looks.
<img src="divider.png" alt="">
hrefWhere a link goes
<a href="/news">News</a>
How to use
/newsA page on the same website.
https://…A page on another website.
#topA spot on the same page, the element with id="top".
Examples
Another page on your site.
<a href="/news">News</a>
Another website.
<a href="https://www.wikipedia.org">Wikipedia</a>
Jump to a part of the same page.
<h1 id="top">Front Page Bytes</h1>...<a href="#top">Back to top</a>
titleTooltip on hover
<a href="/news" title="Latest stories">News</a>
How to use
Works on almost any tag.
Shows a small note when the mouse rests on the element.
Examples
Extra information about a link.
<a href="/news" title="Latest stories">News</a>
A note on a picture.
<img src="logo.png" alt="Logo" title="Front Page Bytes, since 2024">
idOne unique name
<h2 id="top-story">...</h2>
How to use
Use each id value once per page.
No spaces in the value. Use dashes instead.
#top-storyHow CSS and JavaScript pick it.
Examples
A heading you can jump to.
<h2 id="sports">Sports</h2><a href="#sports">Go to Sports</a>
One box you style with CSS.
<div id="top-banner">Front Page Bytes</div>
classA group name
<p class="headline">...</p>
How to use
Many elements can share the same class.
class="headline big"One element can have several classes, split by spaces.
The browser doesn't know an attribute called scr, so it ignores it. The image has no src, so no picture appears.
tips_and_updatesTip: Spell it s-r-c, short for source. When an attribute seems to do nothing, check its spelling first.
error
Using the same id twice
The mistake
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h2 id="story">Phones Go on Sale</h2> <h2 id="story">Laptops Get Cheaper</h2> </body></html>
What you see
description(no title)
This one looks fine, so it's easy to miss. An id should be one of a kind, and when two elements share it, CSS and JavaScript in later lessons won't know which one you mean.
tips_and_updatesTip: Give each id a different name, like id="phones" and id="laptops". Use class when several elements belong together.
quizTest your knowledge
Edit the code on the left. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Add an alt text to the image.not done yet
Give the link an href so it goes somewhere.not done yet