HTML/CSS 中的像素与点

发布于 2024-11-16 00:15:05 字数 105 浏览 3 评论 0原文

创建 HTML 页面时,我应该使用像素或 CSS 中的点来指定诸如 margin 之类的内容吗?

其中一种做法是否被认为比另一种更好?两者有何优点或缺点?

When creating an HTML page, should I specify things like margins with pixels or with points in CSS?

Is one of them considered to be better practice than the other? Any advantages or disadvantages to either one?

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

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

发布评论

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

评论(8

微暖i 2024-11-23 00:15:05

使用pxem

点 (pt):点传统上用于打印介质(任何要打印在纸张上的内容等)。一点等于 1/72 英寸。点很像像素,因为它们是固定大小的单位并且不能缩放大小。

一般来说,1em = 12pt = 16px = 100%。

[结论]

获胜者:百分比 (%)。

  • JohnB 注意:这是针对文本的。显然您问的是“诸如边距之类的东西”。 (我假设您也指的是 padding。)为此,我建议使用 pxem。就我个人而言,我会根据具体情况进行切换。

更多文章

点值仅适用于打印 CSS!

(进一步评论)

积分可以打印吗?不。

积分并非专门用于打印。理论上,点是用来定义绝对度量的。像素不是绝对的,因为根据您的屏幕和选择的清晰度(不是分辨率),分辨率(每英寸像素)可以很大(150dpi)或很小(75dpi)。这意味着您的像素可以是一个大小,也可以是该大小的一半。这意味着您设计的在屏幕上完全清晰的文本可能在您客户的屏幕上看起来太大(“请将文本变小,好吗?”)或太小而无法在您邻居的屏幕上阅读(“嘿,您的网站”)。告诉我前几天的事吗?你说你已经写过的那个……好吧,我无法很好地阅读文本,它太小了”)。

积分是这个问题的解决方案。但浏览器和操作系统需要管理这些。基本上,这意味着:

浏览器必须使用给定值(例如 10pt)和屏幕的实际分辨率来计算显示尺寸(以像素为单位);
操作系统必须传达当前的实际分辨率,而不是默认值。

另外:

Use px or em

Points (pt): Points are traditionally used in print media (anything that is to be printed on paper, etc.). One point is equal to 1/72 of an inch. Points are much like pixels, in that they are fixed-size units and cannot scale in size.

Generally, 1em = 12pt = 16px = 100%.

[Conclusion]

The winner: percent (%).

  • JohnB note: this is for TEXT. Obviously you asked about "things like margins." (I assume you mean padding also.) For that, I would recommend px or em. Personally, I switch, depending on the particular situation.

MORE ARTICLES

Point values are only for print CSS!

(Comment further down)

Points are for print? Nope.

Points are not for print exclusively. Theoretically, points are for defining an absolute measure. Pixels are not absolute, since depending on your screen and chosen definition (not resolution), the resolution (pixels per inch) can go from a lot (150dpi) or very little (75dpi). Which means your pixels can be a size, or maybe half that size. Which means that text you design to be perfectly legible on your screen may look too big on your client’s screen (“please make the text smaller, ok?”) or too small to be readable on your neighbor’s screen (“hey, the website you told me about the other day? the one you said you had worked on… well i couldn’t read the text very well, it’s so small”).

Points are a solution to this issue. But browsers and operating systems need to manage those. Basically, it means:

browsers have to calculate the display size in pixels using the given value (say, 10pt) and the screen’s real resolution;
operating systems have to communicate the real current resolution, and not a default value.

Also:

傲世九天 2024-11-23 00:15:05

经验法则是:

屏幕显示的像素;打印点。

'em' 或 '%'(以及鲜为人知的 rem)更适合更灵活的布局。

em 是基于当前字体中字母“m”的大小的度量单位。以 em 为单位指定大小可以让您根据字体大小确定大小,这意味着您可以更改字体,并且布局也会随之更改。

但很多时候您需要使用固定大小。在这种情况下,px 通常是最好的,因为大多数网页都显示在基于像素的屏幕上。但是,如果您计划有一个需要大量打印的页面,那么您可以使用具有基于点的布局的特定于打印的样式表。

