How to Insert Images in HTML | An HTML5 Image Tutorial

Dave Gray
27 Dec 202131:02

Summary

TLDRIn this beginner's HTML tutorial, Dave guides viewers through adding multimedia to a web page, focusing on incorporating images. He demonstrates using the Chrome browser, Visual Studio Code, and the Live Server extension to organize images in a folder and insert them into an HTML document using the 'img' tag. Dave explains the importance of 'alt' and 'title' attributes for accessibility and SEO, and discusses the 'width', 'height', and 'loading' attributes for performance optimization. He also introduces the 'figure' and 'figcaption' elements for semantically grouping images with captions. The video concludes with resources for free images and software recommendations for image editing and compression.

Takeaways

  • 🌐 Use the Chrome web browser, Visual Studio Code editor, and Live Server extension for efficient web development and real-time preview.
  • πŸ–ΌοΈ Organize images in a separate folder, typically named 'img', for better project management and accessibility.
  • πŸ“Έ Utilize the `<img>` tag with `src` and `alt` attributes to embed images in an HTML document, where `alt` text aids accessibility and provides a fallback for non-loaded images.
  • 🎨 Include `title` attribute for additional complementary text that appears on mouseover, enhancing user experience.
  • πŸ“ Specify `width` and `height` attributes in the `<img>` tag to prevent layout shift and improve page loading performance.
  • πŸ”„ Implement lazy loading for images below the fold by setting the `loading` attribute to 'lazy', which loads images as they become visible to the user.
  • πŸ–ΌοΈ Wrap images with the `<figure>` element and use `<figcaption>` for captions to semantically associate images with their descriptions.
  • πŸ”„ Use the `code` element within `<figure>` to display code snippets as part of the content, providing context and explanation.
  • 🌐 Validate HTML code using the W3C Markup Validation Service to ensure there are no errors or warnings.
  • πŸ“š Explore resources like placeholder.com, unsplash.com, and canva.com for free images and tools to enhance web development.
  • πŸ› οΈ Use image editing software like Earp View and online tools like tinypng.com to resize and compress images for optimized web performance.

Q & A

  • What tools does Dave use in this tutorial for working with HTML?

    -Dave uses the Chrome web browser, Visual Studio Code editor, and the Live Server extension for Visual Studio Code to work with HTML in this tutorial.

  • What is the purpose of the 'img' tag in HTML?

    -The 'img' tag in HTML is used to embed images into a web page. It has a 'src' attribute to specify the path to the image and an 'alt' attribute to provide alternative text for the image.

  • Why is it a good practice to keep images in a separate folder for a web project?

    -Keeping images in a separate folder helps in organizing the web project more efficiently, making it easier to manage multimedia files and maintain the overall structure of the project.

  • What does the 'alt' attribute in an 'img' tag serve?

    -The 'alt' attribute serves two main purposes: it provides a description for assistive technologies like screen readers for users who may not be able to see the image, and it also displays text if the image fails to load for any reason.

  • What are the 'width' and 'height' attributes used for in an 'img' tag?

    -The 'width' and 'height' attributes in an 'img' tag define the dimensions of the image in pixels. They help the browser to understand the size of the image and can prevent layout shifts by informing the browser how much space the image will occupy.

  • What is 'lazy loading' of images and why is it recommended?

    -Lazy loading is a performance optimization technique where images that are not immediately visible on the page load (i.e., below the fold) are only loaded when they are about to enter the viewport. This technique helps in faster page loading times and reduced server load.

  • What is the 'figure' element in HTML and how is it used in relation to images?

    -The 'figure' element in HTML is used to enclose self-contained content, such as images, diagrams, photos, code listings, etc. It allows for associating a caption with the content, improving accessibility and semantic structure of the web page.

  • How can you validate your HTML code?

    -You can validate your HTML code using the W3C Markup Validation Service by uploading your HTML file or entering the URL of your web page. It checks for any errors or warnings and provides feedback on how to correct them.

  • What are some resources Dave recommends for finding free images for web pages?

    -Dave recommends websites like unsplash.com, pexels.com, gratisography.com, and pixabay.com for downloading freely usable images. He also mentions placeholder.com for using placeholder images during layout development.

  • What tools does Dave suggest for image manipulation and optimization?

    -Dave suggests using Earphone View for basic image resizing and cropping, Canva for more advanced design and manipulation, and tinypng.com (or tinyjpg.com) for compressing images to reduce file size and improve page load times.

  • How does Dave demonstrate the 'lazy loading' effect in Chrome?

    -Dave demonstrates lazy loading by using Chrome Dev Tools to monitor network activity. He disables the cache and filters for image files, then reloads the page and scrolls to observe that the image below the fold only loads as it becomes visible.

Outlines

00:00

🌐 Introduction to HTML Multimedia and Images

This paragraph introduces the viewer to incorporating multimedia, specifically images, into an HTML web page. The tutorial uses the Chrome web browser, Visual Studio Code editor, and the Live Server extension to demonstrate the process. The creator explains the importance of organizing images in a separate folder within the project and shows how to add images to the web page using HTML code. It also mentions resources for downloading free images and emphasizes the use of the 'img' tag with 'src' and 'alt' attributes, highlighting the importance of alt text for accessibility and placeholder text in case the image fails to load.

