Chapter 4. Adding Graphics
It’s safe to say that the creators of the Internet never imagined that it would look the way it does today—thick with pictures, ads, videos, and animated graphics. They designed a meeting place for leading academic minds; we ended up with something closer to a Sri Lankan bazaar. But no one’s complaining, because the Web would be an awfully drab place without pictures.
In this chapter, you’ll master the art of web graphics. You’ll learn how to add ordinary images to a web page and to position them perfectly. You’ll also see how to use styles to jazz up your pictures with borders, captions, and background effects.
Introducing the <img> Element
Web page pictures don’t live in HTML files. Instead, you store them as separate files, like banana.jpg and photo01.jpg. To display a picture in a web page, you use the <img> element.
For example, here’s an <img> element that displays the picture banana.jpg:
<img src="banana.jpg" />
When a browser reads the <img> element above, it sends out a request for the banana.jpg file. After retrieving it, the browser inserts the picture into the page in place of the <img> element. If the image file is large or the Internet connection is very slow, you might actually see the web page text appear first, before the picture shows up.
Here’s an example that puts a picture in the second paragraph of a typical (albeit somewhat boring) web page (Figure 4-1):
<!DOCTYPE html> <html> <head> <title>Two paragraphs, One picture</title> </head> <body> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access