Part 2: The Anatomy of a Tag
Tags: webdev, beginners, html, tutorial
Published: 2020-05-26 16:50:38

Last time on *The Anatomy of Web Design*, we discussed the idea of tags in HTML. There I stated that tags usually come in opening tag-closing tag pairs, which mark the beginning and end of an element. For instance, these opening and closing `<p>` tags mark the start and end of a paragraph:

```html
<p>
	I’m a paragraph, at least in name if nothing else. 
	See, I have more than one sentence.
</p>
```

However, some elements only have a start tag. These [void elements](https://html.spec.whatwg.org/multipage/syntax.html#void-elements) don’t directly have any content inside them, but can be used to embed media, add metadata, provide formatting cues, or link other files to the page.

## The `<img>` tag

Let’s take a look at one of the more common void elements: the image tag `<img>`.

```html
<img>
```

Well that was a bit anti-climactic. The thing is, the `<img>` tag can’t do much in its current form. It needs additional data in order to work properly, for instance the source url for the image or some alternative text to show if the image can’t be loaded. The way we specify that data is through…

### Attributes

Attributes provide a way to add specific characteristics to an element. They are listed as key-value pairs in an element’s starting tag.

```html
<tag attribute1="value1" attribute2="value2"></tag>
```

Keep in mind some of the formatting rules attributes require:
1.  Attributes are added after the element's name in the start tag.
2. The attribute and its value are separated by an '=' sign.
3.  Although the name of the attribute should not be placed in quotes, its value should be.
4.  An element can have more than one attribute, but they must be separated by spaces.
    

So now that we have a rough understanding of what attributes look like, let’s try to add some to our `<img>` tag to load an image. 


![](https://lh3.googleusercontent.com/ZHDSWqx9o02C-qEFmfhxzaAgBTX2SSrkaWccocBBuZHF8W0X1OeWUnAuTi_wl2vFdakDaIBY7TOUcg8QNvm_mxIf3njVECPPsoCL0qZRJ4InDiDLy6EfUUx60ipP1C6KGAx--qWP)

```html
<img src="https://www.publicdomainpictures.net/pictures/90000/velka/planet-venus.jpg" 
alt="The planet Venus">
```
The `<img>` tag requires two attributes:
-   `src` - The source url of the image
-   `alt` - Alternate text to display if the image fails to load, and to better meet [web accessibility standards](https://www.w3.org/WAI/standards-guidelines/wcag/).
    

Since we now have a fully functioning image tag, lets try adding it to our page from last time

```html
<!DOCTYPE html>
<html>
	<head></head>
	<body>
		<p>I'm a paragraph.</p>
		<img src="https://www.publicdomainpictures.net/pictures/90000/velka/planet-venus.jpg" alt="The planet Venus">
	</body>
</html>

```

![](https://lh4.googleusercontent.com/LHsrkNU4RFyc6-b5PqYJ3KptTVVKkd74R0CqUSVHc3OfBbnsShaVrVwDtgdWPOPPRd0f63fWI_h42UN3XuIuJVGdxsL1cQytjQRsFBeIQb0hxpdmg7YnjXU54g8lzhMD3Wq1t0DI)

Once you save your .html file and open it in the browser, you should see something like this. As you can see, I clearly underestimated the size of this image, but that’s not a problem we’re going to concern ourselves with at the moment. For now, try experimenting with image tags -- see what happens if you break the url, for instance -- and next time we’ll talk about headings in HTML.