Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Giulia F
Giulia F
361 Points

Can someone please explain to me why we need to use the (attribute?) class when putting an image on the website?

For example: <img scr=".." alt= .. class=>

Why should we use we should we use class?

2 Answers

Hey Giulia Filippini,

Here's the breakdown:

The <img> tag has two required attributes:

  1. src - Specifies the path to the image.
  2. alt - Defines alternate text, if the image for some reason cannot be displayed.
<img src=src="img/mountain.jpeg" alt="mountain">

For the best practice, you should always include the alt attribute. It's very useful for the user who is visually impaired and is using a screen reader, the screen reader will read the alt description to the user.

class attribute is optional. It can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name.

For example, if you want to add a border on the image, you can select its class attribute:

<img src=src="img/mountain.jpeg" alt="mountain" class="img-moun">
.img-moun {
border: 1px solid red;
}

To learn more about the class attribute, check out this link: https://www.w3schools.com/tags/att_class.asp

Hope this helps!

Giulia F
Giulia F
361 Points

Hi Rabin,

very helpful!

I wish you a lovely day.

Giulia