We continue to explore the basics of HTML. In this lesson, you’ll learn how to insert an image into an HTML document, set it as a background, adjust its size, alignment, text wrapping, and more. We’ll go over it with clear examples and results. Images on web pages can be background or regular images. What’s the difference?
Background image — an image placed as the background of a web page, allowing text, images, tables, or other elements to be added on top.
Regular image — a separate element that displaces other page elements. Recommended image formats for web pages: JPEG (JPG), GIF, PNG.
Use the <background> attribute in the<body> tag:
<body background="image_name.jpg">
Example: Place the image file “fon.jpg” next to your HTML file.
<html>
<head>
<title>My First HTML Page on overcod.net</title>
</head>
<body background="fon.jpg">
Hello, this is my first page on overcod.net.
</body>
</html>
Note: File names must be in Latin characters!
Use the <img> tag with the src attribute to specify the file path:
<img src="image_name.jpg">
Example:
<html>
<head>
<title>My First HTML Page on overcod.net</title>
</head>
<body>
<img src="kartinka.jpg">
Hello, this is my first page on overcod.net.
</body>
</html>
If the image is located in a folder, specify the path:
<img src="img/image_name.jpg">
For images from other websites:
<img src="https://www.overcod.net/images/example.jpg">
Use the align attribute:
left — aligns the image to the left; text flows on the right.right — aligns the image to the right; text flows on the left.
<img src="kartinka.jpg" align="right">
Use the hspace (horizontal margin) and vspace (vertical margin) attributes:
<img src="kartinka.jpg" hspace="20" vspace="20">
Use the width and height attributes to specify the size:
<img src="kartinka.jpg" width="100" height="50">
The border attribute sets the border thickness:
<img src="kartinka.jpg" border="3">
Let’s combine everything:
<html>
<head>
<title>My First HTML Page on overcod.net</title>
</head>
<body background="fon.jpg">
<h2>My Dream!</h2>
<img src="https://www.overcod.net/images/example.jpg"
title="Welcome to overcod.net!"
align="right" hspace="20" vspace="20">
<p>I have a dream:</p>
<p>→ Travel around the world;</p>
<p>→ Create interesting projects;</p>
<p>→ Earn more;</p>
<p>→ Have more time for creativity.</p>
</body>
</html>
Try implementing these examples yourself! 🌟
Knowledge Check: HTML Basics Test
63
HTML from Scratch: Adding Audio and Video to Your Website — For Beginners. Lesson #26
82
HTML from Scratch: The download Attribute in HTML5 (for downloading files). Lesson #25
149
HTML from Scratch: Commenting in HTML. Lesson #24
507
HTML from Scratch: The DOCTYPE Tag. Lesson #23
124
Leave a Reply