Detailed explanation of the basic usage of the img image tag in HTML/XHTML

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a web page.
HTML/XHTML Image <img /> Tag
In XHTML, an image is defined using the <img /> tag. <img /> is an unpaired tag.

Basic syntax:

XML/HTML CodeCopy content to clipboard
  1. < img   src = "url"   />   

The <img /> tag determines the image source through the src attribute. The url is a relative or absolute image address.

Image tag attributes:
src: Image source, required. Specify the source address of the displayed image, which can be a relative address or an absolute address.
alt: Alternative text, can be omitted. Used for replacement text displayed when the image cannot be displayed or the browser blocks the image.
title: Image hint text, can be omitted. When the mouse hovers over the picture, relevant text will be displayed.
width: The width of the image display, which can be omitted. The unit is pixel.
height: The height of the image display, which can be omitted. The unit is pixel.

Text Alternative Attribute (alt)
The text replacement attribute alt of the image tag is not a required attribute, but it is a very important attribute. When the browser fails to read the image for some reason, the alternative text will be displayed instead of the original image to provide the missing relevant image information. This attribute also helps users who use text-only browsers understand the content of the web page.
Therefore, it is a good habit to add a meaningful alt text replacement attribute to each image.

<img /> tag usage practice
Create an images folder under the e:html folder to store image files. Right-click the picture below and select "Save Picture As" to save the picture to the images folder for future use.
201636120500949.jpg (350×318)

Edit our XHTML example1.html and make the following changes in the div tag with id="main-content":

XML/HTML CodeCopy content to clipboard
  1. < h3 > Article Title </ h3 >   
  2. < p > Article content </ p >   
  3. < p > < img   src = "images/flower_1.jpg"   alt = "flowers"   /> </ p >   

In this way, we display an image on the web page.

Specify the image display size
You can add width or height attributes to the <img /> tag to manually specify the image display size:

XML/HTML CodeCopy content to clipboard
  1. < img   src = "images/flower_1.jpg"   alt = "flowers"   width = "350"   height = "270"   />   

hint
In general, the image size attribute is ignored and the original image size is displayed by default or the browser adaptive size is displayed. Specifying an inappropriate image display size may cause the image to be distorted.
Since it takes a certain amount of time to load images, you should try to reduce the size of the images while ensuring the quality of the images in order to achieve a good user experience.

Further reading
Pixel: We can figuratively think of a pixel as the smallest dot that a computer can display. For example, the screen resolution we often say is 1024*768, which means that the screen has 1024 (pixels) dots horizontally and 768 (pixels) dots vertically. When used as a unit, it is generally written as pix by default.

<<:  Share 13 basic syntax of Typescript

>>:  Two box models in web pages (W3C box model, IE box model)

Recommend

Pay attention to the use of HTML tags in web page creation

HTML has attempted to move away from presentation...

iframe adaptive size implementation code

Page domain relationship: The main page a.html bel...

CSS method of clearing float and BFC

BFC BFC: Block Formatting Context BFC layout rule...

Comparison of the usage of EXISTS and IN in MySQL

1. Usage: (1) EXISTS usage select a.batchName,a.p...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...

What to do if you forget the initial password when installing MySQL on Mac

Forgetting the password is a headache. What shoul...

A brief discussion on the use of React.FC and React.Component

Table of contents 1. React.FC<> 2. class xx...

Let's talk in detail about the props attributes of components in Vue

Table of contents Question 1: How are props used ...

Web Design Tutorial (6): Keep your passion for design

<br />Previous article: Web Design Tutorial ...

MySQL Series II Multi-Instance Configuration

Tutorial Series MySQL series: Basic concepts of M...

Introduction to the properties of B-Tree

B-tree is a common data structure. Along with him...

Details on using bimface in vue

Table of contents 1. Install Vue scaffolding 2. C...

Detailed explanation of component communication in react

Table of contents Parent component communicates w...