一般来说,我会推荐使用 em 而不是 pxpt。如果您使用 px,这是因为页面上的元素大小以像素为单位(例如图像),您需要布局的其余部分符合这些元素。在这种情况下,因为图像以像素为单位,所以样式表也应该以像素为单位。此外,由于点是打印测量单位,因此无法保证它们将如何显示在屏幕上:px 是基于屏幕的,因此将为您提供跨浏览器和跨平台的屏幕上更加一致的外观。

顺便说一句,此页面可能会为您提供一些进一步的信息:http://webdesign。 about.com/cs/typemeasurements/a/aa042803a.htm

The rules of thumb is:

Pixels for screen display; points for printing.

'em' or '%' (and the lesser known rem) are better for a more flexible layout.

em is a unit of measure based on the size of the letter 'm' in the current font. Specifying sizes in em allows you to have a size based on the font size, meaning that you can change the font, and the layout will change to follow suit.

But there are plenty of times when you need to use a fixed size. In that case, px is usually going to be best, as most web pages are displayed on a pixel-based screen. But if you're planning to have a page which is printed a lot, then you could have a print-specific stylesheet with a point-based layout.

Generally I'd recommend em over px or pt. If you're using px, it is because you have elements on your page which are sized in pixels such as images, which you need the rest of the layout to conform to. In this case, because the images are in pixels, so should the stylesheets. In addition, because points are a printing unit of measure, there's no guarantee how they'll appear on the screen: px is screen-based, so will give you a much more consistent look on-screen cross-browser and cross-platform.

By the way, this page might give you some further info: http://webdesign.about.com/cs/typemeasurements/a/aa042803a.htm

少钕鈤記 2024-11-23 00:15:05

点非常好,因为它们的高度为 1/72 英寸。您知道您的文字有多高。像素的问题在于,分辨率越高,像素就越小。 如果您想将文本环绕在背景图片周围,像素非常有用。需要一些工作,但它可以制作出漂亮的页面。我听说 IE 中的像素不能调整大小 - 所以如果他们想增加字体大小,他们不能。不用IE,所以不好说。记得听到过这句话。 EM 让您受制于人们如何在浏览器中定义字体大小。 Em 和百分比让这一切变得简单。我总是使用积分。

[...]

不,积分并不是最好的。对于任何找到此主题的人,请忘记您曾经读过该主题。这是一个网页设计论坛。用于在屏幕媒体上显示页面。 CSS 中包含点仅用于打印设计的目的。对于印刷媒体样式表。 它们不能像 % 和 ems 那样进行缩放。如果您是任何类型的网页设计师,您应该努力使您的页面易于访问,而使用点是对这一点的直接打击。

http://www.v7n.com /forums/coding-forum/17594-font-size-pixel-vs-point.html

很有趣,每个人都回答“像素或%/em”而不是“像素或点”。

我不知道两者之间有区别。

更多信息

编辑:更多信息...官方信息!

请勿以 pt 为单位指定字体大小,或屏幕样式表的其他绝对长度单位。它们在不同平台上呈现不一致,并且无法由用户代理(例如浏览器)调整大小。 在具有固定和已知物理属性(例如打印)的媒体上继续使用此类单位进行样式设置

Points are great since they are 1/72 inch tall. You know how tall your text will be. Problem with pixels is that they get smaller the higher the resolution. Pixels are great if you want to wrap text around a background picture. Takes some work but it can make a nice looking page. I have heard that pixels are not resizable in IE - so if they want to increase the font size they can't. Don't use IE, so I can't say. Remember hearing that. EM's leave you at the mercy of how the person has that defined font sizes in their browser. Ems and percentages make it easy. I always use points.

[...]

No, points are not the best. To anyone who finds this thread, forget that you ever read that. This is a web design forum. For displaying pages on screen media. Points are included in CSS solely for the purpose of print design. For print media stylesheets. They are not scalable as % and ems are. If you are any sort of web designer you should be working to make your pages accessible and using points is a strike against that right off the top.

