Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Elements

0/1

HTML Attributes

0/1

HTML Headings

0/1

HTML Paragraphs

0/1

HTML Styles

0/1

HTML Formatting

0/1

HTML Quotation

0/1

HTML Comments

0/1

HTML Colors

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Block and Inline

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML - The Head Element

0/1

HTML Style Guide

0/1

HTML Entities

0/1

HTML Symbols

0/1
Text lesson

Images

HTML Images

IMG_3830

Example

<img src=”image.jpg” alt=”Flowers”>

Images Syntax

The <img> tag in HTML is utilized for embedding images within a webpage.

Rather than being physically inserted, images are externally linked to the webpage, with the <img> tag serving as a placeholder for the specified image.

Notably, the <img> tag is a self-closing element, consisting solely of attributes and lacking an end tag.

Syntax Example

<img src=”url alt=”alternatetext>

Src Attribute

The essential ‘src‘ attribute determines the image’s path or URL.

Keep in mind, it is during the webpage loading process that the browser retrieves the image from a server to display it on the page. Hence, it’s crucial to maintain the image’s location consistent relative to the webpage to prevent the appearance of a broken link symbol to your visitors. If the browser fails to locate the image, it will display a broken link icon along with the ‘alt’ text, which serves as an alternative description.

Example

<img src=”your image source/image.jpg” alt=”image name”>

When an image cannot be located by a browser, it will show the text specified in the ‘alt’ attribute instead.

 

Click to Learn

The alt Attribute

The required alt attribute provides alternate text for an image if the user cannot view it for some reason, such as a slow connection, an error in the src attribute, or the use of a screen reader.

The alt attribute’s value should describe the image:

Example

<img src=”img_chania.jpg” alt=”Flowers in Chania”>

If a browser cannot locate an image, it will display the alt attribute’s value:

Example

<img src=”wrongname.gif” alt=”Flowers in Chania”>
Tip: A screen reader is a software program that interprets the HTML code, enabling users to “listen” to the content. Screen readers are particularly helpful for individuals who are visually impaired or have learning disabilities.

Width and Height

The ‘style‘ attribute can be employed to define the dimensions of an image, including its width and height.

Example

<img src=”image.jpg” alt=”your image” style=”width:500px;height:600px;”>

 

Click to Learn

 

Width and Height, or Style?

The width, height, and style attributes are all valid in HTML.

However, we recommend using the style attribute as it prevents stylesheets from altering the size of images.

Example

<!DOCTYPE html>
<html>
<head>
<style>
img {
  width: 100%;
}
</style>
</head>
<body>

<img src=”image.gif” alt=”Image Icon” width=”128″ height=”128″>

<img src=”image.gif” alt=”Image Icon” style=”width:128px;height:128px;”>

</body>
</html>

 

Images in Folder

When your images are located in a sub-folder, it’s necessary to include the folder name in the src attribute.

Example

<img src=”/images/image.gif” alt=”image Icon” style=”width:128px;height:128px;”>

Images on Another Server

To link to an image hosted on a different server, you need to use a complete URL in the ‘src’ attribute, known as an absolute URL.

Example

<img src=”https://code7school.com/images/image.jpg” alt=”code7school”>

Important Information : Considerations for using external images: Such images may be protected by copyright, and using them without authorization could infringe on copyright laws. Moreover, you have no control over external images; they could be deleted or altered without notice.

Animated Images

 

HTML supports animated GIFs.

Example

<img src=”programming.gif” alt=”Computer Man” style=”width:48px;height:48px;”>

Image as a Link

For an image to act as a link, place the <img> tag within the <a> tag.

Example

<a href=”https://code7school.com/”>
  <img src=”image.gif” alt=”Tutorial” style=”width:42px;height:42px;”>
</a>

Image Floating

Utilize the CSS float property to position the image to the left or right of text.

 

Example

<p><img src=”smiley.gif” alt=”Smiley face” style=”float:right;width:42px;height:42px;”>
The image will float to the right of the text.</p>

<p><img src=”smiley.gif” alt=”Smiley face” style=”float:left;width:42px;height:42px;”>
The image will float to the left of the text.</p>

Chapter Summary

  • Employ the <img> tag in HTML to insert an image.
  • Utilize the ‘src‘ attribute in HTML to specify the image’s URL.
  • Apply the ‘alt‘ attribute in HTML to provide alternative text for the image in case it fails to display.
  • Set the dimensions of the image using the ‘width’ and ‘height’ attributes in HTML or the ‘width‘ and ‘height‘ properties in CSS.
  • To position the image to the left or right, use the ‘float‘ property in CSS.