html - 使用媒体查询获得完美图像响应的正确方法是什么?

标签 html css twitter-bootstrap media-queries

我正在使用 Bootstrap,我有一个看起来像这样的 div:

<div class="header-back one"></div>

然后我有如下所示的 @media 查询:

@media (min-width: 320px) {
  .landing-page .header-back.one {
    background: asset-url('landing/320x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }    
}

@media(min-width: 375px) {
  .landing-page .header-back.one {
    background: asset-url('landing/375x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

@media(min-width: 425px) {
  .landing-page .header-back.one {
    background: asset-url('landing/425x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

@media(min-width: 768px) {
  .landing-page .header-back.one {
    background: asset-url('landing/768x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

@media(min-width: 1024px) {
  .landing-page .header-back.one {
    background: asset-url('landing/1024x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

@media(min-width: 1440px) {
  .landing-page .header-back.one {
    background: asset-url('landing/1440x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

@media(min-width: 1441px) {
  .landing-page .header-back.one {
    background: asset-url('landing/2560x500-View-Anywhere.jpg') 100% 0 no-repeat;
  }
}

问题是这不是无缝的。

例如,这是它在我的 13"Macbook Pro 上的样子:

enter image description here

注意灰色背景。那不应该在那里。整个中间 block 应该是蓝色背景。

div 的大小为 1200+ px,因此某些规则无法正常工作:

enter image description here

我是否正确使用了这些媒体查询?我什至需要这样做吗?我只想在这些断点处在所有设备上获得流畅的体验....我该如何实现?

编辑 1

按照 Tha'er 下面的建议重新调整样式后,这就是我所做的:

.landing-page .header-back.one {
  background: asset-url('landing/2560x500-View-Anywhere.jpg') 100% 0 no-repeat;
  // background-size: cover;
}

@media (max-width: 320px) {
  .landing-page .header-back.one {
    background: asset-url('landing/320x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }

}

@media(max-width: 375px) {
  .landing-page .header-back.one {
    background: asset-url('landing/375x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }    
}

@media(max-width: 425px) {
  .landing-page .header-back.one {
    background: asset-url('landing/425x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }
}

@media(max-width: 768px) {
  .landing-page .header-back.one {
    background: asset-url('landing/768x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }
}

@media(max-width: 1024px) {
  .landing-page .header-back.one {
    background: asset-url('landing/1024x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }
}

@media(max-width: 1440px) {
  .landing-page .header-back.one {
    background: asset-url('landing/1440x500-View-Anywhere.jpg') 100% 0 no-repeat;
    // background-size: cover;
  }
}

这是生成的结果,它是压缩到最小视口(viewport)中的最大图像(这不是我想要的):

enter image description here

最佳答案

实现您想要的效果的一种方法是使用图像作为 div 背景,而不是在 div 中使用它。在 div 中添加图像并不总是会在所有尺寸下都产生良好的效果,使用 div 背景图像方法在所有屏幕尺寸下都会产生良好的效果。

.div { background-image: url(image.jpg);

背景位置:中心中心; 背景大小:覆盖;

这应该会在所有屏幕尺寸上给您带来良好的效果,我猜这就是您想要实现的目标。

关于html - 使用媒体查询获得完美图像响应的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403124/

相关文章:

javascript - 单个 html 文件中的多个内容 'pages'

html - CSS :first-child and :first-of-type (example)

html - 如何保持相同大小的 Bootstrap 列以便它们正确捕捉?

html - 在 html 标签中插入 React 变量作为属性

javascript - 如何更改我的 js 代码以使菜单正常工作

javascript - 如何检查输入值是否等于以下任何一项然后选中复选框

html - 为什么3个span4 bootstrap div将第三个推到一个新行

html - Bootstrap 表单输入框未对齐

javascript - 使用输入 javascript 动态添加和删除列表组

javascript - 单击后不会立即加载预加载器