How to add pictures to html
Nowadays, with the development of the Internet, web design has become an important skill. In web design, pictures are often very important elements. Therefore, how to add images in HTML is basic knowledge that every web designer needs to master. This article will introduce the basic image tags, image attributes and common image formats in HTML, as well as how to optimize and quote images.
1. Basic image tags and attributes
In HTML, images can be inserted through the tag. The following is the most basic syntax of the
tag:
<img src="图片的url" alt="图片的描述">
Among them, the src attribute is required, which specifies the URL of the image file, that is, the address of the image on the server. The alt attribute is optional and defines the alternative text that is displayed when the image cannot be loaded. For example:
<img src="https://example.com/image.jpg" alt="这是一张图片">
If the image file and the HTML file are located in the same directory, you can use a relative URL:
<img src="image.jpg" alt="这是一张图片">
You can also use a relative path to specify the subdirectory where the image is located:
<img src="images/image.jpg" alt="这是一张图片">
In addition to the src and alt attributes, there are also some commonly used attributes that can be used to control the appearance and behavior of images, such as:
- width and height attributes: specify the width and height of the image respectively, Units can be pixels (px) or percentages (%).
- border attribute: Specify the border width of the image, the unit can be pixels (px).
- align attribute: Specifies the horizontal alignment of the image, the value can be left, right or center.
- title attribute: Specifies the prompt text displayed when the user hovers the mouse over the image.
For example, the following code can center a picture and specify the width, height and border at the same time:
<img src="image.jpg" alt="这是一张图片" width="200" height="150" border="1" align="center">
2. Common picture formats
In web design, there are three commonly used image formats: JPEG, PNG and GIF.
JPEG (also known as JPG) is a lossy compression format commonly used to store photos and other real-life images. The compression quality of JPEG can be controlled by adjusting the compression ratio. The higher the compression ratio, the worse the image quality and the smaller the file size.
PNG (Portable Network Graphics) is a lossless compression format that can display transparency and higher color depth and is suitable for saving pixel-level graphics and icons. PNGs generally have larger file sizes than JPEGs, but have better quality.
GIF (Graphics Interchange Format) is a bitmap-based graphics format that supports animation and transparency, and can compress multiple pictures into one file. GIF is usually used to save small dynamic pictures, such as emojis and small animations.
When choosing an image format, you need to decide which format to use based on the type and purpose of the image. For example, when you need to save color gradients and rich details in the picture, you can use the JPEG format; and when you need to save transparency and higher quality, you can use the PNG format.
3. Optimize and quote images
In web design, optimizing and citing images is also very important. Optimizing images can reduce file size and make pages load faster. Here are some common optimization methods:
- Compress images: Reduce file size without affecting image quality.
- Crop the image: Keep only the required part of the image and reduce the file size.
- Caching images: Use browser cache to avoid downloading the same images repeatedly.
- Use CSS Sprites: Merge multiple small pictures into one large picture, use background positioning in CSS to display the required parts, and reduce the number of HTTP requests.
When quoting images, you need to pay attention to the following points:
- The image file path must be correct: if the path is wrong, the image cannot be loaded.
- Pictures should match the theme of the webpage: inappropriate pictures may make the webpage look cluttered.
- Don’t oversize images: Overly large images will increase page loading time.
There are many ways to reference images. For example, you can use relative URLs and absolute URLs, or you can use the base tag to specify the base URL. Here is some sample code:
Use relative URL:
<img src="../images/image.jpg" alt="这是一张图片">
Use absolute URL:
<img src="https://example.com/image.jpg" alt="这是一张图片">
Use base tag:
<img src="images/image.jpg" alt="这是一张图片">
Summary
In web design, pictures are a very important element. In order to correctly reference and optimize images, we need to understand the basic image tags and attributes in HTML, as well as choose the correct image format and optimization method. Only by mastering this knowledge can we design beautiful and efficient web pages.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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

Vue 2's reactivity system struggles with direct array index setting, length modification, and object property addition/deletion. Developers can use Vue's mutation methods and Vue.set() to ensure reactivity.

TypeScript enhances React development by providing type safety, improving code quality, and offering better IDE support, thus reducing errors and improving maintainability.

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

The article explains using useReducer for complex state management in React, detailing its benefits over useState and how to integrate it with useEffect for side effects.

Functional components in Vue.js are stateless, lightweight, and lack lifecycle hooks, ideal for rendering pure data and optimizing performance. They differ from stateful components by not having state or reactivity, using render functions directly, a

The article discusses strategies and tools for ensuring React components are accessible, focusing on semantic HTML, ARIA attributes, keyboard navigation, and color contrast. It recommends using tools like eslint-plugin-jsx-a11y and axe-core for testi
