HTML5 IMAGES CHAPTER 10
TENTH TUTORIAL ON HTML IMAGES
HTML Images Syntax
In HTML, images are defined with the
<img>
tag.
The
<img>
tag is
empty, it contains attributes only, and does not have a closing tag.
The
src
attribute
specifies the URL (web address) of the image:
<img src="url">
The alt
Attribute
The
alt
attribute
provides an alternate text for an image, if the user for some reason cannot
view it (because of slow connection, an error in the src attribute, or if the
user uses a screen reader).
The value of the
alt
attribute should describe the image:
<!DOCTYPE html>
<html>
<body>
<p>If a browser cannot find the image, it
will display the alternate text:</p>
<img src="wrongname.gif"
alt="IMAGE HERE">
</body>
</html>
OUTPUT
If a browser cannot find the image, it
will display the alternate text:
“IMAGE
HERE”
Image Size -
Width and Height
You can use the style
attribute
to specify the width and height of an image.
<!DOCTYPE html>
<html>
<body>
<h2>Image Size</h2>
<p>Use the style attribute to specify the
width and height of an image:</p>
<img src="img_girl.jpg"
alt="Girl in a jacket"
style="width:500px;height:600px;">
</body>
</html>
OUTPUT
Image Size
Use the style attribute to specify the width and height of an
image:
Alternatively, you can use
the width
and height
attributes:
<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">
The
width
and height
attributes always define the width and height of the
image in pixels.
Width and
Height, or Style?
The
width
, height
, and style
attributes are valid
in HTML.
However, we suggest using the
style
attribute. It
prevents styles sheets from changing the size of images:
<!DOCTYPE html>
<html>
<head>
<style>
/* This stylesheet sets the width of all images
to 100%: */
img {
width:
100%;
}
</style>
</head>
<body>
<h2>Styling Images</h2>
<p>The image below has the width
attribute set to 128 pixels, but the stylesheet overrides it, and sets the
width to 100%.</p>
<img src="html5.gif"
alt="HTML5 Icon" width="128"
height="128"><p>The image below uses the style attribute,
where the width is set to 128 pixels which overrides the stylesheet:</p>
<img src="html5.gif"
alt="HTML5 Icon" style="width:128px;height:128px;">
</body>
</html>
OUTPUT
Styling Images
The image below has the width attribute set to 128 pixels,
but the stylesheet overrides it, and sets the width to 100%.
The image below uses the style attribute, where the
width is set to 128 pixels which overrides the stylesheet:
Images in
Another Folder
If not specified, the browser expects to find the image in the
same folder as the web page.
However, it is common to store images in a sub-folder. You must
then include the folder name in the
src
attribute:
<!DOCTYPE html>
<html>
<body>
<h2>Images in Another Folder</h2>
<p>It is common to store images in a
sub-folder. You must then include the folder name in the src
attribute:</p>
<img src="/images/html5.gif"
alt="HTML5 Icon" style="width:128px;height:128px;">
</body>
</html>
OUTPUT
Images in Another Folder
It is common to store images in a
sub-folder. You must then include the folder name in the src attribute:
Images on
Another Server
Some web sites store their images on image servers.
Actually, you can access images from any web address in the world:
<!DOCTYPE html>
<html>
<body>
<h2>Images on Another Server</h2>
<img
src="https://www.w3schools.com/images/unknownprgrammer.jpg"
alt="unknown" style="width:104px;height:142px;">
</body>
</html>
OUTPUT
Images on Another Server
Animated
Images
HTML allows animated GIFs:
<!DOCTYPE html>
<html>
<body>
<h2>Animated
Images</h2>
<p>The GIF standard
allows moving images.</p>
<img
src="programming.gif" alt="Computer man"
style="width:48px;height:48px;">
</body>
</html>
OUTPUT
Animated Images
The GIF standard allows moving
images.
Image as a
Link
To use an image as a link, put
the <img>
tag inside the <a>
tag:
<!DOCTYPE html>
<html>
<body>
<h2>Image as a Link</h2>
<p>The image is a link. You can click on
it.</p>
<a href="default.asp">
<img
src="smiley.gif" alt="HTML tutorial"
style="width:42px;height:42px;border:0;">
</a>
<p>Add "border:0;" to prevent
IE9 (and earlier) from displaying a border around the image.</p>
</body>
</html>
OUTPUT
Image as a Link
The image is a
link. You can click on it.
Add
"border:0;" to prevent IE9 (and earlier) from displaying a border
around the image.
Image
Floating
Use the CSS float
property
to let the image float to the right or to the left of a text:
<!DOCTYPE html>
<html>
<body>
<h2>Floating Images</h2>
<p><strong>Float the image to the
right:</strong></p>
<p>
<img src="smiley.gif"
alt="Smiley face"
style="float:right;width:42px;height:42px;">
A paragraph with a floating image. A paragraph
with a floating image. A paragraph with a floating image.
</p>
<p><strong>Float the image to the
left:</strong></p>
<p>
<img src="smiley.gif"
alt="Smiley face"
style="float:left;width:42px;height:42px;">
A paragraph with a floating image. A paragraph
with a floating image. A paragraph with a floating image.
</p>
</body>
</html>
OUTPUT
Floating
Images
Float the image to the right:

Float the image to the left:
