I'm writing a webpage where I want to use one font for normal text and another for code, so I have the following CSS:
body {
font-family:Verdana, sans-serif;
font-size:100%;
}
code, pre {
font-family:Consolas, monospace;
}
This works except that the Consolas text comes out considerably smaller than the Verdana text. I can sort of fix it by adding font-size:1.2em; to the code, pre settings, but that feels like a hack, particularly since the number isn't derived from anything other than trial and error.
Is this something that happens to be a quirk of the particular fonts involved, or am I doing something wrong?
Alas this is because Consolas has a smaller x-height than Verdana. You can indeed "sort of fix it" by using a larger font size, but if the user doesn't have Consolas the page will fall back to the default monospace font, which will appear too large.
The proper solution lies in CSS3's font-size-adjust. Alas it isn't very well supported at all; as far as I know then only mainstream browser to support it is Firefox.
When you define a font-size in CSS, what you're doing is defining a concept known as an "em square" or "em box" even if you aren't using em units.
The em square gets its name because in the days of metal press printing, each letter was put in its own metal "box" and in traditional typefaces the uppercase Latin M typically filled the width of an entire box, defining the sizes for the entire family. Hence, the em box. Although the original em box was defined by the width of the M, the em unit itself refers to the maximum allowable height of a character. This was just a result of the box created for the uppercase M.
In short, all of the letters in a font family need to fit inside the em box, but they don't have to take up the whole thing. So when you set an em box with a given width, all you are doing is defining the maximum allowable space for the lettering to be placed in. From there, the typeface designer's choices in kerning, geometry, ratios, etc. will determine just how much of that box their lettering will take up. This is why you are having to use two different font sizes to make the lettering appear to be the same.
The font sizes are the same. But in Verdana, characters are generally taller than in most fonts (in the same size). For example, “H” in Verdana is taller than “H” in Consolas. This is a font design issue.
The solution is to use matching fonts. If you really want to use Verdana for copy text, Lucida Console is probably the best choice for a monospace font. If you want to use Consolas as monospace font (fine!), then use another “C font” for copy text, such as Cambria (serif font) or Calibri or Candara (sans-serif fonts).
Although the font sizes are the same here (on browsers that I tested), it is best to make sure of it, since browsers often apply something like font-size: 90% on code and pre and some other elements. The conditions for this to happen are somewhat obscure, and explicitly setting font family to something else than the generic monospace seems to prevent that on modern browsers. But it’s still a useful precaution to set
pre, code, samp, kbd, tt { font-size: 100%; }
The first line is in Verdana and the second is Consolas. Both 12pt. Consolas is slightly shorter, but not so much as in 20% difference. Check the parent elements of your pre, since em font sizes are cummulative - e.g. if body is 0.9em and pre is 0.9em, the result would be 12pt * 0.9 * 0.9 = 9.72pt
If you are trying to match only the height but ignoring the width, then 1.16em looks about right, and it is really the quirk of the font and you are not doing anything wrong.
Edit
Looks like you are doing nothing wrong =) Left is from fiddler, right is from WordPad
Related
I have this string line which has for example exactly 100 characters.
I want to set it's font-size such that the line, starting at exactly pixel 0 of the web page, would end at the final pixel exactly.
The font is monospace so I assume so i assume i'm aiming towards setting the font-size to 1/100 of the window size.
PROBLEM:
* font-size refers to the height of the font! should i not expect a monospace font to be square ratio regard width and height?
An attempt:
<body>
<span>
123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_
</span>
</body>
body {
margin: 0;
font-family: monospace;
}
span {
font-size: 1vw;
}
this did not result in the wanted outcome. However, setting the font-size: 1.82 did result in it. But it's a bit shaky I guess to use such a random ratio setting and I was interested if there's a better way to go about it.
Step one is to not use font-family: monospace, because you are guaranteed to get different fonts on different browser/OS combinations. So use a real font, which leads to...
Step 2: use a real fond that you load as a webfont from your own server, so you control the exact version of the font people will be using. Because merely using a font by name in the assumption that font exists on everyone's OS, or even assuming something like google fonts will always only send one exact version, is guaranteed to fail, too.
And then step 3: look up what the em size as well as glyph width is that your chosen monospace font use, so that you can base your font-size on values that you know are applicable for every user, because you've ensured exactly which font will be used to style your content.
In my Web application I am using google font 'Lato'.Now Requirement is I should use 'Roboto' font for my web application.
I replace Lato font with Roboto but This reflect major change in terms of spacing in whole Web application.
Where I am doing mistake?
I cant share whole page screenshot.
Original with Lato
Disturb with Roboto
Every font-family have own letter spacing and line-height. Now you need yo set re-structure your letter spacing and line-height!
Definitely in specifiing sizes for elements. If you need to translate the site to other language, you meet the same problems. If you change some text you have the same problems. And so on.
Everyting depending on text should not have explicit sizes (width at least). So when you change te text or the font, element just enlarges or shrinks without negative effect in most cases.
The only exception is placing some set of elements inside of one raw or limiting their max-width. These can cause problems if more text appears or new font is wider. Anyway, it's not good if the whole site consists of such elements. And Roboto is not wider than Lato, so it's not your case.
Is it possible to view your webpage ?
You can also change the spacing between letters using the css property "letter-spacing"
h1 {
letter-spacing: 2px;
}
h2 {
letter-spacing: 5px;
}
<h1>SAMPLE</h1>
<h2>SAMPLE</h2>
I'm working on a customers website and they insist I use EXACTLY the same font they had before. The previous (drupal) site had a Cascading Style Sheet callout:
body { font: 76%/170% Arial,sans-serif; }
As I understand it, that correlates to a Websafe font, where the browser looks to the users operating system and tries to match a font, in this case with Arial first and if not, Sans-serif. Nothing is downloaded from the web.
Based on my testing, the native font size seems to be 16px, and the native line height seems to be 20px. The fractional percentage numbers indicate font-size (as a ratio from 16px) and line-height (as a ratio from the calculated font-size). In this case font-size observed (via WhatFont) = 12.1667px and line-height of 20.6833px
The combination shown above seems to have been a very common Drupal selection, at least it would appear so from my Google searches on this topic.
This whole thing seems way odd. I would think...
body{ font-family: Arial, sans-serif; font-size:12px line-height:21px}
makes more sense, communicates the intent clearly for those who follow, etc. Why would anybody do the % / % system for identifying font-size? Is that native height ALWAYS 16px? This feels like obfuscation for nothing. Is there a significant advantage anywhere? Why would anybody code like this? What am I missing here?
Consider the following example: (live demo here)
HTML:
<div>Hello World</div>
CSS:
div {
font-family: monospace;
font-size: 1em;
}
JS:
$(function() {
alert($("div").css("font-size"));
});
In Firefox, the font size is 16px, while in IE8 the font size is 13px.
Why ?
(I tried to change the font-family to Arial, and both Firefox and IE8 say 16px.)
Each browser is a different product by a different company, they program their product differently, and font size is one of them. From this link, 1 em is equal to the current font size, which maybe different for different browsers, also user can change it, i have changed the text size to largest in IE, and the font size is now 21.33 px.
Using em means it is dependent on a lot of things, use %age for consistency.
1 em will be different for different browsers (depending on their default or of user has changed it). For example you said IE has font size of 13 px and firefox has 16 px, when i checked, firefox had 13 px and IE has 16 px, which was changed to 21.33 px when i changed the text size to largest (view -> text size)
Because em is a relative unit. An em is equal to the current font-size. If the font-size of the document is 16pt, 1 em is equal to 16pt.
MSIE seems to think that monospace doesn't need to be as big as Arial (in pixels) to be readable. For example, 12pt Times New Roman is about a big as 10pt Arial.
If you want fixed font sizes, use font-size: 16pt.
Because its an implementation choice. For instance, firefox has a configuration option to set the default font face and sizes for Sans, Serif and Monospace.
Because Firefox builders messed up in how they coded "monospace". To get "monospace" to work properly in Firefox you need to type "monospace,;" not "monospace;".
I kid you not. It is that simple of a problem.
Check it out for yourself. Change your css style to have a comma after the "monospace" and before the semicolon and watch how dramatically it instantly changes the way that text appears.
Conventional belief was that you needed to have it type "monospace, monospace;" to work but they were wrong. That second "monospace" was meaningless, only the comma mattered. Which actually means you could have put anything there and it would have worked. So if you wanted to leave a funny easter egg in your code you could do something like "monospace, hellofromthecoder;" and it would function perfectly fine without that extra bit of text changing anything.
I've been working in HTML/CSS for years, but I'd like to clarify something about setting font sizes. What is the best format to set your font?
Typically, i've been setting with a font-size in a percentage, and then using em to change it up or down from there.
Is this the most standard way to do it? I've seen fonts declared in pixels, points, with relative keywords like "larger" or "smaller" I've seen it set as percentages, etc.
So what's the most standard? Is the most standard the best? any research to back it up?
Thanks,
What I learnt at school is the following:
Set font-size in body with percent to 62.5%:
body {
font-size: 62.5%;
}
Then you can use em in the same sense as you would use pixels, except you divide by 10.
For example:
h1 {
font-size: 1.4em; /* 14px */
}
We learnt to use em for 'elastic' layouts. If you specify your font-size in em, the text will keep its proportions after a user zooms in or out.
Then again, I see people use px or other declarations for fonts all the time; as far as I know they're all standard. I guess it just comes down to creating the best user-experience.
They're all standard. Use what works for you.
You should set the font-size in the body tag to 100%. That way, people who visited your site will see the text at the right size for what they have set in their browser. For instance, people with low vision may set the text size larger. If your font-size is set to 100%, they should see it exactly as desired.
After that, you could set the sizes on your h1, h2, p, etc. with % or em.
I generally set html to 10px, then use font-size: 100% on the body. You can then use the px/em ratio 14px/1.4em on elements. The only thing I run into is then if I nest base elements, the font gets all funky, and you have to specify font-size on all nested elements.
Example: if I have p, section, article, div{font-size: 1.6em;}, any time I have p, section, article, div nested, the font becomes proportional to the container. So the 1.6em that was originally 16px is now 1.6em of 16px (not 10px) or 25.6px. You'd have to re-scale the text to 0.625em (or 16px/25.6px = 0.625em). You will have more control over consistency across browsers, but it may require a bit more effort from you.
Some may be asking, "Why go through all this hassle?" That is a good question. Here is the answer: Responsiveness. That, and I work for a company that needs to be 508 compliant. That includes ultimate control over starting font sizes. I can't rely on assuming that the end user has "medium" or 16pt font selected, because the law clearly states it must be X or Y for high contrast, etc..
There is someone telling it isnt a good thing and can break your layout, see it: http://filamentgroup.com/lab/how_we_learned_to_leave_body_font_size_alone/