Keep a ration between body font-size and Headings - css

Is it possible to achieve this in CSS:
I'd like to keep a Typographic hierarchy in my document. I'd like to change dynamically the size (keeping a ratio) of my Headings depending on the font-size of my body in px.
Let's say I want my Headings and body size to keep in sync between each other and change depending on body's font-size in px.
If the size of body's font-size increases/decreases the Headings h1,h2,h3 also follow and keep a ratio.
is this something doable in CSS alone?

The question asks how to make sure all font sizes in a document change proportional to the size set in the body element.
If you set a definite size in the body and thereafter use the em unit to size your fonts you will find that everything changes proportionally.
BUT be aware that the em unit is itself a relative unit. That is actually probably what you want, but just make sure. Consider this example. The div which is a child of the h1 element has a font size proportional to the h1's font size which is itself proportional to its parent's (body element) font size. The div which is not a child of h1, only of the body element has a font size proportional only to the body's font size. See snippet below.
There is another unit, rem (root em) which is set in the html element of the document. Most browsers set the basic font size o 16px there and thus that is what 1rem is. You can change that and set for example our body font size in terms of rem. The main reason for doing that would be to acknowledge that the user can change the basic setting in their browser, perhaps they hope that all text will then become automatically bigger, so it is considerate to set your body font size in terms of rem.
To make the arithmetic of this easier, set
html {
font-size: 62.5%;
}
Your rem will then be 10px (or a proportion of whatever the user has set). Then to get your body font-size to say 24px (for most users or proportional to the users browser setting) by
body {
font-size: 2.4rem;
}
From there on in use em or rem depending on what suits your case at particular points in the document and everything will be proportional, and as a bonus, give the user better accessibility.
Here's a snippet to illustrate:
html :
font-size: 62.5%;/* 10px on most browsers */
}
body {
font-size: 2.4rem;/* 24px; */
}
h1 {
font-size: 2em;/* 48px; */
}
div {
font-size: 1.5em;
}
<html>
<body>
This is inside the body only
<h1>This is h1 inside body and with font-size 2em
<div>This is div inside h1 and with font-size 1.5em</div>
</h1>
<h1>This is h1 inside body and with font-size 2em
</h1>
<div>This is div outside h1 and with font-size 1.5em</div>
</body>
</html>

Related

Sizes in rem are not scaled when I change body font-size [duplicate]

