Figure & Caption
Introduction:
In web development, images and videos are crucial for engaging your audience. However, simply slapping a picture onto a webpage isn’t enough. You need to effectively present your content through “Figure & Caption” – a combination of images and accompanying text that enhances understanding and adds visual appeal. This tutorial will guide you through the basics of using media in HTML, focusing on how to create and manage these elements.
Understanding Media in HTML
HTML’s <img> tag is the primary tool for inserting images. The src attribute specifies the URL of the image file, and the alt attribute provides alternative text for accessibility and SEO. The width and height attributes control the image’s size. However, the real power of “Figure & Caption” lies in the combination of the image and the accompanying text.
Creating a Basic Figure & Caption
Let's start with a simple example. Here's how to create a basic figure and caption:
<!DOCTYPE html>
<html>
<head>
<title>Figure & Caption Example</title>
</head>
<body>
<h1>My Awesome Website</h1>
<img src="https://netgramnews.com/images/logo.png" alt="NetGram Logo" width="200" height="100">
<p>This is a paragraph of text about my website.</p>
</body>
</html>
In this example:
src="https://netgramnews.com/images/logo.png": This line tells the browser to load the image from the URL provided. Important: Replace"https://netgramnews.com/images/logo.png"with the actual URL of your image.alt="NetGram Logo": This provides alternative text for screen readers and users who can't see the image. It's crucial for accessibility.width="200" height="100": These attributes set the width and height of the image, respectively. Adjust these values to fit your design.
Adding a Caption
The caption attribute allows you to add a descriptive text that appears below the image.
<!DOCTYPE html>
<html>
<head>
<title>Figure & Caption Example</title>
</head>
<body>
<h1>My Awesome Website</h1>
<img src="https://netgramnews.com/images/logo.png" alt="NetGram Logo" width="200" height="100">
<p>This is a paragraph of text about my website.</p>
<p><b>This image illustrates the key features of our platform.</b></p>
</body>
</html>
Here, the caption attribute adds the text "This image illustrates the key features of our platform." It's a good practice to use a descriptive caption that complements the image.
Styling Figures & Captions
You can style your figures and captions using CSS. Here's a simple example:
<!DOCTYPE html>
<html>
<head>
<title>Figure & Caption Example</title>
<style>
img {
max-width: 100%; /* Ensures images don't overflow their containers */
height: auto;
margin-bottom: 10px;
}
p {
font-style: italic;
}
</style>
</head>
<body>
<h1>My Awesome Website</h1>
<img src="https://netgramnews.com/images/logo.png" alt="NetGram Logo" width="200" height="100">
<p>This is a paragraph of text about my website.</p>
<p><b>This image illustrates the key features of our platform.</b></p>
</body>
</html>
This CSS snippet sets the max-width property on the img tag to prevent it from overflowing its container. It also sets the height to auto to maintain the image's aspect ratio. The p tag styles the paragraph text to be italic.
Advanced Techniques
- Image Galleries: For displaying multiple images, you can use the
srcsetattribute to specify different image sizes for different screen resolutions. - Image Galleries: For displaying multiple images, you can use the
srcsetattribute to specify different image sizes for different screen resolutions. - Lazy Loading: Implement lazy loading to improve page load times by only loading images as they come into view.
Summary & Key Takeaways
<img>tag: The fundamental HTML tag for inserting images.altattribute: Essential for accessibility and SEO.captionattribute: Adds a descriptive text below the image.- CSS Styling: Use CSS to control the appearance of your figures and captions.
💡 Tip: Always prioritize accessibility by providing descriptive alt text and using appropriate image sizes. Don't rely solely on the image to convey information.
🖥️ Try It Yourself
Here are a few exercises to practice:
- Simple Image: Create an image with the
srcattribute set to a URL and thealtattribute set to a brief description. - Caption Placement: Add a caption to the image using the
captionattribute. - Styling: Modify the CSS to change the image's size and add a background color.
- Lazy Loading: Add the
srcsetattribute to the<img>tag to load images only when they come into view.

