提问者:小点点

通过css显示时将图像居中


我试图使用仅css居中图像,当图像显示仅使用css。

@media(max-width: 768px){
  .page-id-28 {
    background-image: url(http://4309.co.uk/wp-content/uploads/2019/10/IMG_2019107_1853-1.jpg) !important;
    background-size: 890px !important;
  }
}

尝试使用URL(foo)中心;

不管用。


共2个答案

匿名用户

background-position可以是center center50%50%(即水平/垂直的值不同是可能的)。 您还需要no-repeat。 在下面的代码片段中,我使用了一个较小的图像大小,以便将其放入此处可用的空间中:

null

body {
  background-image: url(https://placehold.it/400x180/fb8);
  background-size: 400px;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

html,
body {
  margin: 0;
  height: 100%;
}

匿名用户

使用

null

@media(max-width: 768px){
  .page-id-28 {
    background-image: url(http://4309.co.uk/wp-content/uploads/2019/10/IMG_2019107_1853-1.jpg) !important;
    background-size: 890px !important;
    background-position: center;
  }
}