05:00

πŸ“Έ Image Attributes and Accessibility

The second paragraph delves into the various attributes of the 'img' tag, such as 'title' and 'alt', explaining their roles in enhancing user experience and accessibility. It also discusses the 'width' and 'height' attributes, emphasizing their importance in preventing cumulative layout shift and improving page performance. The creator demonstrates how to adjust the image size and provides a brief overview of different image file types and their extensions. Additionally, the concept of 'lazy loading' is introduced, explaining how setting the 'loading' attribute to 'lazy' can improve page load times by deferring the loading of images below the fold until they are about to be viewed.

10:02

πŸ–οΈ Adding Images with Figure and Caption Elements

This paragraph focuses on using the 'figure' and 'figcaption' elements to semantically group images with their captions. The creator shows how to wrap an image in a 'figure' tag and add a 'figcaption' to provide a descriptive caption. This approach not only enhances the visual layout but also aids in accessibility by explicitly linking the caption to the image. The paragraph also touches on the use of the 'code' element for displaying code snippets within a figure, demonstrating the versatility of the figure element for different types of content.

15:02

πŸ› οΈ Image Optimization and Validation

The fourth paragraph discusses the importance of image optimization for web performance. It introduces the concept of 'lazy loading' with the 'loading' attribute set to 'lazy' for images that are not immediately visible on page load. The creator also explains how to provide the 'width' and 'height' attributes for images to ensure they display correctly and maintain aspect ratio. The paragraph concludes with a brief mention of validating the HTML code using the W3C validator to ensure there are no errors or warnings, emphasizing the importance of clean, valid code for web development.

20:02

πŸ” Exploring Image Resources and Software

In this paragraph, the creator shares various resources for finding and working with images. It mentions placeholder image generators, websites for downloading free-to-use images like Unsplash, Pexels, Gratisography, and Pixabay, and software for image manipulation such as Earphone View and Canva. The creator also highlights the importance of using images with proper usage rights and provides tips for compressing images using Tiny PNG to improve load times. The paragraph concludes with a call to action for viewers to like and subscribe to the channel for more content.

Mindmap

Keywords

πŸ’‘HTML

HTML, or HyperText Markup Language, is the standard markup language used to create web pages. It structures content on the web and is the backbone of any website. In the video, the creator uses HTML to build the foundation of a web page, adding multimedia elements like images to enhance the user experience.

πŸ’‘Visual Studio Code

Visual Studio Code is a free, open-source code editor developed by Microsoft. It is highly customizable and supports a wide range of programming languages, making it a popular choice among developers. In the context of the video, the creator uses Visual Studio Code as the primary tool for writing and editing HTML code.

πŸ’‘Live Server Extension

The Live Server extension is an add-on for Visual Studio Code that allows developers to quickly view their web projects in a live browser. It automatically refreshes the browser when the code is saved, providing real-time feedback on changes made. This extension is crucial for efficient web development as it streamlines the preview process.

πŸ’‘Image Tag

The 'img' tag is a fundamental HTML element used to embed images into a web page. It has attributes like 'src' (source) to specify the image file path and 'alt' (alternative text) to provide a textual description for the image, which is essential for accessibility and SEO purposes.

πŸ’‘Alt Attribute

The 'alt' attribute, short for 'alternative text', is a required attribute in the 'img' tag that provides a textual description of an image. It is crucial for accessibility, as it helps screen readers convey the content of the image to visually impaired users. Additionally, it serves as a placeholder if the image fails to load.

πŸ’‘Responsive Images

Responsive images are images that automatically adjust their size based on the screen size of the device viewing them. This is important for creating mobile-friendly websites, as it ensures that images are not too large or too small, improving user experience and reducing data usage. Techniques like specifying the 'width' and 'height' attributes or using CSS can make images responsive.

πŸ’‘Lazy Loading

Lazy loading is a performance optimization technique that delays the loading of non-critical resources, such as images below the fold, until they are needed or about to be viewed by the user. This improves page load times and reduces unnecessary data usage, enhancing the user experience.

πŸ’‘Figure and Figcaption Elements

The 'figure' and 'figcaption' elements are HTML tags used to semantically group media content, such as images, with their captions. The 'figure' element is used to enclose the media, while the 'figcaption' provides a caption or a legend for the content. This pairing improves accessibility and SEO by making it clear that the caption is related to the figure.

πŸ’‘Placeholder Images

Placeholder images are temporary images used in the layout of a web page during development when the final images are not yet available or decided upon. They help in visualizing the design and structure of the page without the actual content.

πŸ’‘Image Optimization

Image optimization refers to the process of reducing the file size of images without significantly compromising their visual quality. This is important for web performance, as smaller image files load faster and consume less data, leading to a better user experience.

πŸ’‘Web Accessibility

Web accessibility is the practice of making web content accessible to all users, including those with disabilities. It involves using proper HTML elements and attributes, such as 'alt' text for images, to ensure that assistive technologies like screen readers can interpret and convey the content to users with visual impairments.

Highlights

The tutorial is part of a beginner's HTML series using Chrome web browser, Visual Studio Code editor, and the Live Server extension.

HTML supports multimedia, and images are a significant part of web content.

