CSS ways to implement full-screen adaptive background images (3 ways)

one

margin:0px;
background: url(images/bg.png) no-repeat;
background-size:100% 100%;
background-attachment:fixed;

tips:

url(images/bg.png)——The location of the image path;

no-repeat——The pictures are not repeated;

center 0px——center is the positioning from the left side of the page, 0px is the positioning from the top of the page;

background-position: center 0——It is the positioning of the picture, the same as above;

background-size: cover;——Expand the background image to a large enough size 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;

min-height: 100vh;——The height of the window. "Viewport" refers to the size of the visible area inside the browser, which is the size of window.innerWidth/ window.innerHeight.

two

 
background: url("bg.png") no-repeat;
height:100%;
width:100%;
overflow: hidden;
background-size:cover;//或者background-size:100%;

three

Specify a background image for the body tag so that the background image fills the entire browser viewport. In fact, this solution can be effective for all block-level containers. The width and height of the block-level container are dynamic, so the background image will automatically expand and contract to fill the entire container.

The style of the css body tag is as follows:

 
body {
    
    
/* 加载背景图 */
background-image: url(images/bg.jpg);
/* 背景图垂直、水平均居中 */
background-position: center center;
/* 背景图不平铺 */
background-repeat: no-repeat;
/* 当内容高度大于图片高度时,背景图像的位置相对于viewport固定 */
background-attachment: fixed;
/* 让背景图基于容器大小伸缩 */
background-size: cover;
/* 设置背景颜色,背景图加载过程中会显示背景色 */
background-color: #464646;
}

Guess you like

Origin blog.csdn.net/Maxueyingying/article/details/134261902