【问题标题】:CSS media queries for screen sizes屏幕尺寸的 CSS 媒体查询
【发布时间】:2012-11-30 15:05:58
【问题描述】:

我目前正在尝试设计一种与多种屏幕尺寸兼容的布局。我设计的屏幕尺寸如下:

屏幕尺寸:

  1. 640x480
  2. 800x600
  3. 1024x768
  4. 1280x1024(及更大)

我遇到的问题是创建 css3 媒体查询,这样当窗口的宽度达到这些宽度之一时,我的布局就会发生变化。下面是我目前正在使用的媒体查询示例,但它对我不起作用,所以我想知道是否有人可以帮助我解决它。

<link rel="stylesheet" type="text/css" media="screen and (max-width: 700px)" href="css/devices/screen/layout-modify1.css">
<link rel="stylesheet" type="text/css" media="screen and (min-width: 701px) and (max-width: 900px)" href="css/devices/screen/layout-modify2.css">
<link rel="stylesheet" type="text/css" media="screen and (max-width: 901px)" href="css/devices/screen/layout-modify3.css">

我尝试使用一组新的媒体查询,但它们仍然不适合我。有人可以帮助解释我错了什么。你们中的一些人已经尝试过解释,但我不明白。新的媒体查询如下所示:

<link rel="stylesheet" type="text/css" media="screen and (max-width: 640px)" href="css/devices/screen/layout-modify1.css">

    <link rel="stylesheet" type="text/css" media="screen and (max-width: 800px)" href="css/devices/screen/layout-modify2.css">

    <link rel="stylesheet" type="text/css" media="screen and (max-width: 1024px)" href="css/devices/screen/layout-modify3.css">

    <link rel="stylesheet" type="text/css" media="screen and (max-width: 1280px)" href="css/devices/screen/layout-modify4.css">

【问题讨论】:

  • 可以改用javascript吗?
  • @Titanium 媒体查询不是静态的。调整浏览器的大小将根据浏览器现在满足的条件重新设置元素的样式。
  • @JaPerk14 您能否更具体地说明什么不起作用?你知道一个 640px 宽的设备会加载所有 4 个样式表,对吧? (因为所有这些表达式都是真的:640
  • 好的,我会尽量说得更具体一些。例如,假设浏览器宽度为 640,则不应应用 800、1024 和 1280 大小的媒体查询的样式。我想你可能已经解决了我的问题,西马农。您可以更改我的新媒体查询,以便它们在浏览器宽度更改为特定宽度时正常工作并更改样式。
  • 这与“响应式”设计相反,您的工作量增加了 4 倍,因为您需要维护四种设计。您应该为所有内容采用一种“流体响应式”设计,然后仅使用媒体查询来解决特定问题。例如,如果侧边栏没有足够的空间,这不是特定于设备的问题,这只是意味着您不希望您的内容超出某个限制,并且该限制不应该由设备决定,而是它应该由内容决定。默认情况下,HTML 是独立于设备的——你将朝着依赖于设备的方向前进。

标签: html css media-queries


【解决方案1】:

将所有内容放在一个文档中并使用它:

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
  /* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
  /* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
  /* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
  /* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
  /* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
  /* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
  /* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
  /* Styles */
}

/* iPhone 4 - 5s ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
  /* Styles */
}

/* iPhone 6 ----------- */
@media
only screen and (max-device-width: 667px) 
only screen and (-webkit-device-pixel-ratio: 2) {
  /* Styles */
}

/* iPhone 6+ ----------- */
@media
only screen and (min-device-width : 414px) 
only screen and (-webkit-device-pixel-ratio: 3) {
  /*** You've spent way too much on a phone ***/
}

/* Samsung Galaxy S7 Edge ----------- */
@media only screen
and (-webkit-min-device-pixel-ratio: 3),
and (min-resolution: 192dpi)and (max-width:640px) {
 /* Styles */
}

来源:http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

此时,我肯定会考虑使用em 值而不是像素。欲了解更多信息,请查看此帖子:https://zellwk.com/blog/media-query-units/

【讨论】:

  • 虽然您的建议没有问题,但它并没有回答 OP 的问题。
  • 您的最后一个查询应该是Edge 7s 而不是Galaxy S7s
【解决方案2】:

除非你有更多的样式表,否则你已经搞砸了你的断点:

#1 (max-width: 700px)
#2 (min-width: 701px) and (max-width: 900px)
#3 (max-width: 901px)

第三个媒体查询可能是min-width: 901px。目前,它与#1 和#2 重叠,并且仅在屏幕恰好为 901px 宽时才自行控制页面布局。

编辑更新的问题:

(max-width: 640px)
(max-width: 800px)
(max-width: 1024px)
(max-width: 1280px)

媒体查询不像 catch 或 if/else 语句。如果任何条件匹配,那么它将应用它匹配的每个媒体查询的所有样式。如果您只为所有媒体查询指定min-width,则可能匹配部分或全部媒体查询。在您的情况下,一个 640px 宽的设备匹配您的所有 4 个媒体查询,因此所有样式表都已加载。您最有可能寻找的是:

(max-width: 640px)
(min-width: 641px) and (max-width: 800px)
(min-width: 801px) and (max-width: 1024px)
(min-width: 1025px)

现在没有重叠。仅当设备的宽度在指定宽度之间时才会应用这些样式。

【讨论】:

  • 您可能应该在响应的代码中反映这一点。我只是写了一个完整的答案,然后重新阅读你发布的内容,发现你已经通过将最大值切换为最小值来回答它。
  • #3 (max-width: 901px) 一直控制页面,因为它是最后一个定义并覆盖之前设置的所有声明。
  • 很好的解释:)
【解决方案3】:

对于所有智能手机和大屏幕使用这种媒体查询格式

/* Smartphones (portrait and landscape) ----------- */

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */

@media only screen and (min-width : 321px) {
/* Styles */
}



/* Smartphones (portrait) ----------- */

@media only screen and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */
}

/**********
iPad 3
**********/

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

/* Desktops and laptops ----------- */

@media only screen  and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen  and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

/* iPhone 5 ----------- */

@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6 ----------- */

@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6+ ----------- */

@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S3 ----------- */

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S4 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

/* Samsung Galaxy S5 ----------- */

@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2018-06-14
    • 2019-12-10
    • 1970-01-01
    • 2020-04-26
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多