02. Images

To place an image on a page, use the <img> tag. The <img> tag has no closing tag.

Important Attributes of the img tag

src
Used to locate the source of the image.
width/height
Set the width and height of the image.
Note that specifying the width and height together may distort the photo.
If you'd like for the photo to scale, just use one of the two.
alt
Used to show an alternative desciption.
In case we want to display additional information for users who cannot see the image.

We can specify the image from a source on another server using an absolute link, or from our own using a relative link.

 
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" alt="HTML 5 Logo" width="200px">
HTML5 logo

Or relative:

 
<img src="/code/img/main-logo.svg" alt="Code Snippets Academy logo">
Code Snippets Academy logo

Sample image formats

There are several image formats, each with their advantages and disadvantages. It's important to understand the filesize and its quality, as a super-high resolution may take a lot longer to load on the page. If your users are impatient, they may navigate elsewhere!

JPEG

JPEG is a lossy compression format that compresses the original image. The upside to JPEGs are a smaller file size, but the drawback is that the image becomes not as clear.

GIF

GIFs contain no more than 256 colors, thus lowering the file size. We can have some pixels in the GIF to be transparent.

PNG

With a PNG, we can have 16 million colors, as well as alpha transparency. Very high resolution, so high file size.

The holy grail of them all: SVG

Scalable Vector Graphics (SVG) is an XML-based vector image format for 2d graphics. The great thing about these is no matter how large or small it gets, the image does not blur or lose quality.

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

$ Check price
29.9929.99Amazon 4.5 logo(63+ reviews)

More Front-end resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad