Table of Contents
tag" > Basic writing method: use the How to add an image to a webpage using HTML5? tag
with " > Add image link: wrap How to add an image to a webpage using HTML5? with
Add titles using
and
(New in HTML5)
Image size control and other properties
Home Web Front-end H5 Tutorial How to add an image to a webpage using HTML5?

How to add an image to a webpage using HTML5?

Jul 27, 2025 am 02:46 AM
html5 picture

The core method of adding images to a web page is to use the <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560396668.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > tag and match the necessary attributes. First, use the self-closed <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560396668.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > tag, and specify the image path through the src attribute, and provide alternative text; second, you can wrap the image through the <a> tag to achieve click jump; again, use the new <figure> and <figcaption> tags added by HTML5 to structure the image and title; in addition, you can set width, height or use CSS to control the image size, and achieve lazy loading through loading="lazy" to optimize performance.

How to add an image to a webpage using HTML5?

Adding images to web pages is one of the basic operations of HTML, and HTML5 provides more concise and semantic tags and attributes to achieve this. The most core way is to use the <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560420539.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > tag and match src and alt attributes.

How to add an image to a webpage using HTML5?

Basic writing method: use the <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560396668.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > tag

The easiest and most straightforward way to insert an image into a web page is to use the HTML <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560396668.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > tag. This tag is a self-closing tag, which means no additional end tag is required.

Example:

How to add an image to a webpage using HTML5?
 <img src="/static/imghw/default1.png"  data-src="example.jpg"  class="lazy" alt="descriptive text">
  • src is a must, it tells the browser the location of the image file.
  • alt is also recommended to write. This alternative text will be displayed when the image cannot be loaded, and it is also important for barrier-free access.

