
大家好,又见面了,我是你们的朋友全栈君。
由于给网页设置背景图时,需要设置背景图不重复且充满整个浏览器屏幕。
    给body标签指定背景图,这样背景图就可以填充整个浏览器viewport了。     其实,该方案对所有的块级容器都可以生效。块级容器的宽高是动态的,那么背景图将自动伸缩,充满整个容器。   可设置body标签的CSS样式如下:   body{    /*加载背景图*/     background-image:url(./images/background.jpg);     /* 背景图垂直、水平均居中 */
    background-position: center center;
    /* 背景图不平铺 */
    background-repeat: no-repeat;
    /* 当内容高度大于图片高度时,背景图像的位置相对于viewport固定 */
    background-attachment: fixed;  //此条属性必须设置否则可能无效/
    /* 让背景图基于容器大小伸缩 */
   background-size: cover;
   /* 设置背景颜色,背景图加载过程中会显示背景色 */
   background-color: #CCCCCC;
}
或简写为如下CSS样式:
  body{
  
  
   background:url(./images/background.jpg)  no-repeat center center;
   background-size:cover;
   background-attachment:fixed;
   background-color:#CCCCCC;
}
  
                                                        发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/134203.html原文链接:https://javaforall.cn