Detailed explanation of common background attributes

Detailed explanation of common background attributes

background-color

Sets the background color for the element

valuedescribe
color_nameSpecifies that the color value is the background color of the color name (such as red).
hex_numberSpecifies the background color whose color value is a hexadecimal value (for example #ff0000).
rgb_numberThe specified color value is the background color of rgb code (such as rgb(255,0,0)).
transparentDefault. The background color is transparent.
inheritSpecifies that the setting of the background color attribute should be inherited from the parent element.
background-color: #ffff00;
background-color: yellow;
background-color: rgb(255,255,0);
background-color: transparent;

Display effect

background-image

Set background image for element

valuedescribe
url('URL')The path to the image.
noneDefault value. The background image is not displayed.
inheritSpecifies that the setting of the background image attribute should be inherited from the parent element.
background-image:url(img/banner04.jpg) ;

Display effect

background-repeat

Set whether and how to repeat the background image; The default background image repeats horizontally and vertically.

valuedescribe
repeatDefault. The background image repeats vertically and horizontally.
repeat-xThe background image repeats horizontally.
repeat-yThe background image repeats vertically.
no-repeatThe background image will be displayed only once.
inheritSpecifies that the setting of the background repeat attribute should be inherited from the parent element.
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-size: 100px 50px;
        border: 1px solid red;    
    }
    .box1{
        background-repeat: no-repeat;				
    }
    .box2{
        background-repeat: repeat-x;
    }
    .box3{
        background-repeat: repeat-y;
    }
    .box4{
        background-repeat: repeat;
    }
</style>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>

</body>

Display effect

background-position

Sets the starting position of the background image

valuedescribe
top left |top center|top right|center left |center center|center right|bottom left |bottom center| bottom rightIf you specify only one keyword, the second value will be "center". Default: 0%.
x% y%The first value is the horizontal position and the second value is the vertical position. The upper left corner is 0%. The lower right corner is 100%. If you specify only one value, the other value will be 50%.
xpos yposThe first value is the horizontal position and the second value is the vertical position. The upper left corner is 0. The units are pixels (0px, 0px) or any other CSS units. If you specify only one value, the other value will be 50%. You can mix the% and position values.
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-size: 100px 50px;
        background-repeat: no-repeat;	
        border: 1px solid red;         
}
    .box1{
        /* background-position: center center; */
        background-position: center; 
    }
    .box2{
        /* background-position: 50% 50%; */
        background-position: 50% ;
    }
    .box3{
    	background-position: 50px 25px;
    }
    .box4{
    	background-position: 50% 25px;
    }
</style>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>
</body>

Display effect

background-size

Specifies the size of the background image

valuedescribe
lengthSets the height and width of the background image. The first value sets the width and the second value sets the height. If only one value is set, the second value is set to "auto".
percentageSets the width and height of the background image as a percentage of the parent element. The first value sets the width and the second value sets the height. If only one value is set, the second value is set to "auto".
coverThe background image is expanded to be large enough so that the background image completely covers the background area. Some parts of the background image may not be displayed in the background positioning area.
containExpand the image to the maximum size so that its width and height fully adapt to the content area.
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-repeat: no-repeat;	
        border: 1px solid red;
    }
    .box1{
        /* background-size: 100px 50px; */
        background-size: 50% 50%;
    }
    .box2{
        /* background-size: 100px; */
        background-size: 50%;
    }
    .box3{
        background-size: cover;
    }
    .box4{
        background-size: contain;
    }

</style>
</head>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>
</body>

Display effect

background-attachment

Sets whether the background image is fixed or scrolls with the rest of the page

valuedescribe
scrollDefault value. The background image moves as the rest of the page scrolls.
fixedWhen the rest of the page scrolls, the background image does not move.
inheritSpecifies that the setting of the background attachment attribute should be inherited from the parent element.
background-attachment: fixed;
background-attachment: scroll; 

effect

Keywords: Front-end css3 html5 css

Added by sitorush on Thu, 16 Dec 2021 18:00:02 +0200