在 CSS 中,我们可以使用以下的代码将背景图片居中:
background-position: center;
background-size: cover;
background-repeat: no-repeat;
其中,background-position
属性可以设置背景图片在元素中的位置,而 center
值可以将背景图片水平和垂直方向都居中显示。
同时,使用 background-size: cover;
属性可以让背景图片完整地覆盖整个元素,而不会被压缩或拉伸。
最后,background-repeat: no-repeat;
属性可以防止背景图片的重复。