HTML Images Syntax
In HTML, images are defined with the tag.
The 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:
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:
Note: The alt attribute is required. A web page will not validate correctly without it. If a browser cannot find an image, it will display the value of the alt attribute.
Image Size - Width and Height
You can use the style attribute to specify the width and height of an image.
Alternatively, you can use the width and height attributes:
Note: Always specify the width and height of an image. If width and height are not specified, the page might flicker while the image loads. 提示: 指定图像的高度和宽度的一个很好的习惯。如果图像指定了高度宽度,页面加载时就会保留指定的尺寸。如果没有指定图片的大小,加载页面时有可能会破坏HTML页面的整体布局。
Width and Height, or Style?
The width, height, and style attributes are valid in HTML5.
However, we suggest using the style attribute. It prevents styles sheets from changing the size of images:
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:
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:
You can read more about file paths in the chapter HTML File Paths.
Animated Images
HTML allows animated GIFs:
Image as a Link
To use an image as a link, put the tag inside the tag:
Note: border:0; is added to prevent IE9 (and earlier) from displaying a border around the image (when the image is a link).
Image Floating
Use the CSS float property to let the image float to the right or to the left of a text:
Floating Images
Float the image to the right:
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a floating image.
Float the image to the left:
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a floating image.
Tip: To learn more about CSS Float, read our CSS Float Tutorial.
Image Maps
The
In the image below, click on the computer, the phone, or the cup of coffee:
Image Maps
Click on the computer, the phone, or the cup of coffee to go to a new page and read more about the topic:
The name attribute of the
The
Background Image
To add a background image on an HTML element, use the CSS property background-image:
To add a background image on a web page, specify the background-image property on the BODY element:
Background Image
To add a background image on a paragraph, specify the background-image property on the P element:
...
The
HTML5 introduced the
The
Each
The browser will use the first
Example
Show one picture if the browser window (viewport) is a minimum of 650 pixels, and another image if not, but larger than 465 pixels.
The picture Element
Resize the browser to see different versions of the picture loading at different viewport sizes.
The browser looks for the first source element where the media query matches the user's current viewport width,
and fetches the image specified in the srcset attribute.
The img element is required as the last child tag of the picture declaration block.
The img element is used to provide backward compatibility for browsers that do not support the picture element, or if none of the source tags matched.
Note: The picture element is not supported in IE12 and earlier or Safari 9.0 and earlier.
Note: Always specify an element as the last child element of the
HTML Screen Readers
A screen reader is a software program that reads the HTML code, converts the text, and allows the user to "listen" to the content. Screen readers are useful for people who are blind, visually impaired, or learning disabled.
Chapter Summary
Use the HTML element to define an image
Use the HTML src attribute to define the URL of the image
Use the HTML alt attribute to define an alternate text for an image, if it cannot be displayed
Use the HTML width and height attributes to define the size of the image
Use the CSS width and height properties to define the size of the image (alternatively)
Use the CSS float property to let the image float
Use the HTML