Font-size CSS
how can i use the min and max function in font-size css i want to in 1200px font-size get 60px and less than 1200px font-size get 40px
13 Replies
By 1200px font size do you mean 1200px screen size?
yes i meant the 1200px screen size
Look into clamp: https://developer.mozilla.org/en-US/docs/Web/CSS/clamp
clamp() - CSS: Cascading Style Sheets | MDN
The clamp() CSS function clamps a middle value within a range of values between a defined minimum bound and a maximum bound. The function takes three parameters: a minimum value, a preferred value, and a maximum allowed value.
Clamp works like this:
yeah , I did check the clamp() but I don't understand where the maxvalue will apply to the property and when the minvalue will apply it to the property
Clamp is divided in order: minimum value it will take, changing value, maximum value it will take -
clamp(min, changing, max)
.
So something like clamp(1rem, 5vw, 2rem)
won't be smaller than 1rem or bigger than 2rem.To add onto what was just said, you don't need a media query with clamp.
so as you said the changing is the breakpoint to change the size
what about the min() and the max()
No, that's not what they mean. Read the mdn docs I linked.
If you want to change it on breakpoints you can do
Clamp is generally the better option imo because less media queries the better
Also don't use px for media queries or font sizes, use rem
ok ok
i need to practice this more to understand it deeply
thanks bor
bro*
No problem, basically just to reiterate: clamp() is really good for typography because you don't need to rely on media queries when changing font sizes. It takes 3 arguments:
min, preferred, max
. min
is the smallest the font size will be. preferred
is the preferred value, which is to say the value that clamp will try to use (you should make this a unit that scales with screen size, like vw). max
is the largest the font size can grow. Don't use px
for your font size as it won't adapt based on user browser preferences. A user can set their default browser font size to 24px for example, and if you have your font sizes in rem, it will calculate 1 rem to be equal to 24px. If you make configure your font sizes to be in px, it will disregard user browser preferences. For media queries, it's generally regarded to use rem as well, though I did read that you can use px if you really want to (though you'd be losing support for IE I believe). By default, 1 rem = 16px though. So you can either use a VSCode extension that does the calculation for you (https://marketplace.visualstudio.com/items?itemName=cipchk.cssrem) or you can use a website (https://nekocalc.com/px-to-rem-converter).I would also suggest using the REM to PX Converter, I found a very nice tool (https://codebeautify.org/rem-to-px-converter); by default, 1 rem = 16px, but it can be changed.
While you can change it, you shouldn't. It's an accessibility nightmare waiting to happen.