CSS height in terms of line-height - css

Using CSS, how can I size a box's height in terms of its (or its parent's) line-height?
This would allow easily making regions of text an exact multiple of a number of lines, e.g. to allow showing exactly three lines.
I don't want a javascript solution as those are generally slow and interact poorly with dynamically re-rendered layouts.
Edit: The line-heights in my case are specified in unit-less numbers, as recommended on MDN, so it'd be handy if line-height units weren't required to get this to work.

I'm not sure I've entirely understood the question, but couldn't you just use relative units - ems?
If the line height is, for example, 2ems (or unitless) and you want the total height of the box to be 3 "lines" high (presumably not taking into account padding) then you could set the height to be 6ems. This way, the line height is flexible based on your base unit (font size) and the height will also be fluid using the same base unit.
Here's an example:
.myBox {
line-height: 2; // this will be twice the value of the font-size
height: 6ems; // equivalent to 3 lines high
To achieve the same thing in SASS you could write:
$lineheight: 2; // this value can be unitless or a unit like ems, px or percentage
$numberoflines: 3;
.myBox {
line-height: $lineheight;
height: $lineheight * $numberoflines;
}
This would have the flexibility for you to move your variables into a settings file so that you (or someone else) can easily alter the values without having to find all the selectors that use the variables.

Related

What is the CSS height of a line of text? It doesn’t seem to be 1em

Look at this fiddle:
function toggle(event) {
var element = document.getElementById('changeme');
element.innerText = element.innerText ? '' : 'some text';
}
button = document.getElementById('button');
button.onclick = toggle;
#changeme {
min-height: 1em;
outline: 1px solid red;
}
<input id=button type=button value='Toggle content'>
<p id=changeme>dynamically changing content
<p>This should not move (up and down)
I have a text element there (it is a <p>, but could be a <div>) which I sometimes set to be empty. (Use the button to toggle between empty and some content.)
What height does this element with a line of text have? I want a generic answer so I can set its min-height to that value so that the element doesn’t collapse when empty. I thought 1em is what I’m looking for, but 1em seems to be the size of letters like “W” without the space below the baseline for letters like “g”.
I’m looking for a neat trick to give the possibly empty element a constant height, without putting into it. (At the moment I use 1.5em.)
The exact calculation of heights of lines is complicated (see CSS 2.1 section 10 Visual formatting model details), but in a simple case like this, the height is determined by the line-height value when the element is not empty. When it is empty, the height would be empty, but the min-height setting forces the height to the font size (1em).
The initial value of line-height is browser-dependent, and it is expected to depend on the font, too. The default is in practice always larger than the font size. This explains what happens here.
To keep the height constant, it is best to explicitly set its line-height and to set min-height to the same value, e.g.
#changeme {
line-height: 1.25em;
min-height: 1.25em;
}
Note: The em unit means the size of the font, which is by definition the height of the font. This is a reference quantity and does not normally correspond to the height (or width) of any letter; the height of most letters is smaller than the font size (though this varies by letter and by font).
The default line-height is normal (valid CSS value for that property). You can’t set min-height to that value unfortunately.
Apart from normal, line-height can have fixed length values (which are of no interest here) or factors which work relative to the font-size. The numeric equivalent of normal is not just browser dependent, interestingly. It commonly ranges between 1.0 and 1.5 (without unit) for different font faces and font-sizes!
So set the min-height to something beautiful between 1em and 1.5em. Then set line-height to the same value to make sure it is actually the same.
Note that for line-height a value with em-unit or unit less mean the same: the former is a fixed length relative to the current font-size and the latter is a factor relative ... to the font-size.

how to always have a 40-70 characters wide page? scren, mobile, print

How to balance a decent font-size regardless of the media (small vertical phone screen or full page printing) and a comfortable 40~80 char per lines using the current CSS best practices?
I think I know the drill for regular screens, set body's font-size to some percentage, mark your main content element with width being 70em and min-width 40em...
but then, i know nothing about the accepted standards for mobile and print for this problem.
All my search attempts take me to (now) futile discussions from 2002~2011 about the benefits of [em,pixel,%,pt] over the other...
body {
min-width: 16em;
min-width: 40ch;
max-width: 28em;
max-width: 70ch;
}
Use the ch unit for “width of a character”, and back it up with a setting in em units, with a numeric value that is about 40% of the desired number of characters.
The em unit means the size of the font. For texts in Latin letters, this is about 40% of the average width of characters, maybe a little more.
The ch unit means the width of the digit 0. It is generally the best available approximation for “average width of characters” in CSS. It is supported by modern browsers, but for less than modern browsers, set first the width in em units.
This answers the question in the heading of your message. The body of the message is vague and does not pose a question that could be addressed constructively.
Setting a page width relative to the character size would only make sense with a monotype font. Consider these two strings "iiiiiiiiii" and "WWWWWWWWWW" - both consist of 10 characters, but clearly require a container of different width!
You could play around with ex/em/ch units, but they would not give you full control to accomplish what you're trying to do. Also: ch isn't even fully supported!
Fiddled sample

