Rem does not rezise elements when screen gets smaller - css

I have a problem where rem is not receing any of my elements when I change the size of the screen for example I have a padding on the body that is 3rem and my font-size on the html element is 62.5% yet when I resize the screen the padding on the body remains at 3rem or 30 pixels
*,
*::after,
*::before {
margin: 0;
padding: 0;
box-sizing: inherit;
}
html {
font-size: 62.5%;
}
body {
font-family: "Lato", sans-serif;
font-weight: 400;
line-height: 1.7;
color: #777;
padding: 3rem;
box-sizing: border-box;
}
I have also included the viewport meta tag
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

The way you have it, the rems would only change if you change the font-size.
If you want to adjust rems based on screen size you need to use media queries and change the root font-size there, e.g.:
html {
font-size: 62.5%
#media only screen and (max-width: 900px) {
font-size: 56.25%;
}
}

html {
font-size: 62.5%;
}
will in normal circumstances ensure that the basic em size is 10px. This is because browsers will default to 16px for the font-size and 62.5% gives you 10px - making it easier to know what, for example, 3em actually is (30px).
If you want to have a coninuous decrease (or increase) in font-size when you decrease (or increase) the viewport width then you can set font-size to be proportional to viewport width. For example:
html {
/* font-size: 62.5%; */
font-size: 1vw;
}
However, there are a couple of caveats. Some browsers (I believe Chrome does this) do not allow font-size to get too small. More importantly, a user may set their default em size to be larger so they see text enlarged and setting a font-size in absolute units rather than percentage terms will override that.
Therefore it needs careful thought before reducing font-size just because the viewport is smaller - the user still needs a decent size to be able to read things well. This is true whether you use the above 'continuous' method or a set of media queries to reduce the size. It's not necessarily advisable. Unfortunately it is not possible to tell how far away from the screen the user might be based on viewport size.

Related

Why is my font size of 1.1em way too large?

When I change my font-size from 1em to 1.1em, the font changes to a huge size. It looks like about size 48px or something. It should only increase by 10%.
* {
font-size: 1.1em;
}
Because with that selector every nested element increases the size by 10%. You might want 1.1rem instead, or better yet, just set :root { font-size: 18px; }
#ray hatfield's answer is correct.
As an alternative, you might want to change the * selector of this rule to html.
html {
font-size: 1.1em;
}
This way only the "basic font-size" (defined for the html tag, which is defined by the browser's default settings) will be increased by 10%, and all other font sizes (but only those with a relative unit like em or rem will change accordingly.

Is it possible to use both vw and vh in CSS?

When I resize the window width-wise it looks fine, but when adjusting the height, the text does not resize, which in turn creates overflow across the bracket.
#body div[class^="side-"] ol li p.game a {
color: #000000;
font: bold .7vw/1 tungsten, sans-serif;
height: auto;
width: auto;
padding: 0;
box-shadow: 0;
}
I've tried using vmin, but that does not help as the height of the window is almost always smaller than the width. Is it possible to use both vw and vh so that the window adjusts to both width and height resizing?
Yes, depending on the specifics of your use case. CSS allows for aspect-ratio media queries. This could allow you to have one vmin or vw/vh size for various aspect ratios. A rough example based on yours:
html {
color: #000000;
font: bold 7vw tungsten, sans-serif;
height: auto;
width: auto;
padding: 0;
box-shadow: 0;
font-size: 20vmin;
}
#media (min-aspect-ratio: 3/1) {
html {
color: #00a;
font-size: 14vmin;
}
}
This should allow you to solve for your concern about aspect ratios: "I've tried using vmin but that does not help as the height of the window is almost always smaller than the width".
Depending on your design, using vw when the screen is wide and vh when the screen is taller may be better than using the vmin shown above.
Maybe, you can see the property word-wrap: break-word ?
Else, you can see :
#media all and (orientation: landscape) { /* HERE YOUR CODE */ }

Font SIze not Consistent (Percentage)

I have a base size in my html of 62.5% and I multiply my headers' font-sizes accordingly. When I inspect it in my Chrome: different h2 headings have the same font-size, but they appear in diff. sizes on my screen. From my understanding the font sizes (with %) depend on the font-size of html. But anyways the parent containers are also the same width.
html {
font-size: 62.5%; }
h1, h2, h3, h4 {
text-align: center; }
h1 {
font-size: 250%; }
h2 {
font-size: 187.5%; }
h3 {
font-size: 125%; }
h4 {
font-size: 100%;
font-weight: bold;
margin-bottom: 0.5em; }
I figured out, that there are two font-sizes in the parent elements, which are crossed out. If I deactivate them in my developer window it still affects the size of the headers. Interesting to see.
But I think I had a misunderstanding, I thought if I have a parent defined in the css like
div h1, the font-size of the parent affects the size. But it seems that every parent font-size has an effect on its child elements. That makes working with %, like in the article below, in my humble opinion useless, because you are not able to set a hierarchy for the fonts. Or am I wrong? So the best way would be to use rems everywhere to get consistent font-sizes?
In html tag set font size in percentages, in body tag set to 1em, and other tags use em units

CSS "em" issue: avoid scaling to font-size of specific element

