Home >Web Front-end >CSS Tutorial >How to set image background with css

How to set image background with css

青灯夜游
青灯夜游Original
2021-04-20 16:42:0932534browse

How to set the image background in css: 1. Use the background-image attribute, the syntax "background-image:url('image address');"; 2. Use the background attribute, the syntax "background:url(' The map's address');".

How to set image background with css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In css, you can use the background-image attribute or background attribute to set the image background.

css background-image property

The background-image property sets the background image of an element.

The background of an element is the total size of the element, including padding and borders (but not margins).

By default, the background-image is placed in the upper left corner of the element and repeats vertically and horizontally.

Attribute value:

  • url('URL') Path pointing to the image.

  • none Default value. No background image is displayed.

  • inherit specifies that the setting of the background-image property should be inherited from the parent element.

Example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>css设置背景图片</title>
		<style>
			body {
				background-image: url(&#39;img/1.jpg&#39;);
			}
		</style>
	</head>

	<body>

	</body>

</html>

Rendering:

How to set image background with css

##css background attribute

The background attribute is a property specifically set for the background. It is a shorthand property that can set all background properties in one statement. (Learning video sharing:

css video tutorial)

Background attributes that can be set:

  • background-color: Specifies the background color to be used.

  • background-position: Specifies the position of the background image.

  • background-size: Specifies the size of the background image.

  • background-repeat: Specifies how to repeat the background image.

  • background-origin: Specifies the positioning area of ​​the background image.

  • background-clip: Specifies the drawing area of ​​the background.

  • background-attachment: Specifies whether the background image is fixed or scrolls with the rest of the page.

  • background-image: Specifies the background image to use.

It can be seen that the background-image attribute is to set the background image attribute for the html page. Let’s take a look at its usage

background-image:url(1.jpg);

This is given in url() By specifying the path of the image, you can set a background image for the div box; it seems simple, but one thing to note is that the box for setting the background image must have substantial width and height, so that the background image can be displayed on the display.


If setting the above background attributes one by one seems cumbersome, in fact, some attributes can be set together. Such css background expression can save and optimize css file code. Example:

background:url(bgimg.gif) no-repeat 5px 5px;

Explanation as shown below:

How to set image background with css

Example:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>背景图片设置</title>
        <style>
            .demo{
                position:fixed;
                top: 0;
                left: 0;
                width:100%;
                height:100%;
                min-width: 1000px;
                z-index:-10;
                zoom: 1;
                background-color: #fff;
                background: url(img/1.jpg);
                background-repeat: no-repeat;
                background-size: cover;
                -webkit-background-size: cover;
                -o-background-size: cover;
                background-position: center 0;
            }
        </style>
    </head>
    <body>
        <div class="demo"></div>
    </body>
</html>

Rendering:


How to set image background with css

For more programming-related knowledge, please visit:

Introduction to Programming! !

The above is the detailed content of How to set image background with css. 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