It's a common practice to keep images in a separate folder for web projects.

The 'img' tag is used to insert images into an HTML page, and it doesn't require a closing element in HTML5.

The 'src' attribute specifies the path to the image, and the 'alt' attribute provides a text description for the image.

The 'alt' attribute is essential for accessibility and displays text if the image fails to load.

The 'title' attribute provides additional information about the image, which can be displayed on mouseover.

Specifying 'width' and 'height' attributes in the 'img' tag can improve page performance by preventing layout shift.

The 'loading' attribute with a 'lazy' value allows the browser to load images only when they are about to enter the viewport.

The 'figure' element is used to wrap images and related content, and the 'figcaption' element provides a caption for the figure.

The 'code' element is used to display computer code or other types of code examples on a web page.

Validating HTML code with a validator like W3C's validator ensures there are no errors or warnings in the code.

Placeholder images can be used for layout purposes before final images are chosen.

Free image resources like Unsplash, Pexels, and Pixabay offer high-quality images for free use.

Image editing software like Earp View and online tools like Canva and TinyPNG can help optimize images for web use.

The tutorial provides a comprehensive guide on adding and managing images in an HTML web page, improving accessibility, and optimizing performance.

Resources for free images and tools to compress and resize images are shared for practical applications.

Transcripts

play00:00

[Music]

play00:04

hello and welcome hi i'm dave

play00:07

this tutorial is part of a beginner's

play00:09

html series i'll be using the chrome web

play00:12

browser the visual studio code editor

play00:14

and the live server extension for visual

play00:16

studio code to view the web page there

play00:19

are links to these tools starter code

play00:21

files and all resources in the

play00:23

description below let's look at adding

play00:26

images to our web page until now we've

play00:28

only worked with text but html supports

play00:31

multimedia and images are a big part of

play00:34

that so what we want to do is go to

play00:36

visual studio code that i have here on

play00:38

the left and i have our project running

play00:41

here on the right and we're using live

play00:44

server for that

play00:45

so the live server has launched down

play00:47

here and you can click to close the

play00:49

server or open the server

play00:51

if you don't have the live server

play00:53

extension search for that here in the

play00:54

extensions

play00:56

find

play00:57

live server

play00:58

and that should pull up by ritwik day

play01:01

and you just want to install that and it

play01:03

can launch your page for you

play01:05

okay going back to what we need we have

play01:07

clicked on the file explorer

play01:10

in visual studio code and we need to

play01:12

create a folder to hold our image files

play01:15

so i'll type img to name this folder

play01:18

and it is a common practice to keep your

play01:21

images for your web page or web pages in

play01:24

a separate folder as you organize your

play01:26

project

play01:27

now that we've got that i'm going to

play01:28

grab some images and paste those in so

play01:31

we can work with them you'll be able to

play01:33

download these images from the files and

play01:36

resources that i share in the

play01:38

description however at the end of this

play01:40

tutorial i'm also going to go over some

play01:42

resources where you can download your

play01:45

own images that are free for use

play01:47

so now inside this image folder i'm just

play01:49

going to right click and choose paste

play01:53

and paste these images in

play01:55

let's look at what we've got i can click

play01:57

on the vacation image

play01:59

and i get this vacation image here and

play02:02

notice at the bottom we can see the size

play02:04

of the image it's 400 by 267. those are

play02:08

pixels

play02:09

we can look at this html logo that we're

play02:11

going to use it's 300 by 300 and we've

play02:14

got an image of the caribbean as well

play02:16

and it's 400 by 225 so i'm going to

play02:20

collapse this now and

play02:23

we're going to use those in our html

play02:25

code so now i'll click on the file

play02:27

explorer to hide the file tree and this

play02:30

all starts with the image tag so let's

play02:32

scroll down in our html to where we want

play02:35

to insert our first image in our page

play02:38

we're going to put it right below

play02:40

the paragraph that says this is my first

play02:42

web page in the i'm ready to learn html

play02:45

section okay let's start by typing img

play02:49

and pressing tab and then visual studio

play02:51

code will help us with that image

play02:53

abbreviation

play02:54

and it will automatically fill out the

play02:56

rest of the image element now notice the

play02:59

image element doesn't have a closing

play03:01

element like we see here with a heading

play03:03

or we see here with a paragraph

play03:05

sometimes you will see it

play03:07

with a slash like that at the end which

play03:10

means it's self closing html does not

play03:13

require that i should say html5 does not

play03:16

require that however if you are using

play03:18

this in the future in something like

play03:21

react it may require that so that's just

play03:24

something to be aware of right now i

play03:26

won't put that in because html5 does not

play03:29

require it and that's what we're

play03:30

focusing on okay notice the image

play03:33

element has a source attribute and an

play03:35

alt attribute automatically the source

play03:38

attribute tells html where to grab the

play03:41

image we want so we start by typing the

play03:44

folder name that we created now we see

play03:46

the folder here image slash and i can

play03:48

click on that and then it provides us

play03:51

the names of the images that we have

play03:53

well we want the html logo that i saved

play03:56

so i'll just click on that and there is

play03:58

the full relative path to our image it's

play04:02

in the same project here and it's on our

play04:05

local server

play04:07

in this area so we don't need that

play04:09

