Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Inserting images

From: Up and Running with HTML

Video: Inserting images

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.

Inserting images

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.

Show transcript

This video is part of

Image for Up and Running with HTML
Up and Running with HTML

49 video lessons · 25826 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 2m 12s
    1. Welcome
      55s
    2. Using the exercise files
      1m 17s
  2. 29m 30s
    1. Learning HTML
      2m 47s
    2. Choosing a code editor
      5m 2s
    3. Exploring basic HTML syntax
      8m 18s
    4. Do I need to learn HTML5?
      5m 6s
    5. Exploring HTML references
      8m 17s
  3. 35m 40s
    1. Exploring an HTML document
      5m 19s
    2. Working with doctype declarations
      4m 3s
    3. Examining the document head
      8m 20s
    4. Looking at the document body
      3m 21s
    5. Adding document structure
      8m 52s
    6. Lab: Coding a basic page
      3m 9s
    7. Solution: Coding a basic page
      2m 36s
  4. 1h 23m
    1. How does HTML format text?
      5m 51s
    2. Adding headings
      7m 24s
    3. Formatting paragraphs
      4m 54s
    4. Controlling line breaks
      3m 50s
    5. Creating lists
      10m 37s
    6. Emphasizing text
      6m 42s
    7. Displaying special characters
      5m 8s
    8. Controlling whitespace
      4m 35s
    9. Inserting images
      9m 20s
    10. Lab: Controlling page content
      13m 57s
    11. Solution: Controlling page content
      10m 55s
  5. 31m 54s
    1. Linking to pages within your site
      6m 45s
    2. Linking to external pages
      3m 2s
    3. Linking to downloadable resources
      2m 25s
    4. Linking to page regions
      8m 0s
    5. Lab: Creating Links
      5m 57s
    6. Solution: Creating Links
      5m 45s
  6. 40m 27s
    1. Examining basic table structure
      5m 10s
    2. Adding content to tables
      6m 20s
    3. Setting table attributes
      7m 42s
    4. Adding table captions
      4m 3s
    5. Defining table headers
      2m 13s
    6. Making table data accessible
      5m 46s
    7. Lab: Building tables
      4m 13s
    8. Solution: Building tables
      5m 0s
  7. 43m 23s
    1. Understanding the relationship between HTML and CSS
      4m 58s
    2. Creating inline styles
      4m 53s
    3. Exploring the style element
      5m 13s
    4. Basic font styling
      9m 24s
    5. Changing color
      4m 55s
    6. Taking styles further
      5m 24s
    7. Lab: Controlling basic styles
      5m 10s
    8. Solution: Controlling basic styles
      3m 26s
  8. 5m 44s
    1. Next steps
      2m 56s
    2. Additional resources
      2m 48s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with HTML.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.