How to test weird `font-size` definition in different browsers - css

I recently came across this odd definition of font-size: in css, and I flagged it as a bug during a code review.
The way browsers deal with "relative" font sizes through rem is very clear to me, but I've never come across a font size definition as below:
.xx-small {
font-size: xx-small;
}
.x-small {
font-size: x-small;
}
.small {
font-size: small;
}
.medium {
font-size: medium;
}
.large {
font-size: large;
}
.x-large {
font-size: x-large;
}
.xx-large {
font-size: xx-large;
}
And apparently is W3C standard compliant (https://www.w3schools.com/cssref/pr_font_font-size.asp)
The questions:
Is this a reliable way to define the font size across browsers?
What is it based upon? Is it relative to some default value?
Is there a way to test these font size definitions work nicely across different browsers and devices?

Is this a reliable way to define the font size across browsers?
No, since the sizes are based on the browsers' default font sizes (that can be set by the users in the preferences), so they differ between systems.
So you can use these if you want to respect the users' preferences. If you just want to set the same font size in all browsers, use pixels.
What is it based upon? Is it relative to some default value?
Yes, medium is the default font size. Note that this is the same as 1rem, but only if you do NOT set a font size for the html element in your stylesheet.
Is there a way to test these font size definitions work nicely across different browsers and devices?
Well, they do work nicely on all browsers, since this has been around since forever (CSS1, and even before that, in the <font> element), but the defaults may differ, and the relative font sizes may differ between browsers and between quirks/standards mode.
Conclusion: these are not suitable for what you want.

Related

how to fix different font size shown for input fields in chrome and firefox

i am noticing in my web app that the same input form font size, currently set to 17px, reads smaller on chrome compared to firefox. i've attached a screenshot.
is there a more elegant method to resolve this than simply using
/*chrome*/
input {
font-size:17px;
}
/*firefox*/
#-moz-document url-prefix() {
input {
font-size:15px;/*reduce font size to match what is seen in chrome*/
}
}
I don't know what font you're using (or platform (Mac vs Win makes a difference too) , but some browsers render fonts using CLEARTYPE subpixel rendering, some use QUARTZ subpixel rendering (making appearance differ), then others use standard antialias-ing.
A good way around this is to use font-smoothing css in your html or body css:
html {
font-family: /*yourfont*/;
-webkit-font-smoothing: antialiased;
}
Read more about it and see more examples on Max Voltar's website
Also note that using em is preferable to using px for measuring your font. Large font sizes are especially harmed by pixelation if you don't use font smoothing, so in order to keep accessible text especially, it's better to use ems (this way you can still use large type sizes).

Is there a CSS trick to obtain a default font size on all devices

Is there any system independent way in CSS to obtain a standard-text font size from the browser, something that would be suitable for, say, a blog post?
I thought that em would do this, but when I set font-size : 1 em; I get a standard text font size on desktop and something extremely small on mobile.
I understand that sometimes the true DPI might be unknown to the OS. But on a mobile device, shouldn't the browser have some rough idea of how big readable text has to be? And shouldn't there be a way to obtain this information?
If you want the standard system font size, you'd just do this in your CSS:
html {
font-size: 100%;
-webkit-text-size-adjust: 100%;
}
It's a best practice to define your font sizes in relative units (percentage, em) whenever possible so that they scale appropriately according to the target device's built-in settings.
If you're using em's then you first need to define a base value for the font. Ex.:
body { font-size: 16px }
Now 1em = 16px.
Most browsers have a default user-agent font-size of 16px but you shouldn't rely on it. Set the base value then start using em's.

css - font size relative to overridden value

I know that em will set the font-size relative to the parent. What if I wanted to set the font-size relative to the overridden value of a given element?
For instance:
h1 {
font-size: 20px;
}
.smaller h1 {
font-size: (80 percent of the standard h1);
}
Is this possible?
I should mention that I'm using less which might provide some more flexibility.
Not sure about EMs, I suppose you could try it and see what happens.
If you wanted to utilize less's variables, you could do something like this:
#h1-font-size: 20px;
h1 {
font-size: #h1-font-size;
}
.smaller h1 {
font-size: #h1-font-size * .8;
}
You cannot. There is no way to make anything relative to the “overridden value”. You should design your styling in a different way.
And it would be rather odd to set heading size as a fixed amount of pixels for some headings and as relative to an “overriden value”, which is generally unknown and should be expected to be browser-dependent.
But to the extent that you expect browsers to have common defaults, you can pretty much use the HTML5 “expected rendering” rules, since they reflect usual browser practice rather well. According to them, h1 has font-size: 2.00em, so if you wish to set some size to 80% of that, just use 1.6em.

Is using font-size: 100.01% still needed, and for what browsers?

I'm cleaning up some old and terrible css, which includes:
body {
font-size: 100.01%
}
While researching, I found body { font-size: 100.01%; } vs body { font-size: 100%; }?, which explains the reason for setting font-size to 100.01%, but doesn't tell what browsers this 'fix' targets.
Do any modern browsers still face this issue, and if so, which and what versions?
Answer taken from CSS: Getting Into Good Coding Habits by Adrian Senior
This odd 100.01% value for the font
size compensates for several browser
bugs. First, setting a default body
font size in percent (instead of em)
eliminates an IE/Win problem with
growing or shrinking fonts out of
proportion if they are later set in
ems in other elements. Additionally,
some versions of Opera will draw a
default font-size of 100% too small
compared to other browsers. Safari, on
the other hand, has a problem with a
font-size of 101%. The current "best"
suggestion is to use the 100.01% value
for this property.

Ems to Pixel Conversion - Why 62.5% and not 6.25%?

I know that a lot of us are familiar with setting the font size on the body element in our CSS to 62.5%. This means that 1em will equal 10px and helps for keeping things pixel perfect but also allows for scaling of fonts.
So wouldn't that mean that setting it to 6.25% would equate to 1em = 1px? Seems like an even simpler conversion rather than having to mess with decimals...
Thanks guys! I'm quite aware of the em and it's history (design degree), but I'm sure others may find it helpful :)
As far as the 1em = 1px, I don't see how this is undesirable. The em is square, regardless of your units (be it points or pixels) and nobody would set their type at 1px (just like nobody would set printed type at 1pt). Furthermore, even your article concedes that in most digital typefaces, the capital "M" is usually smaller than 1em, and that the em is merely a reflection of the point size (48pt type would render a 48pt by 48pt square for the em, 12pt type would yield 12x12, etc.)
Besides, the reason people would do this would be more for setting dimensions of other elements on the page so that everything scales nicely when the user adjusts their font size. Sure, there will always be the rare few who set their default to something other than 16px, but well worth the price to pay for a pixel perfect layout that scales nicely.
First of all, do not assume that 1 em will equal 10 pixels. An em unit is in direct correlation to the typography being used. If someone has a font size of 16 pixels, then 62.5% is indeed 10 pixels (16 * 0.625 = 10) but this will obviously change when someone has modified their default font size.
Secondly, this is the first I've ever heard of using 62.5% for the base body font-size. I always use a font-size of 76% as based on Sane CSS Typography by Owen Briggs.
Lastly, to answer your question, yes you could use a font-size of 6.25% and then use 12em instead of 1.2em, for example. However, I would highly discourage this methodology. In the world of typograhy, one em is intended to be the width of the capital letter 'M'. This method completely violates that common practice and will seriously confuse anyone that may maintain your CSS in the future.
Arguably, but then you lose control over your scale. Don't forget that headings will typically inherit those same sizes in proportion to their rank (i.e. <h1> will be largest, <h2> slightly smaller). If you want to decrease those elements, you will need to use em values with a lot of decimal placeholders. Imagine <h4> font-size: 0.005em.
Or worse, if you want fonts to be scaled larger, you could potentially be looking at font-size: 40em or something ridiculous.
In short, 1em = 10px is much more practical for the scaled values of fonts. While a 1:1 scale might make sense on paper, it doesn't lend itself that well to sensible and maintainable CSS.
The conversion may be simpler, but an em wouldn't mean what it is supposed to mean.
1em is supposed to be equal to the width if a capitalized "M" in a given font. If the width of the letter M is 1 pixel, your font is going to be unreadable.
http://en.wikipedia.org/wiki/Em_(typography)
The whole "62.5%=10px" thing is fundamentally broken anyway - 62.5% may or may not be 10px depending on the browser, the user's settings, and, especially, the minimum font size setting. So you can't just design in pixels and then "convert" to ems on the assumption that 62.5%=10px, because your design will break all the time. If you want a pixel-perfect design, you have to use pixels as the unit. If you want a flexible design, you need to think about the appropriate units for different elements of the web site - ems for elements which should scale relevant to text size, percentages for elements that should scale relative to window size, and pixels for elements (like images) that shouldn't scale at all.
Anyone who includes font-size: 62.5% in their CSS fundamentally doesn't understand how to design for the web.
Great question.
I see 6.25% as an interesting proposition for adaptive / responsive web design and elastic templates.
In particular font sizing with rem unit's lends it's self to your argument... a 1:1 ratio is just easier.
rem: "root em"... the font size of the root element.
http://www.w3.org/TR/css3-values/
See this rem example from: http://snook.ca/archives/html_and_css/font-size-with-rem#c67739
html { font-size: 62.5%; }
body { font-size: 14px; font-size: 1.4rem; } /* =14px */
h1 { font-size: 24px; font-size: 2.4rem; } /* =24px */
And now with your suggestion...
html { font-size: 6.25%; } /* 1em = 1px if browser has 1em = 16px */
body { font-size: 14px; font-size: 14rem; } /* =14px */
h1 { font-size: 24px; font-size: 24rem; } /* =24px */
... Play with my JSBin example: Testing CSS3 "rem" Units for Elastic Content
A 1:1 em to px ratio should lead to less typos.
REM Notes: With proper CSS resets and body declaring the base font-size in both px and rem your styles degrade gracefully... If rem is supported, and declared after px, it's value is applied. Otherwise the browser falls back to px.
Determining support (especially on mobile) for rem. Please hit this page with any/all browsers/devices you can... http://ahedg.es/w/rem.html
I tried to do the same thing, but ran into an issue of using rems for margins and paddings. Setting font-size to 62.5% avoids these issues.
For example, the following CSS
html {
font-size: 6.25% /* 16px * .0625 => 1px */
}
p {
font-size: 1rem;
margin: 1rem;
}
renders as:
p {
font-size: 1px;
margin: 9px; /* WTF?! */
}
Strange, right? I'm assuming this is caused by some odd conflict with minimum font sizes.
Now, if you use font-size: 62.5% on the other hand, things render as expected:
html {
font-size: 62.5% /* 16px * .625 => 10px */
}
p {
font-size: .1rem;
margin: .1rem;
}
renders as:
p {
font-size: 1px;
margin: 1px;
}
You might find this useful as well. http://pixel2em.kleptomac.com
This provides an online pixel to em converter and you can also do a complete CSS file conversion.
An updated version is available at http://pixelconverter.kleptomac.com
Its an online unit converter for converting pixels, point, em, percentages. This supports conversion from/to any of these units.
For anyone who arrives at this useful post, I would like to share a link for a youtube video (approx.48 min.) about good web typography. It's actual and gives everyone a significant insight that changes the way you set type for the web.
I just made some subtle changes based on this conference video, and the results achieved were perceived, even by our users, as surprising.
The presentation is from Richard Rutter, and the link for the presentation is Richard Rutter | Web Typography

Resources