在 CSS 中使用像素是不是很糟糕?

发布于 2024-09-02 17:06:45 字数 65 浏览 5 评论 0原文

在 CSS 中使用像素数而不是百分比在兼容性方面是否会很差?低分辨率怎么样?可以在 1-100 的范围内使用它们吗?

Is it bad in terms of compatibility to use pixel numbers in CSS instead of percentages? How about lower resolutions? Is it okay to work with them in ranges of 1-100?

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

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

发布评论

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

评论(13

小傻瓜 2024-09-09 17:06:46

所有测量都有自己的目的:

  • 将像素用于基于像素的事物,例如边框。您可能不希望最终宽度为 1.3422 像素的边框。

  • 对基于文本的事物使用以文本为中心的度量(em、ex),例如内容区域、标签和输入框。这是确保有一定长度和宽度的文本空间的简单方法。

  • 对基于窗口的事物使用百分比,例如列。

当然也有例外。例如,您可能想要指定最小列宽(以像素为单位)。但按照上述操作,您的页面将能够很好地扩展。始终放大和缩小页面,看看它们如何在不同的字体大小和浏览器形状下工作——以后不要感到惊讶。

All of the measurements have their own purposes:

  • Use pixels for pixel-based things, like borders. You probably don't want a border that ends up being 1.3422 pixels wide.

  • Use text-centric measures (em, ex) for text-based things, like content areas, labels, and input boxes. It's an easy way to make sure you have room for text of a certain length and width.

  • Use percents for window-based things, like columns.

There are exceptions, of course. For example, you might want to specify a minimum column width in pixels. But follow the above and your pages will scale well. ALWAYS zoom in and out on your pages to see how they work with different font sizes and browser shapes -- don't get surprised later.

智商已欠费 2024-09-09 17:06:46

这是一个很难的问题,因为答案主要取决于您的情况。

像素还不错,我也主要使用它们。 (有时甚至是字体大小。)
我通常按​​给定大小(固定宽度布局的像素和流体布局的百分比)修复布局的外部块元素,并且在内部元素上我通常尽可能设置百分比。

有些元素根本无法使用百分比或 em 来设计样式,尤其是来自不理解这些原则的图形设计师的更奇特的东西。

例如:如果您的网站上有一个简单样式的栏目,您可以轻松地将其宽度设置为百分比,但如果它的背景图片具有特定宽度且未设计缩放请记住,它只有固定宽度才看起来不错。在这种情况下,您必须确保页面的其余部分正确占据剩余宽度。

请注意,您可以将像素与百分比一起使用。
例如,这是我最新的网络应用程序之一的片段:

min-width: 800px;
width: 80%;
max-width: 1500px;

选择还取决于您想要实现的设计或布局。

对于固定宽度布局,像素值就可以了。如果设计师给你一张 Photoshop 图像,其中包含非常奇特的东西,甚至考虑如何调整大小都非常复杂,那么你绝对应该这样做。

如果您的布局需要动态,您应该使用百分比来确保它随着分辨率的变化而扩展,并且您可以使用上面的代码片段使其在其他情况下看起来更好疯狂的。

某些布局(例如,想象一下 StackOverflow 会占用所有空间)在宽度为例如 的情况下看起来会非常难看。 1920 像素 - 线宽非常高,阅读起来非常不方便。
这就是 max-width 的用途。即使在某些动态布局中,您也必须限制网站的最大宽度,以最大限度地提高可用性和可读性。

并且还要考虑较小的屏幕。
确实,已经没有人使用 800×600 的桌面了,但许多人使用分辨率更小的移动设备浏览网页。
这就是 min-width 的用途:使动态扩展的布局在较小的分辨率上看起来不那么拥挤。

我希望这有帮助。

编辑:

Smashing Book对此主题有一些非常好的想法。

编辑 2:

我不希望我的帖子听起来像是我希望您对访问者强制执行基于像素的大小调整。
(显然,评论中的一些人以这种方式误解了我。)

澄清一下:

我相信理想的布局是能够很好地适应任何可能的分辨率或设置的布局。
然而,我们不可能总是把每件事都做得完美。时间/资源和目标受众是确定您的网站是否需要高级功能的关键。

我建议您针对给定的工作使用正确的东西。

如果您正在开发一个网站,其中有很大比例的访问者需要对该网站进行更高级的调整,那么这可能是非常值得的它。
(当然,有时我们这样做只是为了让自己有一种以正确的方式做事的感觉,但这并不总是一个经济上合理的决定。)

不过,您应该对它将是什么样的网站进行适当的研究,在决定布局之前,先确定谁将是访客,以及诸如此类的事情,以及是否值得花时间让它们变得流畅或更动态。

This is a difficult question, because the answer mostly depends on your situation.

Pixels are not that bad, I mostly use them too. (Sometimes even for font sizes.)
I usually fix the outer block element of the layout by a given size (pixels with fixed-width layouts, and percentages with fluid layouts), and on the inside elements I usually set percentages whenever possible.

There are some elements which simply can't be styled with percentages or ems, especially the more fancy stuff coming from graphical designers who don't understand such principles.

For example: if you have a column on your site with a simple style, you can set its width to a percentage easily, but if it has a background image with a specific width that is not designed with scaling in mind, it only looks good with a fixed-width. In such cases, you'll have to ensure that the rest of the page occupies the remaining width correctly.

Note that you can use pixels with percentages together.
For example, this is a snippet from one of my latest web apps:

min-width: 800px;
width: 80%;
max-width: 1500px;

The choice also depends on what design or layout you would like to achieve.

For a fixed-width layout, pixel values are fine. If a designer gives you a Photoshop image that contains really fancy stuff which would be extremely complicated to even think about how it would resize, you should definitely go for this.

If your layout needs to be dynamic, you should use percentages to make sure that it expands as the resolution changes, and you can use the above code snippet to make it look better in scenarios where it would otherwise look insane.

Some layouts (eg. imagine if StackOverflow would take up all the space) would look pretty ugly on a width of eg. 1920 pixels - the line widths would be so insanely high that it would be extremely unconvenient to read.
This is what max-width is for. Even in some dynamic layouts, you'll have to limit the maximal width of your site to maximize usability and readability.

And also take into consideration the smaller screens.
It is true that noone uses a 800×600 desktop anymore, but many people browse the web with mobile devices which have even smaller resolution.
This is what min-width is for: to make your dynamically expanded layout look less crowded on smaller resoultions.

I hope this helps.

EDIT:

The Smashing Book has some very nice thoughts about the subject.

EDIT 2:

I don't want my post to sound like I want you to force pixel-based sizing on your visitors.
(Apparently, some people in the comments misunderstood me in such a way.)

To clear it up:

I believe that the ideal layout is one that adjusts well to any possible resolution or setting.
However, we can't always do everything perfectly. Time/resources and the target audience are the key to determine if your site requires that advanced functionality or not.

I'm suggesting that you use the right thing for the given job.

If you are developing a site which will have a significant percentage of visitors who require more advanced adjustments to the site, it may be well worth it.
(Of course, sometimes we just do it for ourselves to have the feeling of doing things the right way, but is is not always a financially sound decision.)

Still, you should do the proper research about what sort of site will be it, who will be the visitors, and such stuff, before deciding about layouts, and whether it is worth the time to make them fluid or more dynamic.

§对你不离不弃 2024-09-09 17:06:46

字体大小

我认为您必须首先了解在 CSS 中处理像素时存在的问题

  • 旧版浏览器中的缩放功能已损坏。例如,IE6 和 IE7 缩放时不会调整文本大小。行高也可能很奇怪。这些问题在现代浏览器中不存在,但它们是许多人回避使用像素作为字体大小的原因。
  • 如果您以像素为单位指定字体大小,每个人都会看到相同大小的文本。浏览器的默认段落大小为 16 像素,因此如果您仅使用 em 和其他相对大小,您将尊重更改此设置的用户的决定。这对于文本较多的网站尤其重要,尤其是在有更多老用户的情况下。另一方面,如果网站的设计很重要,我认为使用 px 来指定字体大小而不破坏可用性是可能且合理的。

最后,你需要自己做出决定,这确实取决于具体情况,但我认为以像素为单位指定字体大小是可以的

顺便说一句,当使用 em 指定字体大小时,将 body 设置为 font-size 是一个好主意: 62.5%。这意味着基本字体大小为 10px,因此 1em 为 10px,1.6em 为 16px 等等,使得在使用 em 进行设计时更容易以像素为单位进行思考。我仍然觉得这样工作很令人沮丧,尤其是当 ems 的值级联时。有一些非常方便的网站,例如 PXtoEM.com 可以帮助解决此问题。

布局问题

屏幕是基于像素的布局,因此像素对于许多事情来说是一个直观的选择。这里的主要问题是不同的用户有不同的屏幕尺寸。正如其他人指出的那样,使用以像素为单位的 min-widthmax-width 以及以百分比为单位的 width 是尊重尺寸的有用方法屏幕的宽度,同时防止您的网站在非常小和非常大的窗口上被不合理地挤压或拉伸。

但是,我通常会避免这种方法,而倾向于使用 CSS 媒体查询。然后,您可以使用固定宽度的块,并随着屏幕尺寸的增加使布局更宽(除其他外)。然而,CSS 媒体查询与所有酷炫的 Web 技术一样,都缺乏浏览器支持。最值得注意的是,IE8 及更早版本不支持它们,尽管有 JavaScript 修复。另一方面,iPhone 和其他手持设备确实支持它们,如果您希望您的网站在这些设备上看起来不错,我强烈推荐它们。

我认为固定宽度网格很好。固定宽度网格系统,例如 960 网格系统
本身就足够受欢迎,而且还有很多其他网站具有固定宽度,我怀疑如果您这样做,您会听到很多抱怨。没有大屏幕的手持设备是一个问题,但这就是应该使用 CSS 媒体查询的地方,因此可以以像素为单位指定所有内容,并使您的网站在桌面和其他设备上看起来很漂亮iPhone。

结论

最终,一切都取决于您的用户是谁、您需要支持什么以及您希望网站的外观,但是在 CSS 中使用像素本身并没有什么问题

Font sizes

I think you must first understand the issues that exist with working with pixels in CSS:

  • Zoom in older browsers is broken. For example, IE6 and IE7 do not resize text when zooming. Line-height can be quirky too. These problems do not exist in modern browsers, but they are a reason why many shy away from using pixels for font sizes.
  • Everybody will see text the same size if you specify the font size in pixels. Browsers have a default size of 16px for paragraphs, so if you only use em and other relative sizes, you will respect the decision of users who change this. This is especially important on text heavy sites, especially if there are more older users. On the other hand, if the design of a site is important I think it is possible and justifiable to use px to specify font sizes without breaking usability.

In the end, you need to make the decision yourself, and it does depend on the exact circumstances, but I think that specifying font sizes in pixels is okay.

By the way, when working with em to specify font size it is a good idea to set the body to font-size: 62.5%. This means the base font size is 10px, so 1em is 10px, 1.6em is 16px and so on, making it easier to think in pixels while designing using ems. I still find it frustrating to work like this, especially when the values of ems cascade. There are some very handy sites like PXtoEM.com that help with this.

Layout issues

The screen is a pixel based layout, so pixels are an intuitive choice for many things. The main issue here is that different users have different screen sizes. As others have pointed out, using min-width and max-width in pixels along with width in percent is a helpful way to respect the size of the screen, while preventing your site to be unreasonably squished or stretched on very small and very large windows.

However, I would generally avoid this approach in favour of CSS media queries. You can then use fixed width chunks and make the layout wider (amongst other things) as the screen size increases. However, CSS media queries, like all cool web technologies, suffers from lack of browser support. Most notably, IE8 and earlier do not support them, although there are JavaScript fixes. On the other hand, the iPhone and other handheld devices do support them, and I would strongly recommend them if you want your site to look nice on these devices.

I think fixed width grids are fine. Fixed width grid systems like 960 Grid System
are popular enough in their own right, and there are so many other sites that have a fixed width, that I doubt you would hear many complaints if you did this. Handheld devices that do not have large screens are an issue, but this is where CSS media queries should be used, so it is possible to specify everything in pixels and have your site looking beautiful on the desktop and on the iPhone.

Conclusion

Ultimately, everything depends on who your users are, what you need to support, and what you want your site to look like, but there is nothing inherently wrong with using pixels in CSS.

寂寞花火° 2024-09-09 17:06:46

这取决于你的造型。例如,对于列,宽度可能应该取决于文本大小,以确保它在多种分辨率/屏幕上看起来最佳。如果您想将页面分为两部分,则应该使用百分比。但如果您希望这两个部分之间有 1 像素的边框(与分辨率无关),请使用像素。

That depends on what you are styling. For columns for example, the width should probably depend on the text size to ensure that it will look optimal on multiple resolutions/screens. If you want to divide your page in two parts, you should use percentages. But if you want a 1px border between these two parts, independent of the resolution, use pixels.

柳絮泡泡 2024-09-09 17:06:46

基本上,这取决于谁雇用您以及您作品的受众。
出于机构目的(内容应优先于形状,例如政府项目),您最好使用 .em 或 %,它们更难控制,但在可访问性方面它们确实对用户友好。

如果我们谈论企业网站(其中形状是交易),像素将是一个更准确的工具,可以满足您的客户对其品牌的期望。
当巧妙地完成时,液体界面(%,.em)总是一个好东西,但不要忘记在极端条件下检查您的设计并确保它是稳定的。

如果您使用像素,您将完全控制网站的最终外观,但您将不得不假设某些用户无法有效地使用它。

最好的选择:与其设计一个兼容所有平台的网站(这将导致设计的多缺陷),不如建议您的客户使用适合每种需求的特殊版本的网站,这对设计师来说是更好的做法和更好的业务。 ..

Basically it depends on who's hiring you and consequently the audience of your work.
For institutional purposes (where content should prevail over the shape, like a goverment project), you better work with .em or %, they are harder to control, but they will be really user friendly in terms of accessibility.

If we talk about corporative Websites (Where shape is the deal) pixel will be a more accurate tool to fulfill your customer expectation regarding his brand.
A liquid interface (%, .em) is always a good stuff when it is smartly done, but don't forget to check your design under extreme conditions and be sure that it will be stable.

If you work with pixels, you will have absolute control over the final look of your site, but you will have to assume the impossibility for some user's to operate with it efficiently.

Best option: rather than designing a Website compatible for all platforms (what will result in a multi-deficiency design) suggest your customers special versions of the site adapted for every demand, what is a better practice and a better business for a designer too...

可遇━不可求 2024-09-09 17:06:46

我想说的是,如果可能的话,除了某些情况之外,避免使用它。

例如,对于细边框,只需指定 1px 即可。

对于 max-min- 样式属性也是可以的。但如果可能的话,将非最大/最小属性设置为百分比。

I would say avoid it if possible except for in certain cases.

For instance for a thin border it's ok to just specify 1px.

Also for max- and min- style attributes it is ok. But then make the non-max/min attribute be a percentage if possible.

╰沐子 2024-09-09 17:06:46

这确实是一个好问题,我之前已经问过很多次了。我不是一名铁杆网页设计师(我更倾向于开发方面),我通常会询问与我一起工作的设计师的意见,以下是我所提炼的内容:

使用百分比与像素来调整元素的大小等等确实是个人喜好或每个具体情况的要求问题。如果您需要它可以缩放,或者它可以很好地缩放,请使用百分比。如果没有,请使用像素。这里的人们使用了页面中主要列的示例,这些列可能需要流畅,但边框很可能需要像素测量的精度。

显然,图像大小与其分辨率和像素单位密切相关,因此在这种情况下我总是使用它们。

然而,使用 em 大小调整与像素甚至点大小调整文本的大小完全不同。与我一起工作的大多数人都有一个字体大小的基本重置样式,这将 1em 的大小缩小到大约 12px。然后,他们在其他地方(或尽可能接近其他地方)使用 em 来调整文本元素、表单控件等的大小。这也是我现在的操作方式,因为它似乎可以在台式机和笔记本电脑上的各种浏览器、操作系统和 DPI 上运行良好。但我不能保证移动设备。

不过,可访问性是关键 - 如果您需要为残疾人士提供一些可用的东西,或者需要在各种开箱即用的设备上工作,即使是您可能认为过时的东西,那么扩展将是一个要求。将其构建到您的模型中,以便从一开始就设计网站,您可能会意识到在这种情况下甚至不需要绝对像素大小。

例如,一个艺术性很强的设计可能会以单一的规模进行设计,但针对残疾用户的新教育网站将必须在各种情况下工作。

请记住,W3C 将各种大小调整和缩放方法纳入其规范中是有原因的 - 灵活性。做任何最适合并且对你的受众有效的事情(正如穆斯塔德之前所说的)。

This really is a good question which I have asked many times before. Not being a hardcore web designer (I'm more on the development side) I've usually asked the designers I work with about their opinions, and here is what I have distilled:

Using percentages vs pixels for sizing elements and so on is really a matter of personal taste or the requirements in each specific case. If you need it to scale, or it will scale well, use percentages. If not, use pixels. People here have used the examples of major columns in a page maybe needing to be fluid but borders may well need the precision of a pixel measurement.

Obviously sizing images is pretty much tied to their resolution and pixel units, so I always use them in this case.

However, using em sizing versus pixel or even point sizing for text is a whole different kettle of fish. Most of the guys I work with have a base reset style for font sizes which brings the size of 1em down to about 12px. They then use ems everywhere else (or as near to everywhere else as is possible) to size text elements, form controls and the like. This is how I operate now too as it seems to work well across a variety of browsers, OSes and DPIs on desktops and laptops. I can't vouch for mobile devices though.

Accessibility is the key though - if you need to make something usable for people with disabilities or work on a variety of devices out the box, even things you may consider old, then scaling will be a requirement. Build it into your model for designing the site from the get-go and you may realise that absolute pixel sizing is not even needed in that case.

For example, an art-heavy design is probably going to be designed at a single scale, but the new educational website for disabled users is going to have to work in a variety of situations.

Just remember, the W3C put a variety of methods for sizing and scaling into their specifications for a reason - flexibility. Do whatever fits best and works well for your audience (as Moustard said earlier).

澉约 2024-09-09 17:06:46

我不是 css/html 专家,但我使用的惯例是使用像素作为外部容器,使用百分比作为内部对象。

我对审查我的布局的人的另一个经验法则是“三像素规则”。在每个浏览器中,所有内容都完全一致的情况很少见。我们一致认为,将任何东西移动三个像素或更少,所付出的努力胜过所带来的好处。

I'm not a css/html expert but convention I use is to use pixels for the outer containers and percentages for the inner objects.

Another rule of thumb I have with anyone reviewing my layout is a Three Pixel Rule. It is rare that everything will line up exactly within every browser out there. We've agreed the effort outweighs the benefits to move anything three pixels or less.

白日梦 2024-09-09 17:06:46

默认做正确的事。如果用户在禁用 javascript 且没有偏好 cookie 的情况下访问您的网站,您应该为他们提供一个尽可能实用且易于访问的网页。

您的默认样式表不应该对浏览器窗口进行任何假设,并且仍然呈现一个功能页面,无论多么丑陋,分辨率低至 320x320,尺寸无上限。如果摄影保真度如此重要以至于您不能信任浏览器缩放(有时确实如此),那么您应该对封装固定大小图像的元素使用像素测量(至少是最小值),以确保您不会最终得到重要的事情被掩盖了。指定最小宽度和百分比应该可以为您提供所需的控制,同时为用户提供他们所需的可访问性。即使现在可访问性不是问题,但如果网站成功的话,那也是以后的事了。

当您确实了解浏览器窗口的更多信息时,如果保留设计的重要性证明增加的工作是合理的,那么在备用样式表中使用固定宽度是可以的。

Do the right thing by default. If a user visits your site with javascript disabled and no preference cookies, you should serve them a web page that is as functional and accessible as possible.

Your default stylesheet should assume nothing about the browser window, and still render a functional page, however hideous, down to 320x320 resolution, and up to unbounded sizes. If photographic fidelity is so important that you can't trust browser scaling, and sometimes it is, then you should use pixel measurements (at least minimums) for elements encapsulating images of fixed size, to make sure that you don't end up with important things being covered up. Specifying minimum widths along with percentages should give you the control you need while giving your users the accessibility they need. Even if accessibility isn't an issue now, it will be later if the site is successful.

When you do know more about the browser window, then it's okay to use fixed widths in alternate stylesheets, if the importance of preserving the design justifies the added work.

爱你不解释 2024-09-09 17:06:46

如果您的目标是固定大小,那么像素就可以了。但是,如果您希望布局在各种分辨率/屏幕尺寸上看起来都不错,那么您应该坚持使用更相对的度量,例如百分比。大多数人的目标是后者。

If you're targeting a fixed size then pixels are fine. But if you want your layout to look good on various resolutions/screen sizes, then you should stick to more relative measures like percentages. Most folks are targeting the latter.

吹梦到西洲 2024-09-09 17:06:46

如果这就是取悦您的客户所需要的,那就可以了。大多数时候,您无法仅用百分比来平衡不同屏幕尺寸的空间量。 CSS3 媒体查询在这里有所帮助,但采用仍然存在问题。

It is ok if that is what it takes to please your client. Most of the time, you can't balance the amount of space in diverse screen sizes with only percentages. CSS3 media queries helps here but adoption is still problematic.

遥远的她 2024-09-09 17:06:46

在大多数情况下我更喜欢像素。
例如 - 内容区域的默认宽度:960px。
在“1024”中就可以了,屏幕两侧都有小的可用空间。在那里放一些渐变或背景。在“1280”或“1440”或“1680”,或其他什么 - 差距会越来越大,但看起来不错。
是的,对于 800 - 这个网站会很难看。但谁在乎呢?每个主要玩家都放弃了对 800 的支持。有这个解决方案的人太少了。

有很多不同的站点,但 %% 将是更好的解决方案。

I prefer pixel in most cases.
For example - default width of content ares: 960px.
In "1024" it will be ok, with small free spaces on the both side of the screen. Put some gradient or background there. In "1280" or "1440" or "1680", or whatever - there will be bigger and bigger gap, but it will look nice.
Yep, for 800 - this site will be ugly. But who cares? Every major players dropped support for 800. There are too few people with this resolution..

There are many various sites, where %% will be much better solution, though.

花期渐远 2024-09-09 17:06:46

屏幕的组成部分是像素。使用固定的像素大小是不会出错的。正如其他发帖者所提到的,您还可以使用百分比或“em”作为可扩展的替代品。

The building blocks of a screen are pixels. You can't go wrong with fixed pixel sizes. As other posters have mentioned, you can also use percentages or "em"s for a scalable substitute.

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