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.