img
In this module we will do the following:
Discuss the image (
<img>
) tag.Set up an image folder and place an image in the folder.
Create reference to an image.
Find a URL for an image and use it.
Resize the image w/ basic inline styling.
Create a hyperlink from an image.
Add a few more images with favorite places or things. Use a variety of links and styling.
File Location
For this module, you should be working in the following location:
Description:
Images are defined with img
tags and do not have a closing tag. Like hr
and br
, they are self-closing tags.
Image Folder
For this module, you'll be adding a new folder to your project to contain images. This is another example of relative pathing, which was briefly discussed on the "Links" page. For ease of reference, we'll be adding this folder to the same directory as HTML, CSS, and JS Basics. Name the folder 4-assets. Your new folder structure should look like this:
Adding Assets
Great! Now that we've added the folder to contain our pictures, let's go get some assets to use on our website. Using your browser, navigate to https://ericjoyner.com/turnfortheworse/ and right click on the artwork you're presented with. Choose "Save image as..." from the list that appears, and save the image to the 4-assets folder we just created.
Sample Code
Add the following sample code to your file:
Self-Closing Tags
Here we have a few tags for organizing concepts. They are not as common, but useful to know about. They also can teach us about self-closing tags. Self-Closing tags only have one tag involved. They are open and closed. We can write them like this: <hr />
or <hr>
hr
Stands for horizontal rule: Used to separate content. Provides a horizontal line between items.
br
Is simply a line break: separates content using white space instead of a line. (We used one here, to prevent the sentence about br
from wrapping up to the line above!)
Discussion
So, let's dig into what's going on in this file a little bit: In the first example, we're referencing a location inside our project using relative pathing in the src
attribute. In this case, we back up one step (../
) in the directory tree and then dive into our 4-assets
folder (4-assets
), which would look like ../4-assets
; and then point directly at the image we want to be displayed (/TurnForTheWorse.jpg
). The entire path that you type out should look like this: ../4-assets/TurnForTheWorse.jpg
.
In the other examples, we've nested <img>
tags inside of <a>
tags so that the images themselves act as links. The final example uses the class
attribute and the <style>
tag (more on this soon) to display our picture as a circle.
Challenge
Now that you've got a grasp on how this tag works, try to incorporate one of your own pictures. Save your image to your assets folder and experiment!
Last updated