In website source, I have sometimes seen developers use the rem unit. Is it similar to em? I tried it to see what it actually does, but what is it relative to?
Demo
HTML
<div>Hello <p>World</p></div>
CSS
div {
font-size: 1.4rem;
}
div p {
font-size: 1.4rem;
}
EMs are relative to their parent's font size
REMs are relative to a base font-size
This is important when intermediate containers change font sizes. Child elements with EMs will be affected, those using REMs will not.
The unit rem (root em) stands for the font size of the root element. In an HTML document, the root element is the html element.
While em is relative to the font-size of its direct or nearest parent, rem is only relative to the html (root) font-size.
em gives the ability to control an area of a design. As in, scale the type in that specific area relatively.
rem gives the ability to scale type across the entire page easily.
Basically em is relative to the nearest parent in CSS, while is rem is relative to the parent of the page which is usually the html tag...
You see the difference clearly if you run the css below and how the parent is effecting it:
html {
font-size: 16px;
}
.lg-font {
font-size: 30px;
}
p.rem {
font-size: 1rem;
}
p.em {
font-size: 1em;
}
<div class="lg-font">
<p class="em">Hello World - em</p>
</div>
<div class="lg-font">
<p class="rem">Hello World - rem</p>
</div>
Summary:
rem : a CSS unit which is relative to the font size of the html element.
em : a CSS unit which is relative to the font size of the parent element.
Example:
.element {
width: 10rem;
height: 10rem;
background-color: green;
font-size: 5px;
}
.innerElement {
width: 10em;
height: 10em;
background-color: blue;
}
<div class="element">
<div class="innerElement"></div>
</div>
In the above example the green square is 160px by 160 px (unless you don't have browser default at 16px). This is because the browser default of the html element font-size is 16px and 10rem * 16px = 160.
The inside square is 10em big. Because its parent element is 5px the square is 5em * 10px = 50px.
How is this usefull:
By setting all units to rem have the following advantages:
We can scale our whole application with one CSS media query, in this media query we can specify the font size. By altering the font size all the elements which have the unit rem will scale accordingly.
When users are not using the default browser font-size of 16px our application will scale with the selected font size of the user.
Here is an example. divs sized with rem change as we change the font-size of the html element. Whereas those sized with em only change as we change the font-size of the div.
$(function() {
var htmlSize = $('input#html');
htmlSize.change(function() {
$('html').css('font-size', htmlSize.val() + 'px');
});
var divSize = $('input#div');
divSize.change(function() {
$('div').css('font-size', divSize.val() + 'px');
});
});
* {
float: left;
font-size: 20px;
margin:2px;
}
label {
clear:both;
}
div {
border: thin solid black;
}
div.rem1 {
width:4rem;
height: 4rem;
clear: both;
}
div.rem2 {
width: 3rem;
height: 3rem;
}
div.em1 {
width: 4em;
height: 4em;
clear: both;
}
div.em2 {
width: 3em;
height: 3em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Change html font-size
<input id="html" type='number' value="20" min="18" max="30" />
</label>
<div class="rem rem1">rem</div>
<div class="rem rem2">rem</div>
<label>Change div font-size
<input id="div" type='number' value="20" min="18" max="30" />
</label>
<div class="em em1">em</div>
<div class="em em2">em</div>
In em relative unit the font size is measured on the base of nearest parent font size but if the font size is not defined for any of parent elements then by default font size will be defined according to the root html element
The rem relative unit is calculated only by root html element, thus, the font size of the parent element does not affect it
Just some example CSS to show how rem will work, notice the root font-size is set using px via html tag
html, body { font-size: 16px; }
div { font-size: 18px; }
p { font-size: 1rem; }
And the corresponding HTML:
<div>
<p>Lorem Ipsum</p>
</div>
Because the <p> tag is set to 1rem it ignores the parent div’s font-size of 18px. However, if we instead set the font-size to 1em, the paragraph would inherit the 18px font-size of its parent element. I know this example isn’t particularly useful, but hopefully it can help illustrate the difference between em and rem.
em and rem are font-based relative units and it's different to use ems for fonts or for length, so both ems and rems are font-based but the difference between them is that ems use the parent or the current element as a reference while rems use the root font size as the reference.
If we want to use ems for font-sizes then the reference is simply the parents computed font-size similar to what happens with percentages.
In the bellow example, a three em font-size on the header child element results in 72 pixels simply because that's three times parent font size which is (150/100)*16=24px. Now for length, it's just a bit different. The 2em padding on the header, since it's a length measurement, uses the font size of the current element as a reference and we already know that's 24 pixels, so 2em will result in 48-pixel padding, got it? It's a subtle difference, but an important one. When you use em if it's for fonts the reference is the parent and for length, the reference is the current element.
html,body{
font-size:16px;
width:80vw;
}
header{
font-size:150%;
padding 2em;
margin-bottom:10rem;
height:90vh;
widht 1000px;
}
header-child{
font-size:3em;
padding:10%;
}
About the rem, it actually works the same way for both font sizes and lengths because it always just uses the root font size as a reference. This means that the 10 rem padding that we have here will result in 160 pixels because the root font size is 16.
CSS measuring units: em, rem both relative to their parents front size. The size will change depending on its Parents font size.
Example:
rem : 100% of the Root Element font size.
em : 100% of the Parent font size.
EM is relative to the font-size of its direct or nearest parent, and REM is only relative to the html (root) font-size.
The main problem comes in using the 'em' and 'rem' when there is a parent font size....
**Here is the example:
suppose I want the font size of 2em for body of my website so we include 2em font size in body which is known as parent font size ...
and again for my H1 i want a size of 5.6 something and after we hit save and head back to our website and refresh then we will notice that our H1 has now become more than 5.6 .
this happens because the fonts gets inherited and added on top of whatever it got from its parent....(this problems are going to be happened when we are using em and percentages)
so, to get out of this type of problems the programmers use rem.

Why am I getting different actual image sizes using em and rem units in CSS [duplicate]

In website source, I have sometimes seen developers use the rem unit. Is it similar to em? I tried it to see what it actually does, but what is it relative to?
Demo
HTML
<div>Hello <p>World</p></div>
CSS
div {
font-size: 1.4rem;
}
div p {
font-size: 1.4rem;
}
EMs are relative to their parent's font size
REMs are relative to a base font-size
This is important when intermediate containers change font sizes. Child elements with EMs will be affected, those using REMs will not.
The unit rem (root em) stands for the font size of the root element. In an HTML document, the root element is the html element.
While em is relative to the font-size of its direct or nearest parent, rem is only relative to the html (root) font-size.
em gives the ability to control an area of a design. As in, scale the type in that specific area relatively.
rem gives the ability to scale type across the entire page easily.
Basically em is relative to the nearest parent in CSS, while is rem is relative to the parent of the page which is usually the html tag...
You see the difference clearly if you run the css below and how the parent is effecting it:
html {
font-size: 16px;
}
.lg-font {
font-size: 30px;
}
p.rem {
font-size: 1rem;
}
p.em {
font-size: 1em;
}
<div class="lg-font">
<p class="em">Hello World - em</p>
</div>
<div class="lg-font">
<p class="rem">Hello World - rem</p>
</div>
Summary:
rem : a CSS unit which is relative to the font size of the html element.
em : a CSS unit which is relative to the font size of the parent element.
Example:
.element {
width: 10rem;
height: 10rem;
background-color: green;
font-size: 5px;
}
.innerElement {
width: 10em;
height: 10em;
background-color: blue;
}
<div class="element">
<div class="innerElement"></div>
</div>
In the above example the green square is 160px by 160 px (unless you don't have browser default at 16px). This is because the browser default of the html element font-size is 16px and 10rem * 16px = 160.
The inside square is 10em big. Because its parent element is 5px the square is 5em * 10px = 50px.
How is this usefull:
By setting all units to rem have the following advantages:
We can scale our whole application with one CSS media query, in this media query we can specify the font size. By altering the font size all the elements which have the unit rem will scale accordingly.
When users are not using the default browser font-size of 16px our application will scale with the selected font size of the user.
Here is an example. divs sized with rem change as we change the font-size of the html element. Whereas those sized with em only change as we change the font-size of the div.
$(function() {
var htmlSize = $('input#html');
htmlSize.change(function() {
$('html').css('font-size', htmlSize.val() + 'px');
});
var divSize = $('input#div');
divSize.change(function() {
$('div').css('font-size', divSize.val() + 'px');
});
});
* {
float: left;
font-size: 20px;
margin:2px;
}
label {
clear:both;
}
div {
border: thin solid black;
}
div.rem1 {
width:4rem;
height: 4rem;
clear: both;
}
div.rem2 {
width: 3rem;
height: 3rem;
}
div.em1 {
width: 4em;
height: 4em;
clear: both;
}
div.em2 {
width: 3em;
height: 3em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Change html font-size
<input id="html" type='number' value="20" min="18" max="30" />
</label>
<div class="rem rem1">rem</div>
<div class="rem rem2">rem</div>
<label>Change div font-size
<input id="div" type='number' value="20" min="18" max="30" />
</label>
<div class="em em1">em</div>
<div class="em em2">em</div>
In em relative unit the font size is measured on the base of nearest parent font size but if the font size is not defined for any of parent elements then by default font size will be defined according to the root html element
The rem relative unit is calculated only by root html element, thus, the font size of the parent element does not affect it
Just some example CSS to show how rem will work, notice the root font-size is set using px via html tag
html, body { font-size: 16px; }
div { font-size: 18px; }
p { font-size: 1rem; }
And the corresponding HTML:
<div>
<p>Lorem Ipsum</p>
</div>
Because the <p> tag is set to 1rem it ignores the parent div’s font-size of 18px. However, if we instead set the font-size to 1em, the paragraph would inherit the 18px font-size of its parent element. I know this example isn’t particularly useful, but hopefully it can help illustrate the difference between em and rem.
em and rem are font-based relative units and it's different to use ems for fonts or for length, so both ems and rems are font-based but the difference between them is that ems use the parent or the current element as a reference while rems use the root font size as the reference.
If we want to use ems for font-sizes then the reference is simply the parents computed font-size similar to what happens with percentages.
In the bellow example, a three em font-size on the header child element results in 72 pixels simply because that's three times parent font size which is (150/100)*16=24px. Now for length, it's just a bit different. The 2em padding on the header, since it's a length measurement, uses the font size of the current element as a reference and we already know that's 24 pixels, so 2em will result in 48-pixel padding, got it? It's a subtle difference, but an important one. When you use em if it's for fonts the reference is the parent and for length, the reference is the current element.
html,body{
font-size:16px;
width:80vw;
}
header{
font-size:150%;
padding 2em;
margin-bottom:10rem;
height:90vh;
widht 1000px;
}
header-child{
font-size:3em;
padding:10%;
}
About the rem, it actually works the same way for both font sizes and lengths because it always just uses the root font size as a reference. This means that the 10 rem padding that we have here will result in 160 pixels because the root font size is 16.
CSS measuring units: em, rem both relative to their parents front size. The size will change depending on its Parents font size.
Example:
rem : 100% of the Root Element font size.
em : 100% of the Parent font size.
EM is relative to the font-size of its direct or nearest parent, and REM is only relative to the html (root) font-size.
The main problem comes in using the 'em' and 'rem' when there is a parent font size....
**Here is the example:
suppose I want the font size of 2em for body of my website so we include 2em font size in body which is known as parent font size ...
and again for my H1 i want a size of 5.6 something and after we hit save and head back to our website and refresh then we will notice that our H1 has now become more than 5.6 .
this happens because the fonts gets inherited and added on top of whatever it got from its parent....(this problems are going to be happened when we are using em and percentages)
so, to get out of this type of problems the programmers use rem.

why doesn't padding have any effect

This is a css-noob question, but I don't get it: Why doesn't the following code show any padding for the outer div?
<html>
<style>
div {
border: 1px solid red;
padding:10em 30em 10em 30em;
font-size:0px;
}
a {
font-size:1rem;
}
</style>
<body>
<div>
hello
</div>
</body>
</html>
If you remove the font-size property you'll see the padding. But why does font-size influence the padding at all??
You have set the padding in em units which is calculated based on a base font size of the element that it's being applied on, you have set the font size to 0px, so the calculated output for the em unit is 0 (let's say 10em x 0(px)), so the padding gets a size of 0.
You can use rem instead of em. rem is based on the document's root or HTML tag's font size. So if you need to use em but the enforced font size on the element is not proper, set a root font size on the HTML tag and whenever needed use rem instead of em.
"The em and ex units depend on the font and may be different for each element in the document. The em is simply the font size".
source: W3.org
So... 0 font-size is 0 padding when em is used for padding.

Why font size does not change?

I want to set font size with em, not px. Here is my code:
<style type="text/css">
body{
font-size: 62.5%;
}
div{
font-size: 1.2em;
}
</style>
<body>
<div>hello world</div>
</body>
I want to set div font size to be 12px, but it looks small than I expect, I disable div style with chrome browser tools, it does not look to have any change. What should I do to make div font size 12px?
hmm, seems to work for me: example
Though, what you might be referring to is little change from enabling/disabling the font-size property. This is because the em for nested elements is relative to their parents font-size. In your instance, the font-size:Xem for the div is relative to the font-size:X% of the body.
If your still getting no change, try setting your body's font-size to a larger percentage to make your div's font larger

CSS: Why does reducing the font size of an adjacent inline element increase the overall leading?

I have an element that contains two span tags that each contain some text. The container element sets a font size, then the font size on the second span tag is set to a lower size. When the second span is reduced in font size, the space between the line and the next block element is increased. This occurs in both WebKit and Gecko.
The p container element has { margin-bottom: 0; padding-bottom: 10px; } and its following sibling has { margin-top: -5px; }
The following image illustrates the situation and contains a snapshot of the relevant part of the document structure in FireBug.
Why is the spacing beneath the p tag increasing after reducing the font size of the second span tag?
My guess is that you have a (relatively) large line-height being inherited by that decimal span (perhaps 32px?), and when you reduce the font size down to 18px, you get a situation where the baseline of the decimal glyphs match up with the nondecimal glyphs, but the line must still take up the full specified line-height. Thus, extra space is added below the baseline.
Add a line-height rule and I bet this goes away:
.box .value > .decimal { line-height: 18px; }

Resources