absolute url that would grab it from

play04:11

somewhere else on the web so this is

play04:13

once again a relative path to our folder

play04:16

and then to the image

play04:18

then we have the alt attribute now the

play04:20

alt attribute has a couple of purposes

play04:23

one is to help assistive technology for

play04:26

those that may not be able to see the

play04:29

image we are putting on the page the

play04:31

assistive technology like a screen

play04:32

reader will read the description of the

play04:34

image so let's put html5

play04:38

logo as the alt text but this also

play04:42

makes this alt text appear on the page

play04:46

if the image for some reason does not

play04:48

load so first of all let's just save our

play04:51

page and you'll see the logo now appear

play04:53

on the web page and suddenly our page

play04:55

got much more interesting it's awesome

play04:57

putting images into our web pages

play05:00

however let's go ahead and change the

play05:02

file name so that it doesn't find the

play05:05

image and let's see what happens on the

play05:07

page

play05:08

now we have a broken image icon but

play05:11

notice our alt text appears html5 logo

play05:14

so that is another purpose of the alt

play05:17

attribute i'll fix that so we get our

play05:19

logo back now there's other attributes

play05:22

that we can and probably should use with

play05:25

an image so one is title now we've seen

play05:28

the title attribute before remember the

play05:31

title

play05:32

text that we provide here in the title

play05:34

attribute is not accessible a screen

play05:36

reader will not read it so this can't be

play05:40

something that is very important that

play05:42

everybody should know this is text

play05:45

that can complement our image but it's

play05:48

not necessarily

play05:50

required the page is complete without it

play05:53

but we can add some complementary text

play05:55

here so i'll just put i am

play05:57

learning html5

play06:00

it doesn't have to be i don't even need

play06:01

the period really it doesn't have to be

play06:04

identical to the alt text at all this

play06:07

should be complementary but now if we

play06:09

mouse over the image we'll see

play06:11

the title pop-up that says i am learning

play06:13

html5 and that's complementary a couple

play06:16

of other attributes that way back in the

play06:19

1990s these were required however for

play06:22

years these have not been required but

play06:25

now they're coming back and it's width

play06:28

and height so if we define a width here

play06:31

and we want to provide the same width of

play06:34

our image now i saved this image with a

play06:37

file name that tells me it reminds me

play06:39

exactly the width and the height and

play06:42

it's 300 pixels by 300 pixels so i'll

play06:44

just put 300 if we just provide the

play06:47

width

play06:48

html will remember or shift

play06:52

the image to match so the aspect ratio

play06:55

stays the same so if i switch this to

play06:57

200

play06:59

and save notice it also adjusted the

play07:02

height of the image however we want to

play07:04

provide

play07:05

both we want to provide the width and

play07:08

the height

play07:09

and let's provide exactly what they are

play07:12

and i'll tell you why this is making a

play07:14

comeback and why it is now recommended

play07:17

to provide the width and the height even

play07:19

though we can change this in the future

play07:21

with css so i'm saying we provide these

play07:24

but yes css may change

play07:27

and override these values so why do we

play07:29

provide them well it's a little

play07:31

complicated but it's called cumulative

play07:34

layout shift if you've ever gone to a

play07:36

web page

play07:37

and you were getting ready to click on

play07:39

something and then you saw the page

play07:41

shift around maybe a pop-up ad appeared

play07:44

or a banner ad and you clicked on

play07:45

something that you didn't intend to

play07:47

click because the button or the image

play07:49

that you wanted to click suddenly moved

play07:51

that is cumulative layout shift so we

play07:54

provide the width and the height now to

play07:57

tell the browser hey

play07:59

this is going to take up some space here

play08:01

and this is how much if we don't provide

play08:03

that the browser shifts everything once

play08:05

it figures out the size of the image now

play08:08

once again you can still change the size

play08:11

of the image make it responsive which

play08:13

would adapt to mobile devices and

play08:15

everything else with css so we really

play08:18

provide this information with the width

play08:20

and the height

play08:21

just to give the browser an idea of the

play08:25

size and the aspect ratio coming in so

play08:28

it just kind of prepares itself and that

play08:31

helps just a little bit it is now

play08:33

recommended by google developers that we

play08:36

do provide the width and the height in

play08:38

the image element with our first image

play08:41

added now let's scroll down and add a

play08:43

second image to our page and we'll

play08:45

scroll down to our vacation area place

play08:48

i'd like to visit let's change this to

play08:50

places i'd like to visit

play08:52

and they've got i've heard good things

play08:55

about the caribbean so this seems like a

play08:57

great place to put an image of a

play08:59

caribbean beach so once again we'll type

play09:01

our img abbreviation and press tab

play09:05

and then visual studio code helps us out

play09:07

by providing

play09:08

the image element with the source and

play09:11

alt attributes ready to go so for the

play09:13

source once again type the name of our

play09:15

folder press slash and now we get the

play09:19

names of the images we have ready i'm

play09:21

going to choose caribbean

play09:23

and then in the alt area i'm going to

play09:26

type

play09:28

caribbean beach

play09:30

and now let's save and see what we get

play09:33

on the page when we scroll down because

play09:34

we have to scroll down a little ways to

play09:36

see it and there is our caribbean beach

