```The img tag has two main attributes: src and"/> ```The img tag has two main attributes: src and">
search
HomeWeb Front-endFront-end Q&AHow to add pictures to html
How to add pictures to htmlApr 23, 2023 am 10:19 AM

HTML is a markup language used to create text, images, videos and other content in web pages. Pictures are one of the essential elements in web page production. How to correctly add pictures is a skill that every web page producer needs to master.

1. Basic syntax

In HTML, use the img tag to insert pictures. The full name of img is "image", which means picture. The following is the basic syntax of the img tag:

<img  src="/static/imghwm/default1.png" data-src="图片文件路径" class="lazy" alt="How to add pictures to html" >

The img tag has two main attributes: src and alt. src refers to the path of the image file, and alt refers to the text description that will be displayed when the page cannot display the image normally.

Among them, the src attribute must be filled in, while the alt attribute is optional. If the image path is incorrect, the image will not be displayed. If the alt attribute is not set, a string of meaningless text may be displayed on the page where the image cannot be displayed normally.

The image path can be a local path or a network path. The local path refers to the path of the image file stored on the local computer; the network path refers to the path of the image file accessed through the network, such as images downloaded from the Internet.

2. The path of local pictures

The paths of local pictures are divided into absolute paths and relative paths. The absolute path specifies the location of the image in the entire file system, and you need to write the complete path; the relative path is the path description starting from the HTML file, and you only need to write the location of the image relative to the HTML file.

The following are two ways of writing local image paths.

  1. Absolute path

The absolute path refers to the location of the image in the entire file system. Depending on the operating system, the absolute path will be slightly different. In Windows systems, absolute paths start from the drive letter, for example:

C:\Users\Administrator\Pictures\test.jpg

In Mac OS X systems, absolute paths start from the root directory, for example:

/Users/username/Documents/test.jpg

The disadvantages of this approach are , each web page must use the complete file path. If the file storage location changes, the code needs to be modified.

  1. Relative path

The relative path refers to the location of the image relative to the HTML file. The simpler the directory structure, the easier it is to use relative paths. In HTML files, "." represents the directory where the current file is located, and ".." represents the directory above the directory where the current file is located. If the image and the HTML file are in the same directory, relative paths can be used, for example:

<img  src="/static/imghwm/default1.png" data-src="./images/test.jpg" class="lazy" alt="How to add pictures to html" >

If the image is stored in the images folder in the directory where the HTML file is located, the path is "./images/test.jpg".

3. Internet pictures

Internet pictures refer to pictures obtained from the Internet. In HTML, you can use the img tag to insert network images just like local images.

Network images are usually represented by URL (Uniform Resource Locator), which is a string composed of protocol name, host name, file path and file name. For example, the URL to access Baidu pictures is:

https://www.baidu.com/img/bd_logo1.png

Among them, "https://" is the protocol name, indicating that the HTTPS protocol is used for data transmission; "www.baidu.com" is the host name; "img/ bd_logo1.png" is the file path and file name.

When adding network pictures, just copy the URL of the network picture to the src attribute of the img tag, for example:

<img  src="/static/imghwm/default1.png" data-src="https://www.baidu.com/img/bd_logo1.png" class="lazy" alt="How to add pictures to html" >

Network pictures are more convenient, don’t worry about the path problem, as long as the picture If the URL is no problem, you can use the img tag to insert the image directly into the HTML.

4. Image Adaptation

In the process of web page production, a common need is to make the image size adaptive, so that it can have better display effects on different devices and screens. . Commonly used methods include using CSS to set properties such as width, height, and max-width.

  1. Use CSS to set the width and height

The width and height attributes in CSS can set the width and height of the image respectively, for example:

<style>
    img {
        width: 100%;
        height: auto;
    }
</style>

where , setting width to 100% means that the width of the picture will change as the available space changes; setting height to auto means that the height will be automatically adjusted according to the proportion of the width, maintaining the original proportion.

  1. Use CSS to set max-width

Using the max-width attribute can make the image automatically shrink proportionally when it exceeds a certain width, but it will not be smaller than its original size. For example:

<style>
    img {
        max-width: 100%;
        height: auto;
    }
</style>

Among them, max-width is set to 100%, which means that the maximum width of the picture will not exceed its available space and will not exceed the original size. Height is set to auto, which means that the height will be automatically adjusted according to the ratio of the width. , keeping the original proportions.

In short, when adding images to web pages, you must consider issues such as image path, image description, image size and adjustment. As long as you master the basic syntax and common methods, you can flexibly use the img tag in HTML to make the web page more colorful.

The above is the detailed content of How to add pictures to html. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is useEffect? How do you use it to perform side effects?What is useEffect? How do you use it to perform side effects?Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Explain the concept of lazy loading.Explain the concept of lazy loading.Mar 13, 2025 pm 07:47 PM

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits?How does currying work in JavaScript, and what are its benefits?Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

How does the React reconciliation algorithm work?How does the React reconciliation algorithm work?Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

How do you prevent default behavior in event handlers?How do you prevent default behavior in event handlers?Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

What is useContext? How do you use it to share state between components?What is useContext? How do you use it to share state between components?Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

What are the advantages and disadvantages of controlled and uncontrolled components?What are the advantages and disadvantages of controlled and uncontrolled components?Mar 19, 2025 pm 04:16 PM

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)