The path can be a relative path (such as images/photo.jpg ), or an absolute path or network address (such as https://example.com/photo.jpg ).


Add image link: wrap <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560668748.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > with <a>

If you want the image to be clicked and redirected to another page or resource, you can put <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175355560668748.jpeg" class="lazy" alt="How to add an image to a webpage using HTML5?" > in the <a> tag.

How to add an image to a webpage using HTML5?

Example:

 <a href="https://example.com">
  <img src="/static/imghw/default1.png"  data-src="logo.png"  class="lazy" alt="Company Logo">
</a>

In this way, users can jump to the specified link by clicking on the image. This practice is often used for website logo or advertising graphics display.


Add titles using <figure> and <figcaption> (New in HTML5)

HTML5 introduces <figure> and <figcaption> tags to display pictures and their explanatory texts more structured, which is particularly suitable for mixed text content.

Example:

 <figure>
  <img src="/static/imghw/default1.png"  data-src="nature.jpg"  class="lazy" alt="Natural Landscape">
  <figcaption>Beautiful mountain scenery</figcaption>
</figure>
  • <figure> means a separate content block, usually related to the main content but can exist separately.
  • <figcaption> is its child element used to add a title or description to the image.

This method not only makes the code clearer, but also improves semantics and SEO effects.


Image size control and other properties

You can control the size of the image by setting width and height properties:

 <img src="/static/imghw/default1.png"  data-src="photo.jpg"  class="lazy" alt="personal photo"    style="max-width:90%"  style="max-width:90%">

However, it is recommended to use CSS to control the size, so that the style and structure are separated, making it easier to maintain:

 <img src="/static/imghw/default1.png"  data-src="photo.jpg"  class="lazy" alt="personal photo"   style="max-width:90%">

In addition, you can also use loading="lazy" to delay loading of images to improve page loading speed:

 <img src="/static/imghw/default1.png"  data-src="big-image.jpg"  class="lazy" alt="large image" loading="lazy">

Basically that's it. Although adding images seems simple, in actual development, whether the path is correct, whether the alternative text is appropriate, and whether the responsive processing is in place will affect the user experience and accessibility.

The above is the detailed content of How to add an image to a webpage using HTML5?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1510
276
Defining custom vocabularies using HTML5 Schema.org markup. Defining custom vocabularies using HTML5 Schema.org markup. Jul 31, 2025 am 10:50 AM

The Schema.org tag helps search engines understand the structured data format of web page content through semantic tags (such as item scope, item type, itemprop); it can be used to define custom vocabulary, methods include extending existing types or using additionalType to introduce new types; in actual applications, keeping the structure clear, using official attributes first, testing code validity, and ensuring that custom types are accessible; precautions include accepting partial support, avoiding spelling errors, and choosing a suitable format such as JSON-LD.

How does the HTML5 parser handle errors? How does the HTML5 parser handle errors? Aug 02, 2025 am 07:51 AM

HTML5parsershandlemalformedHTMLbyfollowingadeterministicalgorithmtoensureconsistentandrobustrendering.1.Formismatchedorunclosedtags,theparserautomaticallyclosestagsandadjustsnestingbasedoncontext,suchasclosingabeforeaandreopeningitafterward.2.Withimp

What are HTML5 data attributes? What are HTML5 data attributes? Aug 06, 2025 pm 05:39 PM

HTML5dataattributesarecustom,validHTMLattributesusedtostoreextrainformationinelementsforJavaScriptorCSS.1.Theyaredefinedasdata-*attributes,likedata-user-id="123".2.Theyallowembeddingprivate,customdatadirectlyinmarkupwithoutaffectinglayoutor

What is the difference between  and  in HTML5? What is the difference between and in HTML5? Aug 04, 2025 am 11:02 AM

Please clarify the two HTML5 elements or attributes you want to compare, such as versus, versus, or id with class, so that I can provide a clear and practical explanation of the difference.

How does the spellcheck attribute work in HTML5? How does the spellcheck attribute work in HTML5? Aug 03, 2025 pm 02:46 PM

ThespellcheckattributeinHTML5controlswhetherthebrowserchecksspellingandgrammarineditableelements.2.Itworksoninputfields,textareas,andcontenteditableelementsbyunderliningerrorsinredorgreen.3.Setitto"true"toenableor"false"todisable,

How to create an ordered list in HTML5? How to create an ordered list in HTML5? Jul 30, 2025 am 05:23 AM

Creating an ordered list in HTML5 requires use and tags. 1. Use the ordered list to define the ordered list, and use it to represent each item internally. 2. You can specify the starting number through the start attribute, 3. Set the number type such as numerals, letters or Roman numerals through the type attribute. 4. It is recommended to use CSS' list-style-type or custom counter to achieve more flexible style control to separate structures and styles.

How to use the canvas API for basic drawing in HTML5? How to use the canvas API for basic drawing in HTML5? Aug 07, 2025 am 07:15 AM

To use HTML5Canvas API for basic drawing, first create the canvas element in HTML and set the width and height attribute, and then get its 2D rendering context through JavaScript; 1. Use fillRect, strokeRect and clearRect to draw and clearRect; 2. Create paths and draw lines or custom shapes through beginPath, moveTo, lineTo and closePath; 3. Use arc to draw circles or arcs; 4. Use fillText and strokeText to add fill or stroke text; 5. Set fillStyle, strokeStyle, lin

What are the supported audio formats in HTML5? What are the supported audio formats in HTML5? Aug 05, 2025 pm 08:29 PM

HTML5 audio format support varies from browser to browser. The most commonly used formats include: 1.MP3 (.mp3, audio/mpeg, supported by all mainstream browsers, with the best compatibility); 2.WAV (.wav, audio/wav, support better but large files, suitable for short audio); 3.OGG (.ogg/.oga, audio/ogg, Chrome, Firefox, Opera support, Safari and IE are not supported, open source free); 4.AAC (.aac/.m4a, audio/aac, Safari, Chrome, Edge support, Firefox support is limited, often used in Apple devices). To ensure compatibility, the

See all articles