我应该在响应式网页设计中使用什么媒体类型?

发布于 2025-01-02 10:52:27 字数 160 浏览 0 评论 0原文

我看到一些在媒体查询中使用“仅屏幕”,其他人使用“全部”,其他人使用“屏幕和打印”,其他人使用“屏幕”(没有“仅”),其他人没有指定媒体类型。

我不打算将特定的 CSS 用于打印或其他媒体。我应该使用“仅屏幕”吗?我不应该指定媒体类型吗?

大多数人使用什么媒体类型?为什么?

I see some using "only screen" in the media queries, others using "all", others using "screen and print", others using "screen"(without the "only"), others don't specify the media type.

I don't intend to use specific CSS for printing or other medias. Should I use "only screen". Should I not specify the media type?

What media type most people are using and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2025-01-09 10:52:27

基本上:

  • 要么使用 media="screen" 将主样式表应用到所有浏览器,或者如果您不关心,则完全省略 media 属性print

  • 使用 media="print" 来应用您的如果您确实关心打印,请打印样式表

  • 如果您愿意,请包含 only 关键字仅适用于媒体查询,例如 screen 和 (max-width: 1000px) 适用于您的响应式样式(此处没有任何正确、错误或标准可供遵循)

only 关键字是引入主要是为了阻止旧浏览器应用适用于其他设备的样式表,例如智能手机和平板电脑上的现代浏览器。请参阅媒体查询规范

不要使用media="only screen"作为您的主样式表。如果这样做,IE8 及更早版本将完全忽略您的主样式表,并且您的网站在这些版本中将显示为无样式。


一些背景知识:HTML 4 规范要求媒体“类型“(或媒体描述符)如下所示:

<link rel="stylesheet" media="screen and (max-width: 1000px)" href="resp.css">

应该在忽略 and ... 部分的情况下进行解析,因此它相当于:这

<link rel="stylesheet" media="screen" href="resp.css">

意味着它将适用于较旧的浏览器不支持 CSS3 媒体查询,但确实完全支持CSS2媒体类型。这可能会导致不需要的副作用,例如,在较旧的桌面浏览器中应用移动样式表。

然而,根据我的经验,这种情况从未发生过。据我所知,IE7 和 IE8 只是将 screen 和 (max-width: 1000px) 视为无效的媒体描述符,并完全忽略该样式表。

但为了安全起见,我将 only 关键字放入专门供现代浏览器使用的媒体查询中。

当然,这个规则在 HTML5 中已经改变,以便与 CSS3 中的媒体查询兼容。它只是不适用于 HTML5 工作开始之前发布的旧版浏览器。

Basically:

  • Either use media="screen" for applying your main stylesheet to all browsers, or just leave out the media attribute altogether if you don't care about print

  • Use media="print" for applying your print stylesheet if you do care about print

  • If you'd like, include the only keyword only for media queries like screen and (max-width: 1000px) for your responsive styles (there isn't any right, wrong or standard to follow here)

The only keyword was introduced mainly to stop older browsers from applying stylesheets intended for other devices, like modern browsers on smartphones and tablet computers. See the Media Queries spec.

Do not use media="only screen" for your main stylesheet. If you do, IE8 and older will completely ignore your main stylesheet, and your site will appear unstyled in those versions.


For some background: the HTML 4 spec asks that media "types" (or media descriptors) like this:

<link rel="stylesheet" media="screen and (max-width: 1000px)" href="resp.css">

Should be parsed with the and ... part ignored, so it would be equivalent to this:

<link rel="stylesheet" media="screen" href="resp.css">

Meaning it would apply in older browsers that don't support CSS3 media queries, but do fully support CSS2 media types. This may cause unwanted side effects, e.g. a mobile stylesheet being applied in older desktop browsers.

In my experience, however, this has never happened; as far as I'm aware, IE7 and IE8 simply treat screen and (max-width: 1000px) as an invalid media descriptor and ignore that stylesheet altogether.

But I like to be on the safe side, and put the only keyword in media queries intended specifically for use by modern browsers.

Of course, this rule has been changed in HTML5 in order to be compatible with media queries in CSS3. It just won't apply to older browsers that were released before work on HTML5 began.

戏蝶舞 2025-01-09 10:52:27

IMO 将所有样式放入单个样式表中会更容易且性能更好:

<link rel=stylesheet href="style.css">

然后您可以在向上时覆盖:

/* put mobile-friendly first (before media queries) */ 

@media screen and (min-width:481px) {
    /* ... */
}

@media screen and (min-width:961px) {
    /* ... */
}

@media print {
    /* print-specific overrides */ 
}

签出 H5BP320 及以上

您可以使用屏幕全部。如果您要添加仅适用于屏幕的背景图像之类的内容,那么使用 screen 绝对有意义。对于真正的基本东西来说,这并不重要。

IMO it's easier and better performance to put all the styles into a single stylesheet:

<link rel=stylesheet href="style.css">

Then you can override as you go up:

/* put mobile-friendly first (before media queries) */ 

@media screen and (min-width:481px) {
    /* ... */
}

@media screen and (min-width:961px) {
    /* ... */
}

@media print {
    /* print-specific overrides */ 
}

Checkout H5BP and 320 and up.

You can use screen or all. If you're adding things like background images that only apply to screens, then it definitely makes sense to use screen. For real basic stuff it doesn't matter much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文