http://www.v7n.com/forums/coding-forum/17594-font-size-pixel-vs-point.html

It's funny, everyone answer "pixel or %/em" and not "pixel or points".

I didn't know there was a difference between both.

more info

Edit : even more info... official ones!

Do not specify the font-size in pt, or other absolute length units for screen stylesheets. They render inconsistently across platforms and can't be resized by the User Agent (e.g browser). Keep the usage of such units for styling on media with fixed and known physical properties (e.g print).

忆梦 2024-11-23 00:15:05

任何告诉您使用像素的人都是错误的。像素工作正常,但它们根本不是必需的......永远!点是指定绝对度量的完美方式,并且对于我们在网络上通常使用的规模来说,它们通常不是首选度量。

除了点之外,还有异食癖、英寸、厘米等。选择其中一个而不是另一个很像在说“我应该用英尺还是英寸来测量这个房间?”让常识成为您的指南。他们都会完成工作。

Em,在一些答案中出现,应该在适当的时候保留。也就是说,“当这个东西扩展时,我希望另一个东西扩展”。这就是相对措施的目的。我知道这超出了你原来问题的范围,但我不得不解决一些关于“总是使用 em”的废话。

顺便说一句,像素不等于物理像素。如今,样式表中的 px 表示 1/96 英寸。这就是为什么我说不要使用它们。大多数人不知道这一点。他们使用它们时认为他们正在指定实际像素。我不能认真对待这些人,因为这是显而易见的(虽然我不责怪这些人,但我责怪事物状态的令人困惑的本质,这就是为什么我呼吁像素消失。)。此外,如果像素真的意味着像素,那么它们将是一种可怕的指定尺寸的方式。谈论这样一个事实:事物会根据无法控制的任意屏幕分辨率随机缩小和增长。哎呀!远离像素!!!在实践中它们是有效的,但只是由于浏览器制造商和操作系统人员捏造了看不见的努力,理论上它们是一种非常模糊的方式来指定你的意图。

Anybody who tells you to use pixels is wrong. Pixels work fine, but they are simply not required... Ever! Points are a perfectly fine way to specify an absolute measure, and for the scale we are commonly working at on the web they are more often then not the preferred measure.

Beside points, there is also pica, inch, centimeter and so forth. Choosing one of these over the other is a lot like saying, "should I measure this room in feet or inches?" Let common sense be your guide. They'll all get the job done.

Em, which came up in some of the answers, should be reserved for when it's appropriate. That is to say, "when this thing scales, I want this other thing to scale". That's what relative measures are for. I know that is beyound the scope of your original question, but I had to address some of the nonsense about "always using em".

BTW, pixels don't equal physical pixels. Today, px in a stylesheet means 1/96th of an inch. This is why I say don't use them. Most people don't know this. They use them thinking they're specifying actual pixels. I can't take these people seriously with this being apparent (Although I don't blame the people, I blame the confusing nature of the state of things which is why I campaign for pixels to disappear.). Furthermore, if pixels really meant pixels, they'd be a horrible way to specify dimensions. Talk about the fact that things would randomly shrink and grow based on uncontrollable arbitrary screen resolutions. Yikes! Stay away from pixels!!! In practice they work, but only due to fudging an unseen efforts on the part of browser makers and OS folks, in theory they're a horribly ambiguous way of specifying your intentions.

把人绕傻吧 2024-11-23 00:15:05

上面约翰 B 的回答是最好、最准确的。我只是想指出他上面的答案可能造成的混乱。排版中的“em”是您选择的字体中字母“m”的宽度。为了指定字体的高度,打印机/排字机使用“x 高度”,即字体的小写 x 的高度。

正如 John 指出的那样,pt 是固定的测量单位,等于 1/72 英寸。由于显示器具有不同的像素密度(72/英寸、96/英寸...),因此通常不是在 HTML 文档中调整内容大小的好方法。

