Getting to the actual front-end development: CSS font units: px, em, rem and%

For plotting and printing, the "unit" is important, but the page layout, the unit is the same importance, since CSS3 popularity, more support for the unit easy to use some of the (px, em, rem ... etc.), this article will sort out these common CSS units, but also help themselves in the future be able to use more handy.

"Web" and "printing" of the unit
if the unit should do the segment, the simplest can be divided into "pages" and "print" two categories, usually for CSS is only applied to the style pages, after all, really need to do printing, or will tend to be designed through the typesetting software.

Web page (units)

  • px: absolute units, representing each screen "point" (pixel).
  • em: relative units, each sub-element is multiplied by the value of the parent element px through the "multiplier."
  • rem: relative units, each element is multiplied by the value of the root element px through the "multiplier."
  • %: Relative units, each sub-element is multiplied by the value of the parent element px through the "percentage."

One learns there will be confusion, lack of motivation. Here's what I recommend front-end learning exchange group: 731 771 211, which is the front end of the study, if you want to create web pages cool, want to learn programming. Himself compiled a front-end 2019 the most comprehensive study materials, from the most basic HTML + CSS + JS [cool special effects, games, plug-in package design patterns] to move to the end of the project HTML5 real learning materials are finishing, gave every front-end junior partner, had wanted to learn web front-end, or change jobs, or college students, as well as work want to upgrade their skills, are welcome to join the junior partner to learn to learn.

Click: join

Web page (attribute name)

  • medium: in 16px (h4 preset value)
  • xx-small: 0.6 times (h6 preset value) of Medium
  • x-small: 0.75 times the medium
  • small: 0.8 times the medium (H5 preset value, defined as the W3C 0.89, found about 0.8)
  • large: 1.1 times the medium (h3 preset value, defined as the W3C 1.2, measured about 1.1)
  • x-large: 1.5 times (h2 preset value) of Medium
  • xx-large: 2 times (h1 preset value) of Medium
  • smaller: about 80% of the Parent
  • larger: about 120% of Parent

printing

  • pt: printer for each "point" is defined as 1 pt = 1/72 in, if 1 px = 1 pt on a 72 dpi system, but if 1 px = 0.75 pt on a 96dpi system (72/96 = 0.75).
  • in: inches, at 96 dpi on the system 1 in = 96 px.
  • cm: cm, on a 96 dpi system 1 cm = 37.795275593333 px.
  • mm: millimeter on a 96 dpi system 1 cm = 3.7795275593333 px.

Example shows
the following example will show four different units, in fairness, all four examples div take the default format, change the font-size pure look any different, because the child element, if not set font-size, will automatically inherit the parent element's font-size, use should be pre-initialized the font size, the following two CSS can change the default font size to all the elements 16px, then can be individually adjusted.

html{
    font-size:16px;
}

html * {
    font-size: 1rem;
}

Px 1.
px is absolutely units, so long as the number of px is set, there will be the presentation of accurate, useful for some emphasis on the precise location of the typesetting terms, as in the example shown, the font will specify how much px much.

<div style="font-size:16px;">16px
    <div style="font-size:20px;">20px
        <div style="font-size:24px;">24px
            <div style="font-size:16px;">16px
                <div style="font-size:32px;">32px</div>
            </div>
        </div>
    </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

EM 2.
EM relative units is, for each sub-element through the "fold" value is multiplied by the parent element px, if we use 1.2em div each layer, the innermost layer will be 16px x 1.2 x 1.2 x 1.2 x 1.2 x 1.2 = 39.8px. (Browser's default font size is 16px, unless otherwise specified it will directly inherit the parent element's font size)

<div style="font-size:1.2em;">1.2em
    <div style="font-size:1.2em;">1.2em
        <div style="font-size:1.2em;">1.2em
            <div style="font-size:1.2em;">1.2em
                <div style="font-size:1.2em;">1.2em</div>
            </div>
        </div>
    </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

REM 3.
REM is relative units, each element through the "fold" value is multiplied by the root element px, if we use for each layer div 1.2rem, the innermost layer will be 16px x 1.2 = 19.2px. (Html root element refers to a font-size, default 16px)

<div style="font-size:1.2rem;">1.2rem
    <div style="font-size:1.2rem;">1.2rem
        <div style="font-size:1.2rem;">1.2rem
            <div style="font-size:1.2rem;">1.2rem
                <div style="font-size:1.2rem;">1.2rem</div>
            </div>
        </div>
    </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

% 4
% Percentages are relative units, and similar em, em is simply divided by one hundred percentage, if we use for each layer div 120%, equivalent to 1.2em, the innermost layer will be 16px x 1.2 x 1.2 x 1.2 x 1.2 x 1.2 = 39.8px.

<div style="font-size:120%;">120%
    <div style="font-size:120%;">120%
        <div style="font-size:120%;">120%
            <div style="font-size:120%;">120%
                <div style="font-size:120%;">120%</div>
            </div>
        </div>
    </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

5.small, medium, large ... such as
font size There are seven properties, which are xx-small, x-small, small, medium, large, x-large and xx-large, in addition to x-small, the remaining six kinds respectively character size corresponding to the label h6 ~ h1, according to the W3C specifications to 16px preset based medium (if the default font size change html, medium change will follow), multiplied by a fixed percentage of the size of the medium, e.g. ss- small default is 16px x 0.6 = 9.6px (browser display as 12px).
Getting to the actual front-end development: CSS font units: px, em, rem and%

<div style="font-size:xx-small;">xx-small
    <div style="font-size:x-small;">x-small
        <div style="font-size:small;">small
            <div style="font-size:medium;">medium
                <div style="font-size:large;">large
                    <div style="font-size:x-large;">x-large
                        <div style="font-size:xx-large;">xx-large</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

Larger 6. The, Smaller
larger and is fixed percentage Smaller units, 120% larger parent layer, 80% smaller parent layer.

<div style="font-size:medium;">medium
  <div style="font-size:larger;">larger
    <div style="font-size:larger;">larger
      <div style="font-size:larger;">larger
        <div style="font-size:smaller;">smaller
          <div style="font-size:smaller;">smaller
            <div style="font-size:smaller;">smaller</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Getting to the actual front-end development: CSS font units: px, em, rem and%

Summary
After the familiar font size units, they will have a clearer system design throughout the site's CSS framework, but the font-size and font-family itself has a number of complex relationships, different font-family sometimes affect the font-size set fixed, and therefore still have to use a little note Hello!

Guess you like

Origin blog.51cto.com/14284898/2403628