play09:38

image so now let's add a title attribute

play09:42

that's just a little different than our

play09:43

alt attribute just complementary text

play09:46

and this is i

play09:48

want to visit a

play09:52

caribbean

play09:53

beach

play09:55

and we'll save that

play09:56

now we're getting closer but remember we

play09:58

should apply the width and the height

play10:01

attributes as well so let's click on the

play10:04

file explorer because i didn't have

play10:06

the width and the height saved in the

play10:08

name of this jpeg file dot jpg notice

play10:12

the other image we used was a dot png

play10:15

there are different image file types and

play10:18

those have different extensions so now

play10:20

i'll click the

play10:22

explorer up here we'll go back open the

play10:24

image folder and i'm going to click on

play10:27

the caribbean image

play10:29

now when i do that in visual studio code

play10:31

it shows the image and at the bottom i

play10:33

can see it is 400 pixels

play10:36

by 225 the first number is the width so

play10:39

i'm going to close this now

play10:42

click file explorer to hide that so i

play10:43

have a little more room and now i can

play10:46

say

play10:47

width

play10:48

equals 400

play10:50

and the height

play10:52

equals

play10:53

225

play10:55

and if we save everything should remain

play10:57

the same we've just provided these extra

play10:59

attributes now i've noticed

play11:01

that

play11:02

this wrapped a little strange here in

play11:04

visual studio code but know that the

play11:06

width and the height are still part of

play11:08

the image element that we started right

play11:10

here

play11:11

okay now that we've got that let's go

play11:13

ahead and remove the in from the jpeg so

play11:16

it will not find the image and make sure

play11:18

our alt text appears yes we've got a

play11:20

broken image icon but it says caribbean

play11:23

beach and that is our alt text so i'll

play11:25

fix that by putting the in back in the

play11:28

name spelling it correctly and the image

play11:30

shows up because now visual studio code

play11:32

can find the file for us okay we had to

play11:34

scroll down to see this image because

play11:36

our page is long enough

play11:38

that if we scroll to the top

play11:40

we don't see our vacation area anymore

play11:43

now this bottom of the page here you

play11:46

could call it a crease if it was a

play11:48

newspaper and a lot of the terminology

play11:50

kind of goes back to newspapers and

play11:52

magazines as we talk about layout

play11:54

and so this bottom part

play11:57

that defines the line

play11:59

that anything below that we do not see

play12:02

right now that's called the fold so

play12:04

anything

play12:06

after this area that we can now see is

play12:09

called below the fold so anything below

play12:11

the fold we have to scroll to see and

play12:14

now we scroll to see this image well

play12:16

there's one more attribute for this

play12:18

image that i want to talk about and it's

play12:20

the loading attribute

play12:22

and if we provide loading and we can set

play12:24

it equal to a value and there's two

play12:27

possible values the first one

play12:29

is eager but we never have to provide

play12:32

this this is the value by default so if

play12:35

we do not provide the loading attribute

play12:38

the loading attribute is always there

play12:40

and it's just set to eager we just don't

play12:42

see it

play12:43

but the one that we do need to provide

play12:46

is called lazy

play12:47

and what you want to do for performance

play12:49

for your web page

play12:51

is for any image that is below the fold

play12:54

that is one you do not currently see

play12:57

when the page loads you want to set the

play12:59

loading attribute to lazy and that means

play13:02

the browser will only load that image

play13:05

when it knows it is about to show it

play13:08

when we start to scroll

play13:10

now firefox the web browser firefox is

play13:14

much easier to demonstrate this in

play13:17

chrome wants to display the image much

play13:19

earlier so it right now this image is

play13:22

close enough

play13:23

to the area that we see that it will

play13:25

still load this caribbean image

play13:27

automatically that we have here so i

play13:30

need to create some extra space and i

play13:32

can demonstrate this to you by using

play13:35

something called chrome dev tools that's

play13:37

available in the browser so let's do

play13:39

that now i'm going to scroll back up

play13:42

above places i'd like to visit

play13:44

to the hr area the horizontal rule that

play13:47

we have going across right here before

play13:49

the i am also planning a vacation

play13:52

and i'll type an abbreviation

play13:55

br for a line break and then i'll put

play13:58

the asterisk for times and i'm going to

play14:00

put 150.

play14:02

this emit abbreviation that visual

play14:04

studio code supports will insert 150

play14:07

line breaks into our page there's no way

play14:09

i would want to type all of those

play14:11

individually but i can do it with this

play14:13

abbreviation so i'll click that and

play14:14

suddenly we have all of these line

play14:16

breaks

play14:17

if i save

play14:19

and we don't see this change but now

play14:21

there's a lot of space between the

play14:23

sections and that's what i need to

play14:24

demonstrate this so we've changed the

play14:26

code i'm going to click the maximize

play14:28

button here in chrome

play14:30

to bring the browser all the way out and

play14:33

now let's right click

play14:35

and choose inspect and you could also do

play14:38

this with keys

play14:40

and it would be ctrl shift and the

play14:42

letter i all at once the control key the

play14:44

shift key and the letter i i'm just

play14:46

going to right click and choose inspect

play14:48

either way we'll open up the dev tools

play14:51

in chrome

play14:52

and now that we're getting the dev tools

