Links, Images, and Attributes

Attributes provide additional information about HTML elements. They can configure elements or adjust their behavior in a variety of ways. For example, images, videos, and several other HTML elements can have height and width attributes that determine size.

We saw some attributes in the last section on Lists in HTML. We learned that the start attribute determines the value from which the list starts counting. There was also a reversed attribute which reverses the order the list counts from.

So there are many uses for HTML attributes. They come in a variety of forms, serve many purposes, and change the behavior of elements.

So in this section, we will learn:

  • All HTML elements can have attributes.
  • Attributes provide additional information about elements.
  • We add attributes to the opening tag.
  • Attributes usually come in key/value pairs like attribute="value".

HTML ATTRIBUTE SYNTAX

We add attributes to the opening tag of elements, and they usually come in key/value pairs:

<tag key=”value”></tag>

So we have a key or name and some quotation marks into which we give the attribute a value.

So breaking this down, an attribute has two parts:

  • The name or key that defines what attribute it is.
  • A value placed between quotation marks.

BOOLEAN ATTRIBUTES

We can also define attributes with a single keyword.

Such attributes are called Boolean attributes. An example of a Boolean attribute would be the reversed attribute we saw used with lists. Some others include the checked, disabled, and required keywords we commonly use with form inputs.

<input type="email" required>
<input type="submit" value="Submit" disabled>
<input type="checkbox" checked>

GENERAL PURPOSE ATTRIBUTES

Some attributes, including id, class, and style, can be used with most HTML elements.

  • The id attribute is a unique name or identifier for an element.
  • The class attribute also identifies elements for use with CSS & JavaScript. But unlike id, the class attribute does not have to be unique in the document. You can apply a class to multiple elements.
  • The style attribute allows you to specify CSS styles directly within HTML elements. Again, we will look at this in more detail in the CSS section.
<p id='main' class='text' title='paragraph' style='font-weight:bold;'>
Opening paragraph
</p>

Opening paragraph

If you are unsure which attributes you can use with an element, a great resource to check is our HTML Attribute Reference.

The Attribute Reference lists all attributes currently used in HTML. You do not have to know all of these, and you will likely use 10 to 15 regularly.

THE TARGET ATTRIBUTE

Another attribute we can add to links the target attribute. The default value is target="_self", which means that the link will open in the current tab when clicked.

We can also use target="_blank", which allows us to open the link in a new tab. This allows us to keep the current browser tab open when the user clicks a link.

In the next section, we will cover the target attribute in much more detail.

IMAGES & THEIR ATTRIBUTES

There is no requirement for the clickable content of links to be text. We could use images instead, for example.

The HTML <img> element is a single self-closing tag. It does not need both an opening and closing tag as it does not surround text or other content. We call images of this type empty elements.

Images must have at least two attributes:

  • The src attribute.
  • The alt attribute.

src

The src attribute denotes the source location of the image. To reiterate, this could be within project files or from an external web page.

Image from an external source
<img src="https://images.unsplash.com/photo-1606278483460-25d13da45337">
Sourced from Unsplash.com

Finding images to use

A great site to get freely useable images to use in your projects is unsplash.com. Another is pexels.com. If you are looking for illustrations rather than images, undraw.co is a fantastic free resource.

Local Images

It is preferable to have images stored in a location controlled by you. If you do not have control of the image, then you do not control its location. If the URL path to the image changes, the browser will not be able to find it, and it will disappear from your site.

So, I always recommend using images that you can control.

When adding images, you need to take note of the file type. We need to know the full file name for it to work in HTML.

You can have file types including .png, .jpg, .gif, .avif, or .webp.

Image from a local source
<img src="img/cat.png">

Shortly, we will discuss the HTML <picture> element, which allows us to be more flexible in specifying images. We can give the browser a choice of image types or sizes to suit the user's device.

RESIZING AN IMAGE USING ATTRIBUTES

We have attributes called height and width, which allow us to size our images. We can add a numerical value without a specified unit, like 200 (which will give 200 pixels). Or we can explicitly set units, like px or %.

We just need to remember to leave a space between the attributes already present. It does not matter which order attributes are listed inside the opening tag, just that they are there. Any new attributes must be space-separated from others.

Image from an external source using width & height attributes
<img src="https://images.unsplash.com/photo-1606278483460-25d13da45337" width="460" height="290">
Sourced from unsplash.com

NOTE: If you resize this window, the image will always be 460 pixels wide and 290 pixels high.

ALT ATTRIBUTE & ACCESSIBILITY

You may notice that if we autocomplete an <img> element using Emmet, it also gives us an alt attribute.

Image from an external source with an alt attribute
<img src="https://images.unsplash.com/photo-1606278483460-25d13da45337" alt="A random image sourced from unsplash.com">

The alt attribute allows us to display alternative text when images cannot be viewed. This could occur due to a slow internet connection or if there is an error in the image source location. The alt attribute also provides a description for images that can be read by search engines. So it is helpful for SEO.

But the alt attribute's most important function is with web accessibility. The alt attribute is used by screen reader software so that a person who is listening to a web page's content (for instance, a person who is impaired visually) can still interact with the image element.

There is a great article here on the WebAIM site which explains this, and I encourage you to read it.

USING THE HTML5 PICTURE ELEMENT

Sometimes, scaling an image up or down to fit different devices fails to work as expected. Also, reducing the image dimension using width and height attributes does not reduce the size of the rendered file. If an image is to display small on a mobile phone, we might not want a large file size.

To address these problems, HTML5 introduced the <picture> element that allows us to define multiple versions of an image to target different devices.

The <picture> element contains one or more <source> elements, each referring to different image sources, and one <img> element at the end.

Each <source> element has a media attribute, which specifies a condition (similar to the media query) that the browser uses when determining which source to use.

<picture>
<source media="(min-width: 1000px)" srcset="logo-large.png">
<source media="(max-width: 500px)" srcset="logo-small.png">
<img src="logo.png" alt="My logo"/>
</picture>

The browser will evaluate each child <source> element and choose the best match among them for the device in question.

If no suitable match is found, whatever is in the <img> element's src attribute will be used.

The last child of the <picture> element must be the <img> tag.

SUMMARY

So in this section, we have looked at HTML attributes, specifically those associated with links and images.

We have seen how we can add links using <a> tags and images with the <img> tag. We then looked at attributes and how we add these to HTML elements.

For links, the attributes were the href or hyperlink reference, and for images, it was the or source location.

These attributes provide additional information like the location of an image or the URL we want to link to. We then added additional attributes to give more information to the browser about our elements. We also added a couple of IDs that enabled us to traverse between locations on the same page.

We also looked at various kinds of links that we use to link to external pages out on the web, pages within the same site, and locations within a single page.

We will use attributes all the time. You might want to keep looking over the HTML Attribute Reference for more information. Again, you do not need to memorize all of these attributes, and you will only regularly use 10-15 of them.

FURTHER RESOURCES

GITHUB REPO USED IN THE VIDEO

Next section →