Be careful with CSS em units when taking advantage of rules of specificity?

I'm having trouble writing maintainable CSS code when font size is specified with EM units as opposed to PX units. I've been accustomed to writing CSS code like this:
body {font-size: 12px;}
body .sidebar {font-size:11px;}
body .sidebar .loadmore {font-size:10px;}
body .sidebar .warning {font-size:13px;}
The idea is that on many pages through out the site, there's a lot of text that should have 12 pixel font-size. I take advantage of the rules of specificity to override the 12px font size in special areas of the site.
Let's say I rewrote the above code as:
body {font-size: 12em;}
body .sidebar {font-size:11em;}
body .sidebar .loadmore {font-size:10em;}
body .sidebar .warning {font-size:13em;}
If I replaced px with em in the code above, my understanding is that I lose the advantage of rules of specificity. Line 3 of the code would be interpreted as " 10 em of 11 em of 12 em" which is not at all the same meaning as " override all previous rules and use 10 em of (what is the default?)". Is what I've stated correct?
edit If what I've said is correct, then how does one write a font size rule such as "use font size X for all elements, but use font size Y on side bars"?
John the specificity that you are talking about will occur in the way that you have stated. A reference as to why can be seen: http://kyleschaeffer.com/best-practices/css-font-size-em-vs-px-vs-pt-vs/.
Edit As a reply to your edit please consider what jnylen posted in the comments of your original post.
A font size of 12 em as you have listed for body {font-size: 12em;} is going to scale the default font size to 12x it's current size. If you would like to use em's you need to consider at what scale rate you wish to use them and do the math. If you want to set fixed sizes with nested statements you need to stick to fixed attributes (pixels). The advantage of em's as stated in the article that I linked is that you can set a default size, say 12 px and then use em to scale them. For example in mobile based websites.
Yes, em values "multiply" together when the elements they apply to are nested. It's not necessarily a specificity issue - if you specified the rules individually for .loadmore and .sidebar you would see the same issue, since .sidebar contains .loadmore.
Here's an example of a way to work with this: http://jsfiddle.net/PJWrW/
I usually use either px or percentage units for font sizes, to make it explicit that I'm setting an absolute font size or modifying the parent font size.
I sometimes use em units for defining dimensions like paddings and widths, since an em unit is basically the width of a letter at the current font size.
As far as I know, if you're using em's for font size, there is no one way to set a standard font size for elements in a left hand side bar (or elsewhere for that matter) whilst setting the specific size of some elements in that side bar. If you use em's you'll have to specify the sizes of all elements in that side bar.
Personally I usually use px for font sizing, simply because you can set a standard font size for certain elements as you say in your post.
The only convincing arguments I have heard for using em over px regard scaling issues that are no longer really a problem (IE6 doesn't allow you to change font size manually if you use px but most people stopped hacking for IE6 a while back) The only real situation I can see using em as an advantage is if you want to implement functionality whereby the user can change the font size via JavaScript like here

When to use %, px, em, etc...?

I am pretty new to CSS, and would like to know if there is/are some sort of rule/rules of thumb for determining when to use different units to define layouts. Currently I have everything defined in %, because I thought that'd be good for window resizing. That is not the case, text starts to overflow, images get screwed around and so on.
Any help will be appreceiated.
Typically, I use the following
Layouts - Pixel (Unless something needs to be a % width/height)
Fonts - Pixel (Sometimes % for accessibility, but it is a nightmare to maintain)
Generally speaking, you can use pixels most of the time. The font issue is a more complex one. For instance, if you want the "increase font-size" features to work within a browser without resizing the rest of the page, you need to use %'s. However, when using % font sizes, a child element always inherits the parents font-size, so you get the following:
body { font-size:87%; }
h1 { font-size:87%; }
This will mean that the h1 is actually 87% of 87%. This can be quite annoying. As you end up with percentages > 100%. It gets very thick fast, and is best avoided.
I'm not sure if em's work in the same way, I've never looked into them in great detail.
Using percentages to have a layout work in different size viewports is a very advanced technique, and is often done dynamically using javascript. Until you are more familiar with CSS, and can look at working percentage based layouts and understand enough to replicate it, you are better sticking to PX.
If you are going the javascript route it is really quite simple. For a start use jQuery as it makes resizing your layout a breeze compared to trying to do it with native javascript. Then $(window).height(); gives you the height of the viewport; $(window).width(); gives you the width. You set a default px width for your container, and then use percentages for all other block level elements (containers, within the container, sidebar, main etc) and do this:
function percentagize() {
var height = $(window).height()-100;
var width = $(window).width()-20;
$("div#container").css({
'height' : height+'px',
'width' : width+'px',
'margin': '0 auto'
});
}
$(document).ready(function() {
percentagize();
$(window).bind('resize','percentagize');
})
This should give you an idea: http://w3schools.com/cssref/css_units.asp
% is not explained properly on that page, but it means x% of the containing block.
You should use ems for fonts so that they are always relatively sized... by default they are 1 em or 16px... you can set play with this by setting body { font-size: 75% } which makes 1em the equivalent of 12px The PxtoEm calculator is great. From here you can do things like
h1 { font-size: 3em }
p { font-size: 1em }
now no matter what you set that body font size to the h1 tag will always be 3 times larger than a paragraph. It gives more flexibility and keeps yout type hierachy proportional.
For layouts it really depends on the layout type... for the classic fixed with central column then use pixels.... for fluid or adaptive layouts then use percentages (or a mix of fixed width, i.e. left hand nav bar andpercentages)
Use em as much as possible, since this is the most maintainable. The em unit depends on the font size of the current element, so if you change the base font size, em-units scale along.
Use px in screen style sheets when you need a fixed size. Typically you would specify the size of the base font in pixels. Image sizes should also be specified in px, since an image should not scale up or down just because you change a font - it will just make the image blurry. Also border thicknesses should probably be specified in pixels, since you don't want it to depend on font sizes.
Use pt, pc, in, cm, mm only in print media style sheets. You probably wouldn't to mix metric and imperial in the same style sheet, so decide on either in or cm/mm.
% is tricky since it means something different depending on the property. For font-sizes, 100% = 1em, so its just a matter of preference if you like % or em. (I prefer em for font sizes, since % have different meaning in other contexts.) It is not affected by window scaling though. The font size doesn't scale with the window size, and neither does em or % units.
For width and height on boxes, % refers to percent of the size of the parent box, which for the root element is depending on the window size. This is much less useful than it sounds! For example if you have a flow of text without any specified width, the lines will become too long to read comfortably. If you specify the width of the text box in em's you can give it a nice readable line-length, on any screen. But if you specify the width in % it will scale with the size of the window, which means it can still be too long on some screens and to short on others. Scaling with the window size sounds good in theory, but is rarely what you want.

Should I define CSS margins in pixels or ems? Why? When?

We have a CSS file with some rules similar to the following:
.directory-result ul
{
margin-top: 20px;
width: 60em;
}
.about-text
{
margin-top: 1em;
margin-bottom: 1em;
}
Everything is working ok, but we're wondering specifically about the inconsistencies between the margin-top values. One is 20px and the other is 1em.
Which is the best one to go with? What are the points I should consider when deciding which to use? Thanks.
em units are used for better scalability of the page when the size of the elements depend on the page's scale. It's especially important for old browsers (e.g. IE6) and mobile platforms.
px units are used for absolute values, while em is relative to the font size of the particular element.
1em means one font-line, e.g. you have a box with font-size 12px that means that 1em will be equal to 12px
Also, using px seems easier because you know the exact value, but em units inherit the value of their container.
<p>Text</p>
<div class="box">
<p>Lorem</p>
</div>
p {
font-size: 1.2em;
}
.box {
font-size: 1.2em;
}
In this case, the first <p> will have font-size equal to the basic font-size * 1.2, and the second <p> will display with font-size * 1.2 * 1.2.
They're simply two different ways of measuring. Em is linked to the font size (traditionally, 1em is roughly the width of the letter M in a given typeface), while px is pixels.
If you build everything using em, everything will scale accordingly when the user adjusts their font size (e.g. using IE's Page > Text Size menu). It also makes it easier to work to a vertical rhythm.
Pixels are better when you want to build something "pixel-perfect". Be aware that a CSS pixel doesn't always equal a screen pixel - mainly because modern browsers and mobile devices allow for zooming. This isn't usually a problem though because the entire page is scaled accordingly.
Whatever you do, make sure you're consistent throughout - it makes life much easier later on.
The ems unit is relative to the current font size in the browser. So if the user increases the font size*, or if you change an element’s font size in the CSS, the margins should still look “right” in proportion to the text.
*(This ceases to matter if the user zooms the page instead of increasing the text size (as is the default in Firefox and Chrome now, and is an option in IE).
If you're using a margin to position something a set number of pixels away from something else, then you should obviously stick with pixels.
Also here is a very good in depth tutorial:
px – em – % – pt – keyword
In this example directory-result ul represents a block - some sort of list/menu where pixel dimensions are quite important. We can’t always rely on em which defines the text size, because if we need 20px space due to some background image – well, we need 20px, no compromises.
Note that you can't create and save the image i.e. 10em wide, therefore I see no reason why should I use different units on a web page. It just creates confusion and later on it is very difficult to maintain the layout.
There is a one place though, where using em is advisable – I’m talking about text blocks. I’m guessing in your code about-text is placed within other text where adding top/bottom margin of 1em (height of text) makes sense. It’s like in any text editor (i.e. line spacing in MS Word) – text looks best when spacing between lines is defined by multiplying the height of text
So in my opinion – everywhere where you deal with design and you use images by default measured in pixels – usepixels for all padding/margin.
Everywhere where you deal with text inside a text block, and you want to add even spacing between the text nodes – useem.

Resources