Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Manav Misra
Manav Misra

Posted on

     

Use a 'Base Font Size'

...for 'Responsive Font Sizing'

A technique that I have been using to help with Responsive Web Design (RWD) - fancy words meaning that our website resizes and scales 'nicely' based on the width of our screens, etc. -is to simply applyfont-size: 10px (or really any number, but 10px or 20px is easier for the math) to thebody tag.

Then, all of the fonts get appropriately sized withem. You see,em units scale up directly from the first ancestor (e.g. parent, grandparent, etc.) that has an established font size. So, if we set that as10px, then1em =10px,2em =20px, etc.

body{font-size:10px;}h1{font-size:3.052em;}
Enter fullscreen modeExit fullscreen mode

Ourh1 will have a size of:30.52px. If you're wondering about this 'weird' number, I will mention later in the post.

Why Not Just Usepx to Specify the Font Size?

Great ❓, and a common one from my students.

The reason why is that typically folks hold ☎️s or tablets closer to their faces and we don't need the lettering to be as big.

But, on desktop views 🖥️, folks are sitting a bit further back and may need to squint to see the lettering properly.

If we were to usepx, then we would have to manually update each and everyfont-size to be increased size.

Instead, by usingems, we can just increase our 'base'font-size and each and every otherfont-size is appropriately increased.

For example, assuming that our 'base styles - that is, styles for 'mobile' ☎️ are established as above 👆🏽, we can use a@media to easily scale up.

@mediascreenand(min-width:1024px){body{font-size:16px;}}
Enter fullscreen modeExit fullscreen mode

Now, without any additional work, all of thefont-sizes are scaled up by 60%. Or, if you don't like that math, think if it was set as:font-size: 20px. If the original was10px, it means now all things are doubled.

Type Scaling

Why this 'weird' number 👆🏽?font-size: 3.052 em;

TBH, not 💯 sure, but apparently there are some 'design rules' regarding what makes 'font-size scaling' and 'typography' look 'good' on sites. To this end, I useType Scale to get recommendations for how the type should be...scaled.

You simply put in your font (if using Google Fonts) and our base font size and play with scaling.

Screenshot from Type Scale

It actually generates CSS for you, but Idon't use that - there is some other 'weird stuff' added.


For some additional perspective, you can kindly listen 👂🏽 to thisepisode on CSS Units from Syntax.fm.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
  • Location
    62236
  • Education
    BS - Mech. Eng. - Missouri S&T
  • Joined

More fromManav Misra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp