[CSS]设置背景图片居中的方法

2021-12-14 779点热度 0人点赞 0条评论

默认的CSS设置背景图,我们是这样写的:

background: url("images/bg.png") no-repeat;

此时背景图片no-repeat表示不重复,位置会默认在div的左上角。

如果想要让背景图居中的话,可以这样写:

background: url("images/bg.png") center center no-repeat;

第一个center表示水平居中,第二个center表示垂直居中。

所以如果想要让背景图水平居中,但是垂直方向上靠上,可以:

background: url("images/bg.png") center top no-repeat;

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论