Responsive font size in CSS

前端 未结 30 3065
刺人心
刺人心 2020-11-22 07:23

I\'ve created a site using the Zurb Foundation 3 grid. Each page has a large h1:

30条回答
  •  野性不改
    2020-11-22 08:12

    There are several ways to achieve this.

    Use a media query, but it requires font sizes for several breakpoints:

    body
    {
        font-size: 22px;
    }
    
    h1
    {
        font-size: 44px;
    }
    
    @media (min-width: 768)
    {
        body
        {
            font-size: 17px;
        }
        h1
        {
            font-size: 24px;
        }
    }
    

    Use dimensions in % or em. Just change the base font size, and everything will change. Unlike the previous one, you could just change the body font and not h1 every time or let the base font size be the default of the device and the rest all in em:

    1. “Ems” (em): The “em” is a scalable unit. An em is equal to the current font-size, for instance, if the font-size of the document is 12 pt, 1 em is equal to 12 pt. Ems are scalable in nature, so 2 em would equal 24 pt, .5 em would equal 6 pt, etc..
    2. Percent (%): The percent unit is much like the “em” unit, save for a few fundamental differences. First and foremost, the current font-size is equal to 100% (i.e. 12 pt = 100%). While using the percent unit, your text remains fully scalable for mobile devices and for accessibility.

    See kyleschaeffer.com/....

    CSS 3 supports new dimensions that are relative to the view port. But this doesn't work on Android:

    1. 3.2vw = 3.2% of width of viewport
    2. 3.2vh = 3.2% of height of viewport
    3. 3.2vmin = Smaller of 3.2vw or 3.2vh
    4. 3.2vmax = Bigger of 3.2vw or 3.2vh

      body
      {
          font-size: 3.2vw;
      }
      

    See CSS-Tricks ... and also look at Can I Use...

提交回复
热议问题