CSS背景位置不适用于移动设备

时间:2016-04-08 03:41:58

标签: html css responsive-design background-image background-position

我在正确定位背景图像时遇到问题。它适用于桌面,即使我调整窗口大小以模拟正确显示的手机,但当我实际访问手机上的网站(在Android / Chrome和iOS / Safari上测试)时,似乎忽略了背景位置属性。

有问题的CSS:

.blur {
        background: 
    /* top, transparent blue, faked with gradient */ 
    linear-gradient(
      rgba(43, 43, 43, 0.1), 
        rgba(43, 43, 43, 0.1)
    ),
    /* bottom, image */
    /* BACKGROUND IMAGE */
    url('https://suzannecowan.ca/wp-content/uploads/2016/04/2015-10-20-macleans-2-1.jpg');
  background-attachment: fixed;
  background-position: -350px -150px;
  background-position-x: -350px;
  background-position-y: -150px;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: #2b2b2b;
  overflow: auto;
}

@media (min-width: 40em) {

.blur {
      background: 
    /* top, transparent blue, faked with gradient */ 
    linear-gradient(
      rgba(43, 43, 43, 0.1), 
      rgba(43, 43, 43, 0.1)
    ),
    /* bottom, image */
    /* BACKGROUND IMAGE */
    url('https://suzannecowan.ca/wp-content/uploads/2016/04/2015-10-20-macleans-2-1.jpg');
  background-attachment: fixed;
  background-position: -350px 0; 
  background-size: cover;
  background-repeat: none;
  }

@media (min-width: 50em) {

  .blur {
  background: 
      /* top, transparent blue, faked with gradient */ 
      linear-gradient(to right,
        rgba(43, 43, 43, 0.1), 
        rgba(43, 43, 43, 0.1)
      ),
      /* bottom, image */
      /* BACKGROUND IMAGE */
      url('https://suzannecowan.ca/wp-content/uploads/2016/04/2015-10-20-macleans-2-1.jpg');
    background-attachment: fixed;
    background-position: -250px 0; 
    background-size: cover;
    background-repeat: none;
    }

我搜索了解决方案并找到了两条过去曾帮助过人们的建议:添加background-position-xbackground-position-y但没有帮助,并将背景图片移出{{1}并且进入另一个容器(在这种情况下为body),这也没有帮助。

该网站为:https://suzannecowan.ca/

提前感谢您提供的任何建议。

1 个答案:

答案 0 :(得分:2)

将您的bg图像设置为cover意味着它会想要拉伸到屏幕的高度,这意味着在一个小设备上,您将获得非常狭窄的视图。就个人而言,我要么将图像调整为更高更薄,以便在移动屏幕上更好地适应,或者将背景大小设置为100% auto,这看起来相当不错。