em 与旧的排版单位直接相关,并且是一个很好的相对度量。当屏幕尺寸缩放时,字体大小也会随之缩放。如果您使用 em 作为边距,它们会根据您的字体大小进行缩放,这通常是一件好事。

但是,对于边距、填充和所有与字体不直接相关的内容,最好使用 rem 或“相对 ems”。最好的方法是首先为您的 body 或 html 标记声明默认字体大小。像 body font-size = 16px 这样的东西是一个很好的起点。然后文档中的其他地方使用 em 表示文本,使用 rem 表示其他所有内容。或者,使用百分比。两者都可以正常工作。与 em 和 rem 一样,您的 % 是相对于原始 16px = 100% 字体大小的。

文档中的所有内容都将相对于 100% 字体大小(16 像素)的初始设置进行缩放。这样,您只需在文档中使用一次像素测量。如果您稍后想要设置媒体查询来调整大小和边距以适应不同设备显示器上的不同像素密度,这会派上用场。您只需查询 body 标记中的一个初始声明即可。其他一切都会相应调整,不需要改变。

只是一个想法,
最大宽度3st

John B's answer above is the best and most accurate. I just wanted to point out a possible confusion created by the answer above his. An "em" in typography is the width of the letter "m" in the font that you've chosen. To specify height for a font, printers/typesetters used the "x height", which is the height of the lower case x for a font.

As John points out, pt's are a fixed unit of measure equal to 1/72nd of an inch. Since monitors have varying pixel densities (72/inch, 96/inch...) it isn't generally a good way to size things in HTML docs.

The em relates directly to the old typography unit and makes an excellent relative measure. As your screen size is scaled, the font sizes scale with it. If you use em's for margins, they scale relative to your font sizes, which is generally a good thing.

But, for margins, padding and all things not directly font related, it's best to use rem's, or "relative ems". The best way to do this is declare a default font size for your body or html tag initially. Something like body font-size = 16px is a good place to start. Then everywhere else in the document use em's for text, and rem's for everything else. Or, use percentages. Either will work fine. Like em's and rem's, your % is relative to that original 16px = 100% font size.

Everything in the document will scale relative to your initial setting for your 100% font size at 16px. That way you only use a pixel measurement once in the document. This comes in handy if you later want to set media queries to adjust your sizes and margins to accommodate different pixel densities across different device displays. You only have to have queries for that one initial declaration in the body tag. Everything else will adjust relative to that and won't need to be changed.

just a thought,
maxw3st

当梦初醒 2024-11-23 00:15:05

我认为这取决于你想做什么。

我发现关键问题是距离是否需要随窗口调整大小?有些单位是相对的,有些(如像素和点)不是 - 简要说明是 在这里

我还没有看到太多使用的点,当需要绝对测量时, px 似乎更常见。

I think it depends on what you're trying to do.

I find the key question is does the distance need to resize with the window? Some units are relative, some (like pixels and points) are not - a brief description is here.

I haven't seen points used much, px seems more common when an absolute measurement is needed.

書生途 2024-11-23 00:15:05

答案是:视情况而定。你用你的利润做什么?它们是平衡的、可调整大小的布局的一个组成部分,还是只是在边缘提供了一个排水沟?在第一种情况下百分比效果更好,在第二种情况下像素效果更好。

您应该尝试不同的可能性并确定哪种最适合您的文档。由于在这种情况下没有“对与错”,因此您可以选择最适合您的答案。

The answer is: It depends. What are you using your margins for? Are they an integrated part of a balanced, resizable layout, or do they just provide a gutter around the edges? Percentages work better in the first case, and pixels work well in the second.

You should try the different possibilities and determine which works best for your document. Since there is no "right and wrong" in this case, you can choose the answer that works best for you.

小清晰的声音 2024-11-23 00:15:05

我几乎所有事情都使用像素。

对我来说,“感觉”我有更精确的控制。

对于一些需要随窗口动态调整大小的内容,我使用百分比。

编辑:

什么是“em”?

I use pixels for nearly everything.

For me, it "feels" like I have more precise control.

For the few things that I need to dynamically resize with the window, I use percent.

EDIT:

What is "em"?

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