Add Image in HTML Code Simple Guide For Beginners

Adding an image to an HTML document can enhance its visual appeal and provide additional information. Here, we'll guide you through the steps of adding an image using HTML.

Embedding Images in HTML using <img>
Embedding Images in HTML using <img>

Before we dive into the code, let's understand that images in HTML are inserted using the <img> tag. This tag requires an 'src' attribute, which points to the image file's location.

HTML input types
HTML input types

Inserting an Image: The Basics

The basic syntax for the <img> tag is as follows:

About HTML tags
About HTML tags

<img src="image_url" alt="alternative_text">

The 'src' attribute is mandatory and holds the image's URL. The 'alt' attribute is optional but recommended. It provides an alternative text description for the image, useful for accessibility and when the image fails to load.

How To Add Text To Image In PHP
How To Add Text To Image In PHP

Absolute vs. Relative Paths

You can use either an absolute or a relative path for your image's URL. An absolute path starts from the root directory of your website, while a relative path starts from the current directory.

If your image is in the same directory as your HTML file, you can use a relative path like this: <img src="image_name.png">. If it's in a different directory, you might need to use a relative path with dots and slashes, like this: <img src="../images/image_name.png">.

a table with instructions to use the code for an appliance or service center
a table with instructions to use the code for an appliance or service center

Image Sizes and Alignment

By default, an image will maintain its original size. However, you can control its size and alignment using the 'width' and 'height' attributes, or the 'style' attribute for CSS.

For instance, to set the image's width to 300 pixels and height to auto, you can use: <img src="image_url" style="width:300px;height:auto;">. To align it to the right, use: <img src="image_url" style="float:right;">.

HTML Full Notes for Beginners 💻
HTML Full Notes for Beginners 💻

Practical Examples

Let's consider a case where you want to display an image of a sunset on your webpage. First, ensure the image file (sunset.jpg) is uploaded to your server at 'images/sunset.jpg'.

Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript
Digital Code Hub
Digital Code Hub
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
an image with the text, click box for more information and then clickbox for more information
an image with the text, click box for more information and then clickbox for more information
a poster with some text on it and an image of a computer coder in the background
a poster with some text on it and an image of a computer coder in the background
HTML Attributes — Beginner Guide
HTML Attributes — Beginner Guide
Auto Resize Images In HTML CSS
Auto Resize Images In HTML CSS
a person typing on a laptop with the text how to code a link in html
a person typing on a laptop with the text how to code a link in html
the three ways to apply css in html and wordpress, including an info sheet
the three ways to apply css in html and wordpress, including an info sheet
the four ways to add css in your web page or email address info sheet
the four ways to add css in your web page or email address info sheet
Text to Code: 3 AI Tools
Text to Code: 3 AI Tools
using HTML and CSS create a beautiful flower #codet #htmlcss #project #code #programmer #codinglife
using HTML and CSS create a beautiful flower #codet #htmlcss #project #code #programmer #codinglife
an image with the text how to place text over image in html css
an image with the text how to place text over image in html css
the different types of images that can be used in an image formating process, including text
the different types of images that can be used in an image formating process, including text
a table that has some type of text in red and black on the bottom right hand corner
a table that has some type of text in red and black on the bottom right hand corner
Add CSS In HTML
Add CSS In HTML
an image of the back side of a computer screen with text that reads, add css to html
an image of the back side of a computer screen with text that reads, add css to html
an image of a computer screen with some pictures on it and the text, color fuel
an image of a computer screen with some pictures on it and the text, color fuel
Coding a Stylish Blog Design Layout in HTML & CSS
Coding a Stylish Blog Design Layout in HTML & CSS
an image is shown with the text in different colors and font styles, including black
an image is shown with the text in different colors and font styles, including black

Then, you can insert it into your HTML document like this:

<img src="images/sunset.jpg" alt="A beautiful sunset">

In this case, if the image fails to load, visitors will see the text 'A beautiful sunset' instead.

Adding Captions with FIGURE and FIGCAPTION

If you'd like to add a caption to your image, you can use the <figure> and <figcaption> tags. Here's an example:

<figure> <img src="images/sunset.jpg" alt="A beautiful sunset"> <figcaption>Captivating colors of a sunset</figcaption> </figure>

And there you have it! You've now learned how to add images to your HTML documents, along with various ways to control their appearance. Happy coding!

Related Articles

Html Table Design Examples With Code Simple Html Web Code Mysql Sql Pdf Html Table Border Code Example What Is The Div Tag In Html Used For Html Div Tags Greek Word For Greek Language Html Table Css Examples Beautiful With Code What Is Div And Class In Html Tutorialspoint Dart Pdf