LWD Ch07A Img
LWD Ch07A Img
LWD Ch07A Img
ADDING IMAGES
OVERVIEW
• PNG
• JPEG
• GIF
• SVG
PNG, JPEG,
GIF, and WebP
are bitmapped
formats.
SVG is a vector
format.
The img Element
<img src="" alt="">
The img element tells the browser to make a server request for
the image and display it in its place:
• Alternative text (alt text) should convey the same information and
function as the image.
• Consider what the alt text would sound like when read aloud by a
screen reader. Is it helpful or a hindrance?
• When an image is used as a link, the alt text serves as the linked text.
Write what you’d want the link to say, don’t just describe the image.
• Avoid starting alt text with “An image of” or “Graphic of”.
width and height Attributes
<img src="flowers.svg" alt="" width="120" height="160">
• The width and height attributes set the dimensions of the image
on the page in number of pixels.
• They help the browser maintain space for the image in the layout
while the files load.
• Don’t use width and height attributes if you are sizing the image
with style sheets or if the size changes in a responsive layout.
• Those shapes and paths are described in a text file using the
Scalable Vector Graphic markup language.
• <img> element
• <object> element
You can add an .svg file to the page with the img element:
PROS:
• Easy and familiar
• Universally supported
CONS:
• Can’t manipulate the SVG with scripts or styles.
• The SVG can’t contain any external resources such as
images or fonts.
Embedding SVGs with object
The content of the object element is a fallback text or image that
displays if the SVG is not supported:
<object type="image/svg+xml" data="pizza.svg">
<img src="pizza.png" alt="pizza">
</object>
PROS:
• SVG can be scripted and use eternal files (images and fonts).
CONS:
• You can’t change styles with the same CSS used for the document.
• May be buggy in some browsers.
Inline SVGs with the svg Element
You can paste the content of the SVG text file directly into the HTML
source. This is called using the SVG inline.
PROS:
• Can take full advantage of scripting and styling the SVG because
the elements in the SVG are part of the DOM for the document.
CONS:
• Code can get extremely long and unwieldy.
• Harder to maintain images embedded in the source
• Can’t take advantage of caching repeated images
• Not universally supported
Example of an Inline SVG