play14:54

open it will automatically open to the

play14:56

elements tab and notice how if i

play14:59

hover over an area

play15:01

in the elements now it highlights those

play15:03

on the left in the browser so here i've

play15:05

hovered over my goals for the year and

play15:07

we can see that i can hover over the

play15:09

section that discusses html and it

play15:12

highlights that section what we want to

play15:14

do though is click the two greater than

play15:16

symbols here at the top and i'm going to

play15:19

choose the network tab

play15:22

once it shows the network tab

play15:25

you'll want your disable cache

play15:28

to have the check mark

play15:30

and that is because browsers know to

play15:32

save images after they've loaded them

play15:35

initially so they can reload them fast

play15:38

but that is called the cache where it

play15:40

stores the images that's c-a-c-h-e

play15:43

we want to disable that cache so we can

play15:45

demonstrate this also

play15:47

in this area right below the filters you

play15:49

see all fetch slash xhr js css

play15:55

choose the image all we are interested

play15:57

in right now are the image files we

play15:58

don't want to see the rest of the files

play16:00

so once you've done all of that

play16:03

we're going to go ahead

play16:04

and reload the page so i'm going to

play16:07

click the reload icon up here in the

play16:09

browser

play16:10

and we've reloaded and notice down here

play16:14

we just see the html logo file that's

play16:17

all that has loaded right now

play16:19

and now as we scroll down as we get

play16:22

close to the caribbean image file we

play16:24

will see it load because it has

play16:26

the loading attribute set to lazy so

play16:29

only once we get close and i created a

play16:31

lot of space here so i'm scrolling down

play16:33

and fairly soon it should pop up there

play16:35

it is

play16:36

we don't see it on the page yet over

play16:38

here but it's getting close so chrome

play16:41

loaded it in preparation for us to see

play16:43

it but what that really helps is that it

play16:47

did not load the image right when we

play16:49

loaded our page so that made our page

play16:51

load faster it only loads these images

play16:54

when it thinks it's going to need them

play16:56

and that is lazy loading so now you can

play16:59

click the x here in the top right to

play17:01

close the dev tools then i'm going to

play17:04

click the

play17:05

maximize button again to get the browser

play17:07

back to the size we wanted it i'll drag

play17:09

it down here so we can see everything

play17:12

and then over in visual studio code i'm

play17:15

going to press ctrl z which is undo you

play17:18

could also go to the edit menu and

play17:20

choose undo but i'll do control z

play17:23

and get rid of all those line breaks but

play17:25

again if you want to demonstrate that

play17:28

you can just type in br and then how

play17:30

many of those elements you want in

play17:32

visual studio code will recognize that

play17:35

so that is a demonstration of a lazy

play17:38

loading technique that helps your pages

play17:40

load faster imagine if you had a page

play17:42

that had a dozen or 20 or even 50 images

play17:46

you wouldn't want the page to continue

play17:48

loading until it loaded all of those

play17:50

images so lazy loading is a very good

play17:53

performance technique especially when

play17:55

you're dealing with lots of images that

play17:57

are below the fold now let's add a new

play18:00

element to our page and it's something

play18:02

we're going to wrap this image in so

play18:04

i'll put it underneath the paragraph

play18:07

that says i've heard good things about

play18:08

the caribbean and i'm going to type the

play18:09

word

play18:10

figure and i'll press tab because figure

play18:13

is an element now i'll highlight the

play18:16

closing tag and press control x to cut

play18:19

you can also get these options that i

play18:21

use with the keyboard shortcuts in the

play18:24

edit menu there's cut copy and paste so

play18:27

it's ctrl x to cut and now at the end of

play18:30

the image here i'm going to paste which

play18:32

is control v and now i've got the

play18:34

opening

play18:35

and closing figure tags

play18:37

i'll highlight the image here and just

play18:39

tab it in as i'm used to seeing it so

play18:42

the image is inside the figure element

play18:45

now this by itself won't do much but it

play18:48

is saying we've got a figure with an

play18:50

image and we can add a caption and it

play18:53

tells the browser

play18:55

that the caption

play18:56

big caption element

play18:59

is related to the image which is better

play19:02

than just putting a paragraph underneath

play19:04

it the paragraph

play19:06

while we could visually see it's

play19:07

probably related to the image if it was

play19:09

right under the image it's not telling

play19:11

the browser or assistive technology that

play19:14

it is a caption for the image this

play19:17

spells it out for both the browser and

play19:19

assistive technology that yes what we

play19:21

put in here is directly related to this

play19:23

image but it doesn't have to be

play19:25

identical to the alt attribute of the

play19:28

image although it should kind of be a

play19:30

description of the image so i'll put

play19:32

caribbean

play19:34

beach

play19:36

getaway here as our fig caption and now

play19:39

it does show up on the page and notice

play19:42

the image has been indented just a

play19:44

little bit and once again we can change

play19:46

all of the layout of the page and how

play19:48

everything looks with css in the future

play19:50

but this is just the

play19:52

default behavior when we apply a figure

play19:54

it's indented somewhat so we have an

play19:56

image and the caption now let's go ahead

play19:59

and add our last image as well and we

play20:02

want to put that underneath

play20:04

margarita island reserve riviera cancun

