Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Setting font size using vh/vw but with maximum size in px/em/rem?

I love the idea of using vh/vw for changing the size of the font (very dynamic). However, I want to set a maximum size. This way the font will grow according to vh/vw, but will stop when it reaches a specific px/em/rem size, even if the viewer size still increases. Can this be done?

4 Answers

Currently there is no css rule to set a max-font size, so you could get your desired result, but you'd have to use media queries to do so.

You could use SASS to accomplish this:

@mixin responsive-font($responsive, $min, $max: false, $fallback: false) {
  $responsive-unitless: $responsive / ($responsive - $responsive + 1);
  $dimension: if(unit($responsive) == 'vh', 'height', 'width');
  $min-breakpoint: $min / $responsive-unitless * 100;

  @media (max-#{$dimension}: #{$min-breakpoint}) {
    font-size: $min;
  }

  @if $max {
    $max-breakpoint: $max / $responsive-unitless * 100;

    @media (min-#{$dimension}: #{$max-breakpoint}) {
      font-size: $max;
    }
  }

  @if $fallback {
    font-size: $fallback;
  }

  font-size: $responsive;
}

.limit-min {
  @include responsive-font(3vw, 20px);
}

.limit-min-max {
  @include responsive-font(3vw, 20px, 50px);
}

Source

Typography with Minimum and Maximum Sizes

Kevin Korte

I was just thinking the same thing: media queries.

SASS may be able to help you out, see my answer above for guidance (if SASS is in your wheelhouse at this stage).

Michael Davis I'm not yet that familiar with SASS. However, I'm very open to the idea. Can I use SASS for some of the CSS and traditional CSS for the rest of it (if that makes sense)?

I don't see why not, have two CSS files. One is your SASS, and the other your traditional CSS.