Home Web Front-end Front-end Q&A How to add pictures to html

How to add pictures to html

Apr 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="图片文件路径" alt="图片描述">
Copy after login

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
Copy after login

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

/Users/username/Documents/test.jpg
Copy after login

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="./images/test.jpg" alt="测试图片">
Copy after login

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
Copy after login

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="https://www.baidu.com/img/bd_logo1.png" alt="百度logo">
Copy after login

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>
Copy after login

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>
Copy after login

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 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

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

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)

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React Components: Creating Reusable Elements in HTML React Components: Creating Reusable Elements in HTML Apr 08, 2025 pm 05:53 PM

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React's Ecosystem: Libraries, Tools, and Best Practices React's Ecosystem: Libraries, Tools, and Best Practices Apr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

Frontend Development with React: Advantages and Techniques Frontend Development with React: Advantages and Techniques Apr 17, 2025 am 12:25 AM

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React vs. Backend Frameworks: A Comparison React vs. Backend Frameworks: A Comparison Apr 13, 2025 am 12:06 AM

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

React and the Frontend Stack: The Tools and Technologies React and the Frontend Stack: The Tools and Technologies Apr 10, 2025 am 09:34 AM

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

Understanding React's Primary Function: The Frontend Perspective Understanding React's Primary Function: The Frontend Perspective Apr 18, 2025 am 12:15 AM

React's main functions include componentized thinking, state management and virtual DOM. 1) The idea of ​​componentization allows splitting the UI into reusable parts to improve code readability and maintainability. 2) State management manages dynamic data through state and props, and changes trigger UI updates. 3) Virtual DOM optimization performance, update the UI through the calculation of the minimum operation of DOM replica in memory.

See all articles