Resize text based on screen size

I have different elements in my html with different text sizes. I want them to be dynamic so that they display different font sizes for different screen sizes.

What would be the best way? Now I do it like this, but it makes the code look ugly:

<script>
$(window).resize(function(){
$('#first').css('font-size',($(window).width()*0.2)+'px');
$('h2').css('font-size',($(window).width()*0.02)+'px');
$('h1').css('font-size',($(window).width()*0.03)+'px');

For two elements, this will be fine, but I still have a lot. Not having much experience with html / css / javascript, so this is what I came up with.

As I study, I want to know what will be the best way.

Perhaps you have suggestions for improving this?

+4
source share
9 answers

css vw, vh (/ ).

css-, , .

: ( )

h1 {
  font-size: 5.9vw;
}
h2 {
  font-size: 3.0vh;
}
p {
  font-size: 2vmin;
}

, h1 5,9% . .


, font-size , , , .

, AKA CSS.

CSS - CSS, :

  • ,
  • ( ),
  • .

( CSS, , .)

, , , 16 600 , 32 1200 .

SASS mixin, , CSS :

div {
  /* linearly increase the font-size from 16->32px 
     between a viewport width of 600px-> 1200px  */
  @include fluid-type(font-size, 600px, 1200px, 16px, 32px);
}
@media screen and (max-width: 600px) {
  div {
     font-size: 16px;
  }
}
@media screen and (min-width: 1200px) {
  div {
     font-size: 36px;
  }
}

// ----
// libsass (v3.3.6)
// ----

// =========================================================================
//
//  PRECISE CONTROL OVER RESPONSIVE TYPOGRAPHY FOR SASS
//  ---------------------------------------------------
//  Indrek Paas @indrekpaas
//
//  Inspired by Mike Riethmuller Precise control over responsive typography
//  http://madebymike.com.au/writing/precise-control-responsive-typography/
//
//  `strip-unit()` function by Hugo Giraudel
//  
//  11.08.2016 Remove redundant `&` self-reference
//  31.03.2016 Remove redundant parenthesis from output
//  02.10.2015 Add support for multiple properties
//  24.04.2015 Initial release
//
// =========================================================================

@function strip-unit($value) {
  @return $value / ($value * 0 + 1);
}

@mixin fluid-type($properties, $min-vw, $max-vw, $min-value, $max-value) {
  @each $property in $properties {
    #{$property}: $min-value;
  }

  @media screen and (min-width: $min-vw) {
    @each $property in $properties {
      #{$property}: calc(#{$min-value} + #{strip-unit($max-value - $min-value)} * (100vw - #{$min-vw}) / #{strip-unit($max-vw - $min-vw)});
    }
  }

  @media screen and (min-width: $max-vw) {
    @each $property in $properties {
      #{$property}: $max-value;
    }
  }
}

// Usage:
// ======

// /* Single property */
// html {
//   @include fluid-type(font-size, 320px, 1366px, 14px, 18px);
// }

// /* Multiple properties with same values */
// h1 {
//   @include fluid-type(padding-bottom padding-top, 20em, 70em, 2em, 4em);
// }

////////////////////////////////////////////////////////////////////////////

div {
  @include fluid-type(font-size, 600px, 1200px, 16px, 32px);
}
@media screen and (max-width: 600px) {
  div {
     font-size: 16px;
  }
}
@media screen and (min-width: 1200px) {
  div {
     font-size: 36px;
  }
}
<div>Responsive Typography technique known as Fluid Type or CSS Locks. 
  Resize the browser window to see the effect.
</div>
Hide result

- Codepen


CSS-

CSS

+19

css

@media(max-width:767px) {
    body {
        font-size: 10px;
    };
}

@media(min-width:768px) {
    body {
        font-size: 11px;
    };
}

@media(min-width:992px) {
    body {
        font-size: 12px;
    };
}

@media(min-width:1200px) {
    body {
        font-size: 13px;
    };
}
+5

ditch javascript, ... em css, .

div{
   font-size:2em;
}
+1

em, .

:

, em ; ( CSS) W3C HTML - , - , (, em), , ( "px" ) .

0

javascript . CSS .

"em" "%" , , .

:

font-size: 3em;
font-size: 10%;

.

0

, ( javascript) http://fittextjs.com/

0

, % em.

0

css media.

- .

: css

@media screen and (max-width: 1024px) {
   body {
      font-size: 90%;
   }
}

@media screen and (max-width: 1440px) {
   body {
      font-size: 100%;
   }
}

, , .

: h1

h1 {
   font-size: 150%;
}

, .

0

JavaScript . CSS. CSS , px, em % rem (root em).

, . -, . :

@media screen and (max-width: 1024px) {
    #first { font-size: 204px; }
    h2 { font-size: 20px; }
    h1 { font-size: 30px; }
}
0

All Articles