My site is almost totally designed in "em" (as opposed to px). It is supposed to be much better for modern browsers.
Most of the text is font-size:1em. 1em = 16px by default, I didn't specify it.
But I have some content where font-size is 1.2em and other which is 0.8em (for example for H1 or for small buttons).
The issue with "em" is that it re-scale all the sizes of an element (margin, padding, height...) according to the font-size.
I have the specific code in my CSS:
/* Reset */
html [and many other elements] {
font-size: 100%;
font: inherit;
}
/* Design */
body {
font-size: 1em;
line-height: 1; /* Line height will equal the em of each element */
}
.small-button {
font-size: 0.8em;
margin-left: 1em;
}
.normal-button {
font-size: 1em;
margin-left: 1em;
}
The normal-button has a margin of 1x1x16 = 16px. But the small-button has a margin of 1x0.8x16 = 12.8px.
Apparently this is a specific "em" property (it would not be the case in "px") which scales everything according to the font-size of the element.
This example is simple; but on my website it makes things really hard for me to keep things consistent.
How can I de-activate this property so that in the example above the 2 buttons have the same margin? (without re-calculating the sizes; which is what I am doing right now!)
It is the purpose of the em unit that it is relative to the currently set font size. If you want to use an consistent form of em, use the unit 'rem'. It is relative to the root element of your page (most likely your html tag) and stands for root em.
Check out this article by Jonathan Snook if you want to learn more about it.
http://snook.ca/archives/html_and_css/font-size-with-rem
Personally, I set my "master unit" in the body and proceed in multiples of 10s. I hate 16pt as stock, because I don't want to use a chart to set my font sizes the sizes I want them.
body { font-size:10pt; }
As far as particular elements, keep in mind that if you have an element (say a ul) with a size of 1.2em, and the li set to 1.0, and your body is 10pt, then the li is actually based off it's parent container, so it would be 1.2em instead of 1.0(aka 10pt as set in the body), because it's parent is 1.2em.
If you have something that you want a specific size throughout (such as a main menu), I suggest you forgo the em method on that particular parent object (or the li themselves) and use a set px or pt method.

Font-size relative to body?

Is there any way I can have font-sizes relative to the body font-size setting?
I know I could use percentages like this:
body{
font-size: 14px;
}
p{
font-size: 150%;
}
But this produces unwanted results on nested elements, where the font-size becomes relative to the parent element:
li{
font-size: 150%;
}
li li{
/* ... */
}
CSS3 introduced rem(root em), which is html. Unlike em, which sets the unit relative to the parent font-size, rem will set the unit relative to the root. Here is a great article on rem: >>>CLICK HERE<<<
CSS:
html {
font-size: 14px;
}
p {
font-size: 21px; /*cross browser fall-back hack*/
font-size: 1.5rem; /*same as 150%*/
}
li {
font-size: 21px; /*cross browser fall-back*/
font-size: 1.5rem;
}
li li {
font-size:18px; /*cross browser fall-back*/
font-size: 1.25rem;
}
For cross browser compatibility concerns, you can use a fall-back by also including px for desired results.
I am adding my response to Jukka K. Korpela's comment, as I think it is relevant to the original post.
The functionality of rem is an attempt to remove the compounding effect of em/% throughout your document (especially in lists) and also gain elasticity that you do not get with px. em/% requires a great deal of maintenance and with the ever evolving internet environment, this means a-lot of time. Additionally early IE would still require a hack to set the base percentage (most commonly the root [HTML] to 100%) then set the parent element to the necessary size. However, this hack would cause issues with newer browsers that follow the cascade properly. Again, my recommendation is to use rem for the a flexibility and ease of maintenance and a fall-back (for unsupported browsers) of px.
When using relative values, such as font-size: 150%; this is relative to the parent element. You can't change that.
For example if you had:
<div>
<p id=p1>
<p id=p2>
<p id=p3>
...
With this CSS:
div { font-size: 10pt }
p { font-size: 200% }
The result would be p1 would have a font size of 20pt, p2 would be 40pt, p3 would be 8-pt and so on.
You could get around this by overriding the font-size of everything, and then doing a relative font-size on only the relevant elements such as:
* { font-size: 100% }
body { font-size: 10pt }
#p3 { font-size: 150% }
div { font-size: 120% }
But again, any nesting of elements, like <div>s in the above example would again cause the relative font-size to grow or shrink with each nesting.
The best solution in your case is to switch to the use of variables and compile your css in SASS or LESS.
Although, if you don't want to go over the trouble, you can rely on %, em, or javascript...
The absolute unit, the pixel, is used if you want absolute control or in case you need a "pixel perfect" result. On the other hand, the relative units, like em or rem are usually used if the font size needs to change according to the screen size. It provides easier manipulation and requires fewer media query usages.
The em unit is relative to the parent font-size, while the rem unit is relative to the root element.
Here is an example of the usage of the rem unit:
html { font-size: 100% } //usually this is 16px
p,
div {
font-size: 1rem; //this will equal to 16px
}
section,
article {
font-size: 1.5rem; //this will equal to 24px
}
There is a great article here https://kolosek.com/css-relative-font-size/ that explains the absolute and relative font sizes in detail.

Resources