question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responsive font sizes

See original GitHub issue

Coming from Bootstrap I’m wondering if there is also on Tailwind something like: https://getbootstrap.com/docs/4.5/content/typography/#responsive-font-sizes: Responsive font sizes?

I have

h1 {
  @apply text-6xl;
}

but this is good for desktop only.

In mobile I’ll be fine with text-2xl.

Is there a way to do it without using @screen ... {}?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
MartijnCuppenscommented, May 31, 2020

Hi @frederikhors,

You can also use RFS (Bootstraps resizing engine) as a PostCSS plugin. Therefor you’ll need to install rfs (npm install rfs or yarn add rfs) and adjust your postcss.config.js file:

module.exports = {
	plugins: [
		require('tailwindcss'),
		require('rfs'),
		require('autoprefixer')
	]
};

Now you can use the rfs() function, just wrap it around any unit you want to resize in your custom CSS.

If you want to use tailwinds @apply functionality, you’ll need to enable rfs in your tailwind.config.js:

module.exports = {
	theme: {
		fontSize: {
			xs: 'rfs(0.75rem)',
			sm: 'rfs(0.875rem)',
			base: 'rfs(1rem)',
			lg: 'rfs(1.125rem)',
			xl: 'rfs(1.25rem)',
			'2xl': 'rfs(1.5rem)',
			'3xl': 'rfs(1.875rem)',
			'4xl': 'rfs(2.25rem)',
			'5xl': 'rfs(3rem)',
			'6xl': 'rfs(4rem)'
		}
	}
};

If you now use

h1 {
  @apply text-6xl;
}

The following CSS will be generated:

h1 {
  font-size: calc(1.525rem + 3.3vw);
}
@media (min-width: 1200px) {
  h1 {
    font-size: 4rem;
  }
}

You could also use it for fluid margin or padding utilities if you want to:

module.exports = {
	theme: {
		spacing: {
			px: '1px',
			'0': '0',
			'1': 'rfs(0.25rem)',
			'2': 'rfs(0.5rem)',
			'3': 'rfs(0.75rem)',
			'4': 'rfs(1rem)',
			'5': 'rfs(1.25rem)',
			'6': 'rfs(1.5rem)',
			'8': 'rfs(2rem)',
			'10': 'rfs(2.5rem)',
			'12': 'rfs(3rem)',
			'16': 'rfs(4rem)',
			'20': 'rfs(5rem)',
			'24': 'rfs(6rem)',
			'32': 'rfs(8rem)',
			'40': 'rfs(10rem)',
			'48': 'rfs(12rem)',
			'56': 'rfs(14rem)',
			'64': 'rfs(16rem)'
		},
		fontSize: {
			xs: 'rfs(0.75rem)',
			sm: 'rfs(0.875rem)',
			base: 'rfs(1rem)',
			lg: 'rfs(1.125rem)',
			xl: 'rfs(1.25rem)',
			'2xl': 'rfs(1.5rem)',
			'3xl': 'rfs(1.875rem)',
			'4xl': 'rfs(2.25rem)',
			'5xl': 'rfs(3rem)',
			'6xl': 'rfs(4rem)'
		}
	}
};

Hopes this helps, let me know if anything is still unclear.

3reactions
adamwathancommented, Jun 27, 2020

Thanks for providing that example @MartijnCuppens!

@frederikhors this is the very first time this has come up so likely not going to dedicate a docs page to it, but we have plans to add a new “guides” section to the docs site in the future where we can publish short articles teaching people how to do stuff like this, and I think this would be a great candidate for that section. I’ve made a note of it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Create a Responsive Text - W3Schools
Responsive Font Size. The text size can be set with a vw unit, which means the "viewport width". That way the text size...
Read more >
The Beginner's Guide to Responsive Text on the Web
font -size to · 1.6vw. On a browser 1000 pixels wide, the text will be a nice and readable 16 pixels large. However,...
Read more >
Responsive font size in CSS - Stack Overflow
Use dimensions in % or em. Just change the base font size, and everything will change. Unlike the previous one, you could just...
Read more >
Responsive Font Size (Optimal Text at Every Breakpoint)
Fluid font sizes per breakpoint ; 320px (eg: iPhone 4 & 5), 16px ; 768px (eg: iPad portrait), 18px ; 1024px (eg: iPad...
Read more >
Font Size Guidelines for Responsive Websites - Editor X
Body text - Font sizes should be around 16px to 18px for legibility (or 1.6rem to 1.8rem using our sizing rules mentioned above)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found