CSS 在 Web 开发中处理多个屏幕分辨率和纵横比的策略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3130054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:18:23  来源:igfitidea点击:

Strategies for Handling Multiple Screen Resolutions and Aspect Ratios in Web Development

cssscreen-resolutionaspect-ratio

提问by Adrian K

Back in the day, 800 x 600 was the screen resolution to design for - and maybe 640 x 480. Then along came 1024 x 768, etc, etc, etc.

过去,800 x 600 是要设计的屏幕分辨率 - 也许是 640 x 480。然后是 1024 x 768 等等,等等。

But then it gets worse: now we have not only different resolutions but also different aspect ratios.

但后来情况变得更糟:现在我们不仅有不同的分辨率,还有不同的纵横比。

What strategies do people use to accommodate today's ever-expanding range of screen sizes and aspect ratios?

人们使用什么策略来适应当今不断扩大的屏幕尺寸和纵横比范围?

(BTW - I was only thinking about laptop / desktop hardware, but of course there's smart-phones and tablets to consider too.)

(顺便说一句 - 我只考虑笔记本电脑/台式机硬件,但当然也有智能手机和平板电脑要考虑。)

采纳答案by Yi Jiang

I know this would be a somewhat controversial opinion, but I'd say it anyway: Don't

我知道这会是一个有点有争议的观点,但我还是要说:不要

Don't design for multiple screen sizes or aspect ratios. There are of course a few exceptions: Heavy web applications like webmail clients can definitely do with more screen real estate, and are probably flexible enough to accommodate a large range of screen sizes anyway. Mobile versions of said website, with a more flexible design to accommodate the incredible spectrum of mobile screen sizes can help too for sites with high mobile volumes. However, if you stick to the so called 'desktop web', then I think we can say that 95% of the time there are more important things to care about than screen sizes, resolution and aspect ratio.

不要针对多种屏幕尺寸或纵横比进行设计。当然也有一些例外:像 webmail 客户端这样的重型 Web 应用程序肯定可以使用更多的屏幕空间,并且可能足够灵活以适应大范围的屏幕尺寸。该网站的移动版本具有更灵活的设计以适应令人难以置信的移动屏幕尺寸范围,这对于移动量大的网站也有帮助。然而,如果你坚持所谓的“桌面网络”,那么我认为我们可以说 95% 的时间有比屏幕尺寸、分辨率和纵横比更重要的事情需要关心。

First off, lets tackle the easy one. I don't really understand why you would care so much for aspect ratio - it isn't like we care thatmuch for the 'below the fold' nonsense anymore, do we? The web is a vertical medium - scrolling will always have a place in websites. Having everything above the magical 600px line is just stupid.

首先,让我们解决简单的问题。我真的不明白,你为什么会那么关心纵横比-它并不像我们所关心的是多的废话“的首屏”了,我们呢?网络是一种垂直媒体——滚动在网站中总是占有一席之地。把所有东西都放在神奇的 600px 线以上是愚蠢的。

Next, screen resolution/size: Again, I find it difficult to defend.

其次,屏幕分辨率/尺寸:再次,我发现很难防守。

Users with large screens do not usually maximize their browser windows, because they find that most website do not take advantage of them. While the web adjust to the user, the user also adjust to the web. Although you could argue that this is a chicken and egg problem, the fact remains that website are usually designed for the lowest common denominator.I'm not defending this position, but rather, pointing it out as the current prevailing trend in the industry.

大屏幕用户通常不会最大化他们的浏览器窗口,因为他们发现大多数网站都没有利用它们。在网络适应用户的同时,用户也适应了网络。尽管您可能会争辩说这是一个先有鸡还是先有蛋的问题,但事实仍然是网站通常是为最小公分母设计的。我不是在为这个立场辩护,而是指出它是当前行业的流行趋势。

There are certain things that simply won't work with resolution that are too high or too low. There is, for example, a small range of widths that allow people to read comfortably on screen. Any longer and the amount of movement for the eye to the next line would be annoying. Too low and the text would appear cramped. The fact that the web was designed to be resolution neutral means that paradoxically not many provisions has been made for those who wish to build fluid layouts. min-heightand max-heightwould help, of course, but the wider the range, the more difficulties you will face. Things like orphaned elements, displaced images, backgrounds that run out, etc. are unavoidable for truly flexible sites built with today's technology.

有些东西在分辨率太高或太低的情况下根本不起作用。例如,有一小范围的宽度可以让人们在屏幕上舒适地阅读。再久一点,眼睛到下一行的移动量就会很烦人。太低,文本会显得局促。Web 被设计为分辨率中立的事实意味着,矛盾的是,对于那些希望构建流畅布局的人来说,没有多少规定。min-height并且max-height当然会有所帮助,但是范围越广,您面临的困难就越多。对于使用当今技术构建的真正灵活的站点而言,诸如孤立元素、移位图像、用完的背景等是不可避免的。

So my opinion is that the simplest method for dealing with multiple resolutions is to ignore it altogether - with today's technology there are not many options anyway - and design for the lowest common denominator.

所以我的观点是,处理多个分辨率的最简单方法是完全忽略它——以今天的技术,无论如何选择并不多——并为最小公分母进行设计。

回答by NakedBrunch

Watch out for high DPI settings

注意高 DPI 设置

I think one of the most undiscussed issues facing front-end web development today is testing on high DPI systems. Everyone has learned to test and test and test on different browsers but designers/developers have not caught on to testing on different DPI settings.

我认为当今前端 Web 开发面临的最未讨论的问题之一是在高 DPI 系统上进行测试。每个人都学会了在不同的浏览器上进行测试、测试和测试,但设计人员/开发人员还没有掌握在不同 DPI 设置上进行测试的能力。

High (or even low, for that matter) DPI settings break designs when fonts are scaled but images are not (which can happen), can cause images to look fuzzy/blurry, and absolutely positioned objects may not appear in the desired location (which would be devastating for CSS menus.) If nothing else, test your images at high DPI and re-render them as necessary.

高(甚至低,就此而言)DPI 设置在字体缩放但图像未缩放(可能发生)时破坏设计,可能导致图像看起来模糊/模糊,并且绝对定位的对象可能不会出现在所需位置(这可能会发生)对于 CSS 菜单来说将是毁灭性的。)如果不出意外,请在高 DPI 下测试您的图像,并根据需要重新渲染它们。

