HTML/CSS 中的像素与点
创建 HTML 页面时,我应该使用像素或 CSS 中的点来指定诸如 margin
之类的内容吗?
其中一种做法是否被认为比另一种更好?两者有何优点或缺点?
When creating an HTML page, should I specify things like margin
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
px
或em
padding
。)为此,我建议使用px
或em
。就我个人而言,我会根据具体情况进行切换。更多文章
(进一步评论)
另外:
Use
px
orem
padding
also.) For that, I would recommendpx
orem
. Personally, I switch, depending on the particular situation.MORE ARTICLES
(Comment further down)
Also:
经验法则是:
屏幕显示的像素;打印点。
'em' 或 '%'(以及鲜为人知的
rem
)更适合更灵活的布局。em
是基于当前字体中字母“m”的大小的度量单位。以 em 为单位指定大小可以让您根据字体大小确定大小,这意味着您可以更改字体,并且布局也会随之更改。但很多时候您需要使用固定大小。在这种情况下,
px
通常是最好的,因为大多数网页都显示在基于像素的屏幕上。但是,如果您计划有一个需要大量打印的页面,那么您可以使用具有基于点的布局的特定于打印的样式表。一般来说,我会推荐使用
em
而不是px
或pt
。如果您使用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
overpx
orpt
. If you're usingpx
, 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
http://www.v7n.com /forums/coding-forum/17594-font-size-pixel-vs-point.html
很有趣,每个人都回答“像素或%/em”而不是“像素或点”。
我不知道两者之间有区别。
更多信息
编辑:更多信息...官方信息!
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!
任何告诉您使用像素的人都是错误的。像素工作正常,但它们根本不是必需的......永远!点是指定绝对度量的完美方式,并且对于我们在网络上通常使用的规模来说,它们通常不是首选度量。
除了点之外,还有异食癖、英寸、厘米等。选择其中一个而不是另一个很像在说“我应该用英尺还是英寸来测量这个房间?”让常识成为您的指南。他们都会完成工作。
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.
上面约翰 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
我认为这取决于你想做什么。
我发现关键问题是距离是否需要随窗口调整大小?有些单位是相对的,有些(如像素和点)不是 - 简要说明是 在这里。
我还没有看到太多使用的点,当需要绝对测量时, 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.
答案是:视情况而定。你用你的利润做什么?它们是平衡的、可调整大小的布局的一个组成部分,还是只是在边缘提供了一个排水沟?在第一种情况下百分比效果更好,在第二种情况下像素效果更好。
您应该尝试不同的可能性并确定哪种最适合您的文档。由于在这种情况下没有“对与错”,因此您可以选择最适合您的答案。
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.
我几乎所有事情都使用像素。
对我来说,“感觉”我有更精确的控制。
对于一些需要随窗口动态调整大小的内容,我使用百分比。
编辑:
什么是“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"?