CSS is a language used for web design and layout, which can achieve various styles and effects, including the setting of background size. Background size refers to the size and display mode of the background image when the web page is loaded. Setting the background size properly can not only improve the beauty of the web page, but also improve the user experience. Next, we will explain in detail how to use CSS to set the background size.
1. Use the background-size attribute
The background-size attribute is used to set the background size. It can specify the size and display mode of the background image. There are three commonly used values: cover, contain and px. Specifically, they represent respectively:
Below we use several examples to demonstrate how to use the background-size attribute.
<!DOCTYPE html> <html> <head> <style> body { background-image: url('https://picsum.photos/id/100/800/400'); background-size: cover; } </style> </head> <body> <h1>Hello World!</h1> <p>这是一段测试文本。</p> </body> </html>
In the above code, we use a random image provided by Lorem Picsum as the background. In CSS, we spread the background image over the entire background area, and the effect is as follows:
As you can see, the height of the image is stretched, but the width is also fully The retention makes the entire background look more beautiful.
<!DOCTYPE html> <html> <head> <style> body { background-image: url('https://picsum.photos/id/100/800/400'); background-size: contain; } </style> </head> <body> <h1>Hello World!</h1> <p>这是一段测试文本。</p> </body> </html>
In the above code, we also use a random picture as the background. In CSS, the contain attribute is used so that the image is not stretched, but is displayed as centered as possible in the background area. The effect is as follows:
#As you can see from the picture effect, although the picture cannot completely cover the background area, the entire design feels more balanced.
<!DOCTYPE html> <html> <head> <style> body { background-image: url('https://picsum.photos/id/100/800/400'); background-size: 600px; } </style> </head> <body> <h1>Hello World!</h1> <p>这是一段测试文本。</p> </body> </html>
In the above code, we also use a random picture as the background. In CSS, a fixed value setting of 600px size is used, and the effect is as follows:
As you can see, since the image size is limited to 600px, the entire background area is left blank. . Therefore, when using the px value to set the background size, you need to adjust it according to the specific situation to ensure that the size of the background image can be coordinated with the entire page.
2. Use the background-position attribute
In addition to the background-size attribute, we can also use the background-position attribute to set the background position. This property allows the background image to be centered horizontally or vertically within the background area, with the corresponding offset. The specific usage is as follows:
<!DOCTYPE html> <html> <head> <style> body { background-image: url('https://picsum.photos/id/100/800/400'); background-size: cover; background-position: center; } </style> </head> <body> <h1>Hello World!</h1> <p>这是一段测试文本。</p> </body> </html>
In the above code, we set the centered position, and the effect is as follows:
As you can see from the renderings, centered The aligned position coincides with the center of the image, and the visual effect is very good.
3. Summary
The above is about how to use CSS to set the background size. In actual development, focusing on user experience and overall page aesthetics should be the most important. Through good background settings, we can make our web pages more attractive and achieve better design effects.
The above is the detailed content of css set background size. For more information, please follow other related articles on the PHP Chinese website!