When the screen size is small (mobile), the font size is fixed at 14px
. On really large screens, the font size is set to 18px
, ensuring it never goes below 14px
or above
18px>. This is achieved using media queries.</p>
<p>However, for the in-between range, I want the font size to be dynamic, ranging between <code>14px
and 18px
. To achieve this, I am currently using font-size: calc(.5vw + .85em);
, which works adequately but not perfectly. I am unsure how to calculate it so that it starts at 14px
and ends at 18px
between a screen width of 40rem
(16 * 40) and 65rem
.
Any suggestions?
.wrap {
font-size: 16px;
}
p {
font-size: calc(.5vw + .85em);
}
p:before {
content: 'Desktop: ';
opacity: .5;
}
@media screen and (min-width: 65rem) {
p {
font-size: 18px;
}
p:before {
content: 'Desktop large: ';
opacity: .5;
}
}
@media screen and (max-width: 40rem) {
p {
font-size: 14px;
}
p:before {
content: 'Mobile: ';
opacity: .5;
}
}
<div class="wrap">
<p>
text
</p>
</div>
I have also included an example here: https://jsfiddle.net/6ybkp7a1/6/
Example
If my screen width is 40.1rem
(slightly above 40rem
), the font size should be around 14.1px
, slightly higher than 14px
.