Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Although it doesn't technically count as formatting existing content, inserting images is an important part of writing HTML, so we are going to take a brief moment to go over the basics of placing images on the page. And to do that we are going to work with the images.htm file found in the 03_09 folder. You can see here I have it previewed within the browser. And you see we have an image on the page. This is the beautiful low country of South Carolina, right around Beaufort, which is a beautiful area of the country. In order to see what this looks like, in terms of the code, let's switch back to out code editor.
And we can see, in about the second paragraph down actually, we see this tag right here, and this is the image tag, img element. The image tag is what we will use to place images on the page. And as you can see, it has several attributes, and the attributes gives us information about that image, like where to find the image, the dimensions of it, alternate text to display in place of the image if the image can't render, things like that. So there are a lot of different things that we can pass along about that image. The other thing that you will notice about the image tag is that there is no closing tag.
So it is a single contained element. There's no contents of it and there's no closing tag as well. It's also what is considered as an inline element. What that means is you will notice that it's inside of a paragraph. So it can be placed inside of headings, inside paragraphs; really anything that allows flow content in the element can hold an image tag. Using the syntax that we already have on the page here, let's go through all these attributes and discuss some of the different things that we're passing along to the user agent about this image.
First is the source attribute, src, right here. Images are referred to as what's called as replaced content. What that means is is the browser is going to take this image tag and it's going to replace the image tag with whatever it's pointing to. And this point, it's pointing to an image called lowcountry.jpg. Now, this can be found in the images directory. So you have to give your browser a path to the image from the current location of the page, and in this case you are basically saying, hey, look in the same directory I'm in.
Find the _images directory, which is right here, and I am going to open this up. Find the lowcountry.jpg. That's probably one of the most difficult things about placing images on the page for people who are not used to doing that. It's making sure that the path to the image is correct. You want to make what's known as a document relative path, meaning how do I get to there from where I'm currently located? So if you need to go up a directory you would use ../, and then if you need to go down in a directory you pass in the name of the directory forward and then the file that you're looking for.
Now, after source, we also have width and height. These attributes are optional. If you leave them off whatever the actual width and height of the image are, it will be used by the browser to display the image. So these are totally optional. What's nice about this is that if you needed to change this for some reason--perhaps you're doing a retina display for example and you want to display it smaller than it actually is so that the amount of pixels you've got there are appropriate for the screen-- you can do something like 150 and 150. Now notice that I am not putting any type of pixels at the end of this value; it's just a value itself.
Now if I save this, go back into my browser, and refresh, you can see that now the image is half the size it was before. Let's go ahead and strip those out entirely. If I save this, go back into the browser, notice that the browser is using the native height and width of the image to display it. Now finally, the last attribute that we have got here in this particular image tag is the alt attribute. You may hear this referred to as alt text for example.
What does this do for you? Alternative text is very useful for assistive devices. So if a screen reader is looking at this image, this is the information that's going to pass along to the user. So you want this to be descriptive text to describe kind of what's going on within the image. It's also useful if the image can't display for whatever reason; the alt text is typically displayed in its place. Now that we know a little bit about the syntax, let's go ahead and place another image on the page. I am going to go down to the paragraph just underneath this one, where it says, "Most browser support a wide range of image types." The first thing we do when we place an image on the page is just to go ahead and open up an image tag, so img.
And just after that, I am going to go ahead and start placing in all of my different attributes. I really like to start with the source attribute. It doesn't really matter which order you place these in, but I kind of like doing it in sort of this logical order, like, hey, here is where you go find this. Let's talk about which image we are going to put on the page. We are going to place this sc_flag.png. Our current file is images.htm, and if I look at the directory, I can see the images.htm is right here. It's inside 03_09 and the _images directory is in the same folder.
So all I have to do in order to point towards this--and I am just going to go ahead and give myself a little bit space here-- I am going to go ahead and do quotation marks, and I am going to say _images, which is the name of the directory it's found in, /sc_flag.png. Now, there are a lot of programs out there that you can use that will sort of resolve these for you, but I like having you guys do these by hand for a while because it forces you to understand the relationship between this file and the image.
If this path is wrong then the image will not display. So if you're ever putting an image on the page, maybe later on in the lab or even in this exercise, and the image does not display, nine times out of ten the culprit is going to be this path being incorrect. So make sure that you're going directly to where the image is, based on where you are currently located in your directory structure. Directly after that, I am going to go ahead and do height and give this particular one a height of 400, because it is 400 pixels tall, and then I am going to give it a width of 400 as well.
Finally, I am going to give it an alt tag, and you may notice that I'm going whitespace in between these attributes. Whenever you are putting multiple attributes into a tag; you don't use commas to do that; you use whitespace. So here I am going to do alt text, and the alt text I am going to type in is South Carolina flag. And then I have to remember to close the image tag. So I am going to do a right angle bracket. It doesn't really matter with what we know about whitespace, but I am going to remove all except for one character of whitespace there. So your image tag should look like this, img src pointing to _images/sc_flag.png, passing in a height and width of 400 and giving an alt text of South Carolina flag.
So I am going to save this, go back into my browser and refresh this, and now if I scroll down, I can see indeed that I am seeing an image there of the South Carolina flag. Perfect. One of the things that you'll notice the first time you start using images in HTML, especially if you're learning HTML before you learn CSS and not learning the two in conjunction with each other, is that images really are inline-level elements, meaning they are just placed inline in the middle of text. Because of that, browsers don't really differentiate between the fact that this particular image is 300 pixels tall and this text might only be 16 pixels tall.
And this can cause for some very ugly HTML pages at first. Most people look at this and their immediate thought is, "hey, I really want to be able wrap the text around this image. How in the world do I do that?" The sort of standard way of doing it is to use CSS, to use styles. Nine times out of ten that's what you want to do. So obviously, there is a little bit of learning curve there where you learn how to manipulate text wrapping around images by using styles. However, there is a way to do it with your HTML.
Let me qualify this by saying that this has been deprecated in almost every version of HTML. This really is a remnant of when we were using HTML and we did not have access to styling content through CSS. At that point we had to have these types of controls on our HTML elements. This has been done away with for a very, very long time, so it's really not recommended that you use this. However, we want to let you know a little secret: browsers automatically support all of the stuff, because they have to support legacy documents; they have to support somebody's site, even if it's been online since 1994, 95, 96, that type of thing.
So they still support all of these sort of obscure deprecated attributes. So for example, if I come into this particular image and I say align="left," if I save this and then preview it, you can see that this browser still supports that, and it wraps the text around it. Now it wraps the next paragraph and everything else around it as well, so this is not a great solution for what we are working with right here. But that can work for you. Now, my advice to you is to learn how to do that through styles.
However, there are going to be certain situations, like HTML email or something like that, where you might not have access to styles, and it's very helpful to know sort of those older ways of handling that so that in a pinch, if you have to, you can use those types of attributes.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.