em vs px... for mobile browsers
px
won't be a real actual pixel when the screen is zoomed out on a modern mobile browser. These browsers are effectively emulating a desktop browser with desktop browser-size pixels.
So for this type of browser using px
is no worse than it is for a normal desktop browser. You can do it if you want without major problems, but in both situations em
is preferable for the main body content, if you can.
px or em when creating a website for mobile devices?
Mobile browser handle font-size
defining with pixel perfectly, because they are using for rendering not usual physic pixel but screen pixel.
Similar question has the same answer:
“px won't be a real actual pixel when the screen is zoomed out on a modern mobile browser. These browsers are effectively emulating a desktop browser with desktop browser-size pixels.”
I totally agree with this statement
Why em instead of px?
The reason I asked this question was that I forgot how to use em's as it was a while I was hacking happily in CSS. People didn't notice that I kept the question general as I wasn't talking about sizing fonts per se. I was more interested in how to define styles on any given block element on the page.
As Henrik Paul and others pointed out em is proportional to the font-size used in the element. It's a common practice to define sizes on block elements in px, however, sizing up fonts in browsers usually breaks this design. Resizing fonts is commonly done with the shortcut keys Ctrl++ or Ctrl+-. So a good practice is to use em's instead.
Using px to define the width
Here is an illustrating example. Say we have a div-tag that we want to turn into a stylish date box, we may have HTML-code that looks like this:
<div class="date-box">
<p class="month">July</p>
<p class="day">4</p>
</div>
A simple implementation would defining the width of the date-box
class in px:
* { margin: 0; padding: 0; }
p.month { font-size: 10pt; }
p.day { font-size: 24pt; font-weight: bold; }
div.date-box {
background-color: #DD2222;
font-family: Arial, sans-serif;
color: white;
width: 50px;
}
The problem
However, if we want to size the text up in our browser the design will break. The text will also bleed outside the box which is almost the same what happens with SO's design as flodin points out. This is because the box will remain the same size in width as it is locked to 50px
.
Using em instead
A smarter way is to define the width in ems instead:
div.date-box {
background-color: #DD2222;
font-family: Arial, sans-serif;
color: white;
width: 2.5em;
}
* { margin: 0; padding: 0; font-size: 10pt; }
// Initial width of date-box = 10 pt x 2.5 em = 25 pt
// Will also work if you used px instead of pt
That way you have a fluid design on the date-box, i.e. the box will size up together with the text in proportion to the font-size defined for the date-box. In this example, the font-size is defined in *
as 10pt and will size up 2.5 times to that font size. So when you're sizing the fonts in the browser, the box will have 2.5 times the size of that font-size.
Best font size em or px to use for Website and Web apps?
This is an extremely subjective question. In addition, there are more options than just em and px: rem, em, px, vw/vh. Understanding how they all work is the key to picking which one is best for you and you app depending on the situation. Most of the time I use rem/em for responsive reasons, but there are times with vw/vh or px are a better option for me. It totally depends on what you're trying to accomplish.
HTML: How common is it that different browsers render 1em at a different actual px size?
No, you cannot.
The size of em
in pixels is related to the font type & size you're using, the resolution of your screen (depending on OS, browser), and possible further OS and browser settings - eg "Show fonts +10%" may alter the em
value.
Em vs Px and cross browser compatibility
Strictly speaking the use of em over px isn't really a cross-browser compatibility issue - all browsers support the use of them both for font-sizing.
It used to be advisable to avoid using px for font sizing as the browser with the biggest market share, IE6, wouldn't allow text resizing. It sees px as an absolute value, not a relative value like em, and so wouldn't scale text up or down sized in px.
Depending on your audience you may not need to worry about it. There's some great info on the benefits of the em, and how they can help towards building a fluid layout here: http://www.alistapart.com/articles/fluidgrids/
Should I use px or rem value units in my CSS?
TL;DR: use px
.
The Facts
First, it's extremely important to know that per spec, the CSS
px
unit does not equal one physical display pixel. This has always been true – even in the 1996 CSS 1 spec.CSS defines the reference pixel, which measures the size of a pixel on a 96 dpi display. On a display that has a dpi substantially different than 96dpi (like Retina displays), the user agent rescales the
px
unit so that its size matches that of a reference pixel. In other words, this rescaling is exactly why 1 CSS pixel equals 2 physical Retina display pixels.That said, up until 2010 (and the mobile zoom situation notwithstanding), the
px
almost always did equal one physical pixel, because all widely available displays were around 96dpi.Sizes specified in
em
s are relative to the parent element. This leads to theem
's "compounding problem" where nested elements get progressively larger or smaller. For example:body { font-size:20px; }
div { font-size:0.5em; }Gives us:
<body> - 20px
<div> - 10px
<div> - 5px
<div> - 2.5px
<div> - 1.25pxThe CSS3
rem
, which is always relative only to the roothtml
element, is now supported on 99.67% of all browsers in use.
The Opinion
I think everyone agrees that it's good to design your pages to be accommodating to everyone, and to make consideration for the visually impaired. One such consideration (but not the only one!) is allowing users to make the text of your site bigger, so that it's easier to read.
In the beginning, the only way to provide users a way to scale text size was by using relative size units (such as em
s). This is because the browser's font size menu simply changed the root font size. Thus, if you specified font sizes in px
, they wouldn't scale when changing the browser's font size option.
Modern browsers (and even the not-so-modern IE7) all changed the default scaling method to simply zooming in on everything, including images and box sizes. Essentially, they make the reference pixel larger or smaller.
Yes, someone could still change their browser default stylesheet to tweak the default font size (the equivalent of the old-style font size option), but that's a very esoteric way of going about it and I'd wager nobody1 does it. (In Chrome, it's buried under the advanced settings, Web content, Font Sizes. In IE9, it's even more hidden. You have to press Alt, and go to View, Text Size.) It's much easier to just select the Zoom option in the browser's main menu (or use Ctrl++/-/mouse wheel).
1 - within statistical error, naturally
If we assume most users scale pages using the zoom option, I find relative units mostly irrelevant. It's much easier to develop your page when everything is specified in the same unit (images are all dealt with in pixels), and you don't have to worry about compounding. ("I was told there would be no math" – there's dealing with having to calculate what 1.5em actually works out to.)
One other potential problem of using only relative units for font sizes is that user-resized fonts may break assumptions your layout makes. For example, this might lead to text getting clipped or running too long. If you use absolute units, you don't have to worry about unexpected font sizes from breaking your layout.
So my answer is use pixel units. I use px
for everything. Of course, your situation may vary, and if you must support IE6 (may the gods of the RFCs have mercy on you), you'll have to use em
s anyway.
Why use rem instead px when it's the same anyway?
So after all the research, I came to the conclusion that the only advantage of rem
, is that users who use a bigger default font-size in their browser setting, will get the font-size scaled properly, while px
will not scale. In other words, using rem
for font-size
, adds support for Accessibility to your website.
To sum up:
rem
is a way to add support for Accessibility to your website.- Keep in mind, most users use zoom instead of font-size change in their browser and phones, because zoom easier to access.
- Browser zoom has additonal affect on
em
, in other words, it scalesrem
andpx
equally. - Since 2012,
rem
is supported by all major browsers on desktop and mobile devices. - Use
px
as fall back option for IE8 and lower. - Although not specified by W3C, all browser across desktop and mobile devices have implemented a default
font-size
of 16px (feel free to Google yourself), which equals to 1rem
/em
- To make the conversion easier between
px
andrem
you can usehtml {font-size: 62.5%;}
which converts10px
to1rem
In one sentence: Use rem
with px
on font-size
to support accessibility.
html {
font-size: 62.5%;
}
.your_class {
font-size: 16px;
font-size: 1.6rem;
}
CSS - What is best to use for this case (px, %, vw, wh or em)?
Note that I only mentioned the ones you asked about.
Here you can see the full list of CSS measurement units: CSS Units in W3Schools
Rather than telling you which one is the "right one", I would rather want you to understand what each one actually is.
Pixels (px
): Absolute pixels. So for example, 20px
will be literally 20 pixels on any screen. If a monitor is of 1980x1200, and you set an element's height to 200px
, the element will take 200 pixels out of that.
Percentage (%
): Relative to the parent value.
So for this example:
<div style="width: 200px;">
<div style="width: 50%;"></div>
</div>
The inner div will have a width of 100 pixels.
Viewport height/width (vw
/vh
): Size relative to the viewport (browser window, basically).
Example:
.myDiv {
width: 100vw;
height: 100vh;
background-color: red;
}
Will make an cover the whole browser in red. This is very common among flexboxes as it's naturally responsive.
Emeters (em
) and Root Emeters (rem
): em
is relative to the parent element's font size. rem
will be relative to the html
font-size (mostly 16 pixels). This is very useful if you want to keep an "in mind relativity of sizes" over your project, and not using variables by pre-processors like Sass and Less. Just easier and more intuitive, I guess.
Example:
.myDiv {
font-size: 0.5rem;
}
Font size will be 8 pixels.
Now that you know, choose the right one for the right purpose.
Related Topics
Is There a Business Reason for Striving for Pure CSS Layout
How to Style Bootstrap Col-Lg-* Classes
Why Is Overflow Interacting with Z-Index
Can Visual Studio 2013 Generate CSS Files from .Less Files
How to Override Scoped Styles in Vue Components
How to Select a Div with Class "A" But Not with Class "B"
Svg Data Image as CSS Background
What Is The Effect of Content: "\0020"; Property
Change Font Sizes with Style Sheets for Rstudio Presentation
Does The Order of CSS Stylesheet Definitions Matter
Bootstrap 3.0 Affix with List Changes Width
Pace.Js "Hide Everything But Pace Until The Page Has Fully Loaded" Local Copy
How to Set The Background-Position to an Absolute Distance, Starting from Right
When Using CSS Scale in Firefox, Element Keeps Original Position
How to Tell Org-Mode to Embed My CSS File on HTML Export
CSS Calc Invalid Property Value
How to Create 3X3 Grid Menu in React Native Without 3Rd Party Lib