Reputation: 803
After being inspired from this Pen By Dannie Vinther that shows how to make fonts responsive without using media queries
, I decided to make another Pen, but using rem
as Dannie Vinther's pen uses px
. I made some changes, but it does not working.
Here is my code:
* {
/* Setting root font size*/
--root-size: 20px;
font-size: var(--root-size, 20px);
/* Typography */
--main-font: 'Slabo 27px', serif;
/* Calculation, Ranges from 421px to 1199px */
--responsive: calc((var(--min-font) * var(--root-size)) + (var(--max-font) - var(--min-font)) * ((100vw - 420px) / (1200 - 420)));
}
h1 {
/* Set max and min font sizes */
--max-font: 2.5;
/* 2.5rem equals to 50px */
--min-font: 1.25;
/* 1.25rem equals to 25px */
font-family: var(--main-font);
font-size: var(--responsive);
}
p {
font-family: sans-serif;
margin: auto;
width: fit-content;
/* Set max and min font sizes */
--max-font: 1;
/* 1rem equals to 25px */
--min-font: 0.7;
/* 0.7rem equals to 14px */
font-size: var(--responsive);
}
@media (min-width: 1200px) {
h1,
p {
font-size: calc(var(--max-font) * var(--root-size));
}
}
@media (max-width: 420px) {
h1,
p {
font-size: calc(var(--min-font) * var(--root-size));
}
}
/* Whatever */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
display: flex;
}
div {
margin: auto;
padding: var(--root-size);
}
a,
a:visited,
a:focus,
a:active,
a:link {
text-decoration: none;
outline: 0;
}
a {
color: skyblue;
transition: .2s ease-in-out color;
}
h1,
h2,
h3,
h4 {
margin: .3em 0;
}
<link href="https://fonts.googleapis.com/css?family=Slabo+27px" rel="stylesheet">
<div>
<h1>Responsive text with CSS Custom Properties</h1>
<p>Custom Properties <a rel="noopener noreferrer" target="_blank" href="http://caniuse.com/#feat=css-variables">isn't supported</a> by IE.</p>
<p>Inspired From <a href="https://codepen.io/dannievinther/pen/GNExxb?editors=1100">A PEN BY Dannie Vinther</a></p>
</div>
In my code, font-size
is almost constant. It changes on 720px
. font-size
become equals to 50px
, when the screen width
is more than or equals to 720px
. And 25px
when the screen width
is less than 720px
. Only little change in font-size
is observed when the screen width
decreases from 720px
to 420px
.
I'm unable to figure out the root cause. Please help. Thank you.
Upvotes: 3
Views: 2457
Reputation: 272772
Your formula isn't correct. If we consider this part: ((100vw - 420px) / (1200 - 420))
we will have 0px
when 100vw is equal to 420px and 1px
when equal to 1200px. Then you multiply by (var(--max-font) - var(--min-font))
which is equal to 1.25
for the h1
so basically you will have a value between 0px
and 1.25px
and if we add the main part ((var(--min-font) * var(--root-size))
) we will have a font-size
between 25px
and only 26.25px
not 50px
.
You are missing the multiplication by var(--root-size)
in the second part. Doing so you will have a value between 0px
and 25px
BUT there is an issue because you cannot do this multiplication since you will have px*px
which is invalid using calc
.
To overcome this, your font-size
need to be defined uniteless and your fomula need to be adjusted like below:
* {
/* Setting root font size*/
--root-size: 20;
font-size: calc(var(--root-size, 20) * 1px);
/* Typography */
--main-font: 'Slabo 27px', serif;
/* Calculation, Ranges from 421px to 1199px */
--responsive: calc((var(--min-font) * var(--root-size) * 1px) + (var(--max-font) - var(--min-font)) * var(--root-size) * ((100vw - 420px) / (1200 - 420)));
}
h1 {
/* Set max and min font sizes */
--max-font: 2.5;
/* 2.5rem equals to 50px */
--min-font: 1.25;
/* 1.25rem equals to 25px */
font-family: var(--main-font);
font-size: var(--responsive);
}
p {
font-family: sans-serif;
margin: auto;
width: fit-content;
/* Set max and min font sizes */
--max-font: 1;
/* 1rem equals to 25px */
--min-font: 0.7;
/* 0.7rem equals to 14px */
font-size: var(--responsive);
}
@media (min-width: 1200px) {
h1,
p {
font-size: calc(var(--max-font) * var(--root-size) * 1px);
}
body {
background:red;
}
}
@media (max-width: 420px) {
h1,
p {
font-size: calc(var(--min-font) * var(--root-size) * 1px);
}
body {
background:blue;
}
}
/* Whatever */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
display: flex;
}
div {
margin: auto;
padding: var(--root-size);
}
a,
a:visited,
a:focus,
a:active,
a:link {
text-decoration: none;
outline: 0;
}
a {
color: skyblue;
transition: .2s ease-in-out color;
}
h1,
h2,
h3,
h4 {
margin: .3em 0;
}
<link href="https://fonts.googleapis.com/css?family=Slabo+27px" rel="stylesheet">
<div>
<h1>Responsive text with CSS Custom Properties</h1>
<p>Custom Properties <a rel="noopener noreferrer" target="_blank" href="http://caniuse.com/#feat=css-variables">isn't supported</a> by IE.</p>
<p>Inspired From <a href="https://codepen.io/dannievinther/pen/GNExxb?editors=1100">A PEN BY Dannie Vinther</a></p>
</div>
Upvotes: 2