背景属性的简写

Author Avatar
Ninefire 6月 06, 2018
  • 在其它设备中阅读本文章

如有以下样式:

body{
    height: 5000px;
    /* 设置背景颜色 */
    background-color: #bfa;
    /* 设置背景图片 */
    background-image: url(bg.png);
    /* 设置背景图片不重复 */
    background-repeat: no-repeat;
    /* 设置背景图片位置 */
    background-repeat: center;
    /* 设置背景图片不随滚动条滚动 */
    background-attachment: fixed;
}

这样设置背景没有任何问题,但是会显得有些麻烦和复杂,因此我们可以使用背景属性的简写。

  • 使用background来同时设置所有背景相关的样式
    • 无顺序要求,样式编写的顺序不影响效果
    • 无数量要求,没有设置的样式则使用默认值

因此以上样式可以简写为:

body{
    height: 5000px;
    background: #bfa url(bg.png) no-repeat center fixed;
}

如发现错误请联系我,谢谢你。
本文链接:http://ninefire.tk/HTML&CSS.basics/59.html