This has never really been an issue until recently with the release of Windows 7 and people buying computers with high resolution monitors. First off, Windows 7 uses 96DPI as default (which is different from the rest of the computing world that has been using 72DPI as the standard. Moreover, Windows 7 will automatically adjust DPI settings and I have seen people with DPI of 150% of normal (96 DPI in Windows).

直到最近,随着 Windows 7 的发布和人们购买配备高分辨率显示器的计算机,这才真正成为一个问题。首先,Windows 7 默认使用 96DPI(这与其他计算世界一直使用 72DPI 作为标准不同。而且,Windows 7 会自动调整 DPI 设置,我见过 DPI 为正常 150% 的人(Windows 中为 96 DPI)。

Here's a great link discussing this issue in more details: http://webkit.org/blog/55/high-dpi-web-sites/

这是更详细地讨论这个问题的一个很好的链接:http: //webkit.org/blog/55/high-dpi-web-sites/

A great cross-browser website designed with web standards is the goal but don't forget about DPI testing.

目标是使用 Web 标准设计的出色的跨浏览器网站,但不要忘记 DPI 测试。

回答by Stefano

Well, trying to keep the answer not-too-long, this is what I do.

好吧,尽量让答案不要太长,这就是我所做的。

(A) Always start from the most likely used ratio/resolution

(A) 总是从最可能使用的比率/分辨率开始

If your average joe is going to be on a modern laptop or a desktop machine he likely has AT LEAST 1024x768 (refs: w3schoolselykinnovation), that gives you roughly a usable 960px width (you might want to check the 960grid system- there are a hell lot of new framework since I first wrote this). If you users are more likely to start with a mobile device or a tablet, thing about them first. If it's 50%-50%, it's usually better to start from small and then grow up, eg. Rock Hammeror Foundation

如果您的普通乔将使用现代笔记本电脑或台式机,他可能至少拥有 1024x768(参考:w3schools elykinnovation),这为您提供了大约960 像素的可用宽度(您可能需要检查960grid 系统- 有一个自从我第一次写这篇文章以来,有很多新框架)。如果您的用户更有可能从移动设备或平板电脑开始,请先了解它们。如果是50%-50%,通常最好从小处开始,然后长大,例如。岩锤基础

(B) Layout: fluid or not?

(B) 布局:流畅与否?

If your website could benefit from a larger width, pick a fluid design starting from this resolution. Be careful that the human eye does not like to read text over long lines, so do not abuse of fluid design; often sticking to 960px with large margins is acceptable. You might want to add (javascript) some additional side-menus if you really have a lot more space. But design your website to work without JS as much as possible.

如果您的网站可以从更大的宽度中受益,请从该分辨率开始选择流畅的设计。注意人眼不喜欢阅读长行文字,所以不要滥用流体设计;经常坚持 960px 大边距是可以接受的。如果您真的有更多空间,您可能想要添加 (javascript) 一些额外的侧菜单。但是尽可能将您的网站设计为在没有 JS 的情况下工作。

(C) Other resolutions

(C) 其他决议

Finally it's time to check that with least used resolutions things are still acceptable.

最后是时候检查使用最少的分辨率的事情仍然可以接受。

(D) Other devices, ratios and stuff

(D) 其他设备、比率和东西

There are not many options for different ratios; it often means you are running on a mobile, ipad, AAA or similar device.

不同比例的选择不多;这通常意味着您在移动设备、ipad、AAA 或类似设备上运行。

My advice is to ... design for those devices specifically.

我的建议是……专门为这些设备设计。

While writing your HTML keep in mind what you are going to need and remember to do HTML by semantic and not for design. Use properly HTML5 semantic tags if you can. Avoid < bold > or similar tags, and properly use tags and classes that you are going to style with CSS instead.

在编写 HTML 时,请记住您将需要什么,并记住按语义而不是为了设计来编写 HTML。如果可以,请正确使用 HTML5 语义标签。避免使用 <bold> 或类似的标签,并正确使用您将使用 CSS 设置样式的标签和类。

Use a framework!

使用框架!

But you still can make a few different designs for very different devices. You don't have to do everything responsive /in the same design/.

但是您仍然可以为非常不同的设备进行一些不同的设计。您不必/在相同的设计中/做所有响应式的操作。

There are several ways to serve a different CSS depending on the client; you can do it:

有几种方法可以根据客户端提供不同的 CSS;你能行的:

  1. server side, checking the browser in the HTTP heading coming from the client, either with your web server or your dynamic scripting environment - be it python/django, php, or whatever else
  2. javascript (you can easily get the window size)
  3. html - in particular with some specific devices such as iphone
  1. 服务器端,检查来自客户端的 HTTP 标题中的浏览器,无论是使用您的 Web 服务器还是您的动态脚本环境 - 无论是 python/django、php 还是其他任何东西
  2. javascript(您可以轻松获取窗口大小)
  3. html - 特别是一些特定的设备,如 iphone

You can easily produce a generic design for small (eg. mobile) devices by following some simple rules: 1. fluid layout capable of fitting in very small widths 2. compact header/footers not too waste too much space 3. few, clear contents per 'page' 4. avoid :over effects as they won't work on touch devices!!!

通过遵循一些简单的规则,您可以轻松地为小型(例如移动)设备生成通用设计:1. 能够适应非常小的宽度的流体布局 2. 紧凑的页眉/页脚不会浪费太多空间 3. 内容少,清晰每个“页面” 4. 避免 :over 效果,因为它们在触摸设备上不起作用!!!

If you want to go further, you have to check individual devices customizations; an example is the iphone viewport, see the apple ref library.

如果你想更进一步,你必须检查单个设备的自定义;一个例子是 iphone 视口,请参阅 苹果参考库

This is just to get you started. Experience and specific needs will drive the rest!

这只是让你开始。经验和特定需求将推动其余部分!

回答by dave

Your site can't work perfectly for every display. Even if you had enough hours in the day (or should I say year/decade) to design for every possible display, you'd have to do it over every time a new device comes out.

您的网站无法在每个显示器上完美运行。即使您一天中有足够的时间(或者我应该说是一年/十年)来为每种可能的显示器进行设计,但每次新设备问世时,您都必须重新设计。

In my development, I still religiously try to avoid horizontal scrolling, and that isn't too hard with floating divs / variable-width divs. But beyond that, we really are at the "there's an app for that" cross-roads, where you need a specially designed display for specific devices.

在我的开发中,我仍然虔诚地尝试避免水平滚动,这对于浮动 div/可变宽度 div 来说并不太难。但除此之外,我们真的处于“有一个应用程序”的十字路口,您需要为特定设备专门设计的显示器。

One strategy I use is to reduce dependence on a single display -- a customer probably doesn't need to see your entire web page to do what they came to do. You can parse-out functionality to smaller/simpler web pages that scale better on differently-sized devices.

我使用的一种策略是减少对单个显示器的依赖——客户可能不需要看到你的整个网页来做他们想做的事情。您可以将功能解析为更小/更简单的网页,以便在不同大小的设备上更好地扩展。

At work, I have a little more "power", as it were -- I can develop internal web apps that are "designed to run on...some specific browser, some specific display setting, etc. -- use other configurations at your own risk". This, only after getting the managers to agree that spending an extra week in development (and even more in upgrades / future maintenance) just to placate that one vegetarian at the other end of campus who refuses to use IE really isn't worth the cost. In that case, we need another Timmy, not a more flexible web app that can look good on his favorite non-IE browser.

在工作中,我有更多的“权力”,就像它一样——我可以开发内部网络应用程序,这些应用程序“设计为在……某些特定的浏览器、某些特定的显示设置等上运行——使用其他配置在您自己的风险”。只有在让经理同意在开发上多花一周时间(甚至在升级/未来维护上花费更多时间)来安抚校园另一端拒绝使用 IE 的素食主义者之后,才真的不值得为此付出代价. 在这种情况下,我们需要另一个 Timmy,而不是一个更灵活的 Web 应用程序,它可以在他最喜欢的非 IE 浏览器上看起来不错。

回答by tathagata

This is a common but complex question, which unfortunately does not have a single best solution. It all depends on the kind of content that you have. You can use a fluid layout, or design your site differently for different resolutions (see http://www.maxdesign.com.au/articles/resolution/). For an example of a fluid design, check this out - http://hicksdesign.co.uk/journal/finally-a-fluid-hicksdesign

这是一个常见但复杂的问题,不幸的是,它没有单一的最佳解决方案。这完全取决于您拥有的内容类型。您可以使用流体布局,或针对不同的分辨率设计不同的网站(请参阅http://www.maxdesign.com.au/articles/resolution/)。有关流体设计的示例,请查看 - http://hicksdesign.co.uk/journal/finally-a-fluid-hicksdesign

回答by hjuster

I think that responsive web design is the answer to your question. Have a look at these examples and techniques...Responsive Web Design

我认为响应式网页设计是您问题的答案。看看这些例子和技术...响应式网页设计