Best unit for font-sizes in CSS

2019-03-15 09:08发布

What are the advantages & disadvantages of each? em, px, % and pt ?

My current choice are percentages, the only reason is because I can globally change the font-size of all elements, just by modifying the font size on the root element (body)

5条回答
劫难
2楼-- · 2019-03-15 09:37

My original design training said em's where possible.

I believe a main reason was that different browsers and devices have different resolutions, which is once again becoming a big issue.

I think it's frequently better to use em's and %'s as they are an abstraction layer particularly when compared to pixels. Both are similar in some respects as "100%" = "1em". Another problem with the pixel unit is that it does not scale upward for visually-impaired readers.
Today, for mobile, pad, etc. consideration it's often better to have specific stylesheets / rules for each one.

Also, for print concerns, em works well.

I have seen em unit called a standard for font sizes on the web, but the percent unit often gives a more consistent and accessible display. When user settings are changed, percent text scales well preserving readability, accessibility, and visual design.

查看更多
唯我独甜
3楼-- · 2019-03-15 09:43

I would recommend EM — simply because I use Baseline CSS for my basic set up of forms, layout and most importantly type.

Can't recommend it enough : http://baselinecss.com/

查看更多
你好瞎i
4楼-- · 2019-03-15 09:51

Here is a link to one of my favorite articles on proper sizing of text with css from AListApart:

http://www.alistapart.com/articles/howtosizetextincss

Semantically, em is preferred, but I've always found it problematic because it impacts each child element inclusively. If your design nests 4 or 5 divs and each is at .75 em, by the time you get to the last child div your text is almost unreadable.

My preference is pt because it works with various operating systems (allowing the system itself to decide what a pt is) rather than using px which can really put a pinch on the readability of a site depending on resolution. Em is considered the "standard" for css, but it has just as many problems as the others, but it does have the advantage of cascading globally.

查看更多
贼婆χ
5楼-- · 2019-03-15 09:57

Nice question !

Me i'm using pixel because I like it when everything is fixed.

But checkout this article :

http://kyleschaeffer.com/best-practices/css-font-size-em-vs-px-vs-pt-vs/

could help you make a choice. I think all this is very personal. Or it depend of what you are doing I suppose

查看更多
闹够了就滚
6楼-- · 2019-03-15 09:58

I always use ems. Using % is kind of the same, but they mean something else when using them in a padding or margin statement (padding:1em 0; is not the same as padding:100% 0;). So just use ems I you mean relative to the current font size, and avoid any confusion.

Added benefit or using ems is you could e.g. use a media query and body{font-size: 120%} to give mobile users a slightly bigger fontsize.

查看更多
登录 后发表回答