play20:06

an actual resort in the mexican

play20:09

riviera cancun area so we'll scroll down

play20:12

here

play20:13

and underneath the address we want to

play20:15

add an image

play20:16

of at least the cancun area because

play20:19

that's where we're thinking about going

play20:21

so image and

play20:23

we press the tab button and we get the

play20:25

source and alt attribute again so let's

play20:28

type img that's the name of our folder

play20:30

and slash and now we've got our vacation

play20:33

image and that looks good

play20:35

and now we can put in the alt tag the

play20:39

cancun

play20:41

vacation for our vacation image

play20:44

and now in the title

play20:47

we can put something like

play20:50

it's

play20:51

five o clock

play20:55

somewhere because that's kind of related

play20:56

to margaritaville and jimmy buffett

play20:59

and he's famous for that saying and it's

play21:02

five o'clock somewhere there's something

play21:04

worth noting about this phrase we've got

play21:06

single quotations here inside of our

play21:09

double quotations now that's what's

play21:11

possible if we had used single quotes

play21:14

out here this would not be possible but

play21:16

we can use single quotes inside of our

play21:20

double quotes so this does work now

play21:22

let's go ahead and save

play21:24

and if we look at the page right

play21:26

underneath the address for the resort

play21:28

we're thinking about we've got our

play21:30

caribbean vacation image and if we mouse

play21:32

over we've got the title it's five

play21:35

o'clock somewhere that looks good but

play21:37

notice we don't have the space or the

play21:39

indentation that we get from the figure

play21:42

element so let's go ahead and put a

play21:44

figure element around this image as well

play21:47

so we'll type

play21:48

figure press tab once again i'm going to

play21:51

highlight the closing tag press control

play21:54

x

play21:56

underneath the image ctrl v to paste it

play21:58

in and if i save i should get some auto

play22:01

formatting and so now i've got the image

play22:03

indented inside the figure and let's add

play22:06

a caption here as well remember it's the

play22:08

fig caption element so i'll press tab

play22:11

and now let's put

play22:12

a caribbean

play22:18

vacation

play22:20

image

play22:22

so it's not quite the same as our alt

play22:24

attribute it's not the same as our title

play22:26

attribute but it's a nice description of

play22:29

this image okay we're not quite finished

play22:31

with our image it's easy to forget some

play22:34

of these attributes that we might not

play22:36

think about applying right away so

play22:38

one we know we need is the loading

play22:40

because this will absolutely be below

play22:42

the fold so let's set that to lazy as

play22:44

well and save and now let's click on the

play22:47

file explorer again in the top left and

play22:50

let's look at our vacation image and

play22:52

find the dimensions and we can see here

play22:55

at the bottom it's 400 pixels by

play22:58

267 pixels

play23:00

so now we can close that out

play23:02

and click on the file explorer to hide

play23:04

that again

play23:05

and let's provide width and height so

play23:08

the width is equal to

play23:10

400 and the height

play23:13

is equal to 267. so now we've provided

play23:18

all the correct attributes that we need

play23:20

for this image

play23:21

and everything looks good here on the

play23:23

page

play23:24

one more thing i want to add about

play23:26

figure figure is not just for images so

play23:30

let's scroll up here in vs code back to

play23:33

our html area

play23:35

and we do have

play23:36

our html logo right here underneath this

play23:40

is my first web page we'll scroll back

play23:42

up to this area here on the page as well

play23:45

and now we can add a figure underneath

play23:48

this image so i'll type the word figure

play23:51

again and now let's add a fig caption

play23:54

first the fig caption element needs to

play23:57

either be the first thing inside of a

play23:59

figure or the last thing it can be

play24:02

either or so here i'm going to type

play24:05

in example

play24:08

of html5

play24:10

code and that will be our caption so

play24:12

let's save that much and we see that

play24:14

here but really we have no content

play24:17

inside of our figure yet so let's go

play24:20

ahead and add some content and i'll add

play24:21

a paragraph which is a block level

play24:24

element we've discussed that in the past

play24:26

block versus inline

play24:29

and now for the text of the paragraph i

play24:31

want to put that inside of an inline

play24:34

element that will not create a line

play24:36

break and it's a code element

play24:39

code

play24:40

this element code helps you actually

play24:42

display code if that's what you want to

play24:44

use you can use other elements inside of

play24:47

it though so if i want to display html

play24:49

code i can't simply put an h1 element

play24:52

because then it applies this element to

play24:54

the page we need to go back to what we

play24:57

learned about html entities to provide

play25:01

the less than sign which is an ampersand

play25:04

lt with a semicolon then i could type h1

play25:07

and then ampersand gt with a semicolon

play25:10

for the greater than symbol and now i'll

play25:13

put hello world

play25:15

with an exclamation mark and then i'll

play25:17

do the

play25:19

less than html entity again and then a

play25:21

slash h1 and then the ampersand

play25:25

gt semicolon which is greater than

play25:28

in a semicolon and now if we look at the

play25:30

page

play25:32

we have got an example of some html code

play25:35

printed directly to our web page so this

play25:38

is another example of how to use a

play25:40

figure element it can contain other

play25:42

content such as a code sample and you

play25:45

can have a caption at the top just as

play25:47

