在CSS3中,设置背景图的方式有多种方式,可以通过以下两个属性进行设置:
background-image: url("图片地址");
background-size: cover; /*或 contain*/ 其中,background-image用于指定图片地址,可以是绝对路径或相对路径。
而background-size用于指定图片的大小。其中,cover表示图片会被拉伸以覆盖整个背景区域;contain表示图片会被缩小或放大以适应背景区域。
如果想要设置背景图的重复方式,可以使用background-repeat属性,例如:
background-repeat: no-repeat; /*不重复*/
background-repeat: repeat-x; /*水平方向重复*/
background-repeat: repeat-y; /*竖直方向重复*/
background-repeat: repeat; /*水平和竖直方向重复*/ 如果只想在特定位置显示背景图,可以使用background-position属性,例如:
background-position: left top; /*背景图在左上角显示*/
background-position: center bottom; /*背景图在底部居中显示*/
background-position: 50% 50%; /*背景图在中心点显示*/
background-position: -10px -10px; /*背景图在左上角偏移10像素*/ 除此之外,CSS3还提供了其他一些更高级的背景特效,如渐变、图片合成等,可以根据需要进行调用。