105

这听起来像是一个愚蠢的问题。

如果我将此 CSS 片段用于常规显示(box-bg.png200 像素乘 200 像素在哪里);

.box{
    background:url('images/box-bg.png') no-repeat top left;
    width:200px;
    height:200px
}

如果我使用这样的媒体查询来定位视网膜显示器(@2x 图像是高分辨率版本);

@media (-webkit-min-device-pixel-ratio: 2), 
(min-resolution: 192dpi) { 

    .box{background:url('images/box-bg@2x.png') no-repeat top left;}
}

我是否需要将.boxdiv 的大小加倍为 400px x 400px 以匹配新的高分辨率背景图像?

4

4 回答 4

192

我是否需要将 .box div 的大小加倍为 400 像素乘 400 像素以匹配新的高分辨率背景图像

否,但您确实需要设置background-size属性以匹配原始尺寸:

@media (-webkit-min-device-pixel-ratio: 2), 
(min-resolution: 192dpi) { 

    .box{
        background:url('images/box-bg@2x.png') no-repeat top left;
        background-size: 200px 200px;
    }
}

编辑

为了给这个答案添加更多内容,这是我倾向于使用的视网膜检测查询:

@media
only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and (   min--moz-device-pixel-ratio: 2),
only screen and (     -o-min-device-pixel-ratio: 2/1),
only screen and (        min-device-pixel-ratio: 2),
only screen and (                min-resolution: 192dpi),
only screen and (                min-resolution: 2dppx) { 

}

- 资源

注意。这min--moz-device-pixel-ratio:不是一个错字。这是 Firefox 某些版本中的一个有据可查的错误,应该这样编写以支持旧版本(Firefox 16 之前)。 - 资源


正如@LiamNewmarch 在下面的评论中提到的,您可以像这样background-size在您的速记background声明中包含:

.box{
    background:url('images/box-bg@2x.png') no-repeat top left / 200px 200px;
}

但是,我个人不建议使用速记形式,因为 iOS <= 6 或 Android 不支持它,因此在大多数情况下都不可靠。

于 2013-04-22T19:04:24.260 回答
16

这是一个解决方案,它还包括 High(er)DPI ( MDPI ) 设备> ~160 dots per inch,例如很多非 iOS 设备(fe: Google Nexus 7 2012):

.box {
    background: url( 'img/box-bg.png' ) no-repeat top left;
    width: 200px;
    height: 200px;
}
@media only screen and ( -webkit-min-device-pixel-ratio: 1.3 ),
       only screen and (    min--moz-device-pixel-ratio: 1.3 ),
       only screen and (      -o-min-device-pixel-ratio: 2.6/2 ), /* returns 1.3, see Dev.Opera */
       only screen and (         min-device-pixel-ratio: 1.3 ),
       only screen and ( min-resolution: 124.8dpi ),
       only screen and ( min-resolution: 1.3dppx ) {

       .box {
           background: url( 'img/box-bg@2x.png' ) no-repeat top left / 200px 200px;
       }

}

正如@3rror404 在收到评论反馈后在他的编辑中包含的那样,Webkit/iPhone 之外还有一个世界。到目前为止,大多数解决方案都困扰着我的一件事,例如上面在CSS-Tricks中引用的源代码,那就是没有完全考虑到这一点。
原始来源已经走得更远了。

例如,Nexus 7 (2012) 屏幕是一个TVDPI 屏幕,带有一个奇怪device-pixel-ratio1.325. 当以正常分辨率加载图像时,它们通过插值放大,因此变得模糊。对我来说,在媒体查询中应用此规则以包括那些成功获得最佳客户反馈的设备。

于 2014-07-08T18:31:48.703 回答
3

如果您打算对视网膜和非视网膜屏幕使用相同的图像,那么这里就是解决方案。假设您有一个图像,200x200并且在顶行有两个图标,在底行有两个图标。所以,它是四个象限。

.sprite-of-icons {
  background: url("../images/icons-in-four-quad-of-200by200.png") no-repeat;
  background-size: 100px 100px /* Scale it down to 50% rather using 200x200 */
}

.sp-logo-1 { background-position: 0 0; }

/* Reduce positioning of the icons down to 50% rather using -50px */
.sp-logo-2 { background-position: -25px 0 }
.sp-logo-3 { background-position: 0 -25px }
.sp-logo-3 { background-position: -25px -25px }

将精灵图标缩放和定位到实际值的 50%,可以得到预期的结果。


Ryan Benhase的另一个方便的 SCSS mixin 解决方案。

/****************************
 HIGH PPI DISPLAY BACKGROUNDS
*****************************/

@mixin background-2x($path, $ext: "png", $w: auto, $h: auto, $pos: left top, $repeat: no-repeat) {

  $at1x_path: "#{$path}.#{$ext}";
  $at2x_path: "#{$path}@2x.#{$ext}";

  background-image: url("#{$at1x_path}");
  background-size: $w $h;
  background-position: $pos;
  background-repeat: $repeat;

  @media all and (-webkit-min-device-pixel-ratio : 1.5),
  all and (-o-min-device-pixel-ratio: 3/2),
  all and (min--moz-device-pixel-ratio: 1.5),
  all and (min-device-pixel-ratio: 1.5) {
    background-image: url("#{$at2x_path}"); 
  }
}

div.background {
  @include background-2x( 'path/to/image', 'jpg', 100px, 100px, center center, repeat-x );
}

有关上述 mixin 的更多信息,请阅读此处

于 2016-12-01T06:30:14.527 回答
2

您可以使用图像集()。此 CSS 功能目前支持有限,但确实有效。MND web docs 上有关它的更多信息,但这里有一个它是如何工作的示例:

.box {
  background-image: -webkit-image-set(
    url('images/box-bg.png') 1x,
    url('images/box-bg@2x.png') 2x); /* Temporary fallback for Chrome and Safari browsers until they support 'image-set()' better */
  background-image: image-set(
    url('images/box-bg.png') 1x,
    url('images/box-bg@2x.png') 2x);
}

“1x”和“2x”是这里的分辨率。'2x' 用于视网膜显示器。

于 2021-10-22T15:15:59.157 回答