easily as you can at the bottom of the

play25:50

figure okay our code for the lesson is

play25:52

complete but i do want to show you some

play25:54

image resources but before we can do

play25:57

that we need to validate our code with

play26:00

the validator once again so i'm going to

play26:03

grab this and make it just a little

play26:05

wider so the validator shows up better

play26:07

here remember we're at validator

play26:08

validator.w3.org

play26:11

we want to click validate by file upload

play26:15

click choose file make sure you're in

play26:17

the correct folder once this folder area

play26:19

opens up

play26:20

and i'm in the lesson 6 folder and then

play26:24

i'm going to choose the index file that

play26:26

we were working in and choose open and

play26:28

from there i can choose

play26:30

check

play26:31

and it should check the html it says

play26:33

document checking completed no errors or

play26:36

warnings to show that's what you want if

play26:38

you do get an error warning it should

play26:40

tell you what line it's on in your code

play26:42

and you can refer to visual studio code

play26:45

find the line and fix the error that it

play26:47

is telling you about so once again

play26:49

validate your code before you finish

play26:52

before finishing this image lesson i

play26:54

wanted to share some resources that i

play26:56

use now there are many resources out

play26:58

there so these are just a few and i'm

play27:00

sure you can find others as well first

play27:02

there's this article that has 21 of the

play27:04

best placeholder image generators using

play27:07

placeholder images is very common

play27:10

practice when you're laying out a page

play27:12

and you're not sure the images you want

play27:13

to use yet what you can do is take a uh

play27:16

url like this from

play27:18

placeholder.com and put it in as the

play27:21

value of the source attribute in your

play27:24

html image tag and then it will generate

play27:28

a placeholder image until you find the

play27:30

image you want to put there some of

play27:32

these other image generators actually

play27:34

provide pictures like the second one on

play27:36

the list called phil murray provides

play27:38

random pictures of the actor bill murray

play27:40

but it works in the same way you put the

play27:42

url in

play27:44

for the source attribute value in your

play27:47

image tag and then you will get a

play27:49

picture of bill murray from this image

play27:51

generator okay the next several sites

play27:54

are just great places to download

play27:56

freely

play27:58

usable images as it says right here

play28:01

these are licensed for free use it's

play28:03

important to download and use images

play28:06

that you actually have the usage rights

play28:08

to so this is a good way to do that find

play28:11

them on unsplash.com is my favorite you

play28:14

can also go to pexels.com as you see

play28:17

here very similar site you can also go

play28:20

to gratisography.com

play28:23

and this site has unique and kind of

play28:26

funny pictures so that's kind of neat

play28:28

too

play28:29

and you never know what you'll see on

play28:30

these the next one is

play28:33

pixabay.com

play28:34

very much like the previous ones i

play28:36

showed you from there i want to suggest

play28:38

some free image software it's called

play28:41

earphone view i've used this for years

play28:44

it's just really lightweight software

play28:46

easy to use and you can load in an image

play28:49

and resize it and that's what i usually

play28:52

use it for you can also crop images with

play28:54

it i'll pull it over here as i have an

play28:56

instance of orphan view open already

play28:59

you can see i've got in this large

play29:01

picture of the caribbean beach but you

play29:03

can go to the edit menu

play29:06

and you can see different options that

play29:07

are here such as the crop

play29:10

and of course we'd have to select an

play29:12

area to do it but in the image menu

play29:14

there is a resize resample

play29:17

and here you can see you've got the

play29:19

dimensions of the image but i can resize

play29:21

this it's a huge image right now at

play29:24

5464 pixels wide

play29:26

i'll take it to say 400 pixels

play29:30

and it stays in sync so it keeps the

play29:33

aspect ratio and now we're at

play29:35

225 for the height so you see the

play29:38

current size and then the new size i'll

play29:40

click ok

play29:41

and it brings it down to a much smaller

play29:43

nicer size easy to save as well as a new

play29:46

name

play29:47

and a great little software to use from

play29:49

there i also recommend

play29:52

canva.com now disclosure here there is a

play29:56

paid subscription to use this site

play29:59

however i believe they have a free

play30:02

account that you can at least get some

play30:04

of the features or try it out so

play30:05

canva.com is good after you've got your

play30:08

images

play30:09

and even after you have resized your

play30:12

images to the smaller size that you

play30:14

would probably use on your web page it's

play30:16

also a good idea to drag and drop them

play30:19

into tiny png

play30:22

i believe this also comes up if you type

play30:25

in tinyjpg.com

play30:27

but it will compress the images and they

play30:30

will still look good but they get a

play30:33

smaller file size so they load to that

play30:35

web page faster which is always

play30:37

important so all of these resources are

play30:39

great and i will put links to all of

play30:41

them in the description below remember

play30:43

to keep striving for progress over

play30:45

perfection and a little progress every

play30:48

day will go a very long way

play30:50

please give this video a like if it's

play30:52

helped you and thank you for watching

play30:54

and subscribing you're helping my

play30:55

channel grow have a great day and let's

play30:58

write more code together very soon

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
HTML TutorialWeb DevelopmentImage OptimizationLazy LoadingVisual Studio CodeWeb AccessibilityFigure CaptionPlaceholder ImagesImage CompressionWeb Performance