If you are a front-end development engineer, the average PX and EM usage frequency is higher. But today the focus of this article is to introduce some of the units we use very little or even heard of.
First, relive EM
123456789101112131415 |
<style type=
"text/css"
>
body {font-size: 12px;}
div {font-size: 1.5em;}
</style>
<body>
<div>
Test-01 (12px * 1.5 = 18px)
<div>
Test-02 (18px * 1.5 = 27px)
<div>
Test-03 (27px * 1.5 = 41px)
</div>
</div>
</div>
</body>
|
Because Font-size is inherited, the darker the number of layers, the larger the font.
Second, REM
Although the use of EM above may be used in development, but we sometimes want to have a benchmark to expand. When this requirement is met, we can use REM units, where "R" in rem stands for "root", which means that the font size of the current element is set to the root element, and in most cases we will set it on the HTML element.
123456789101112131415 |
<style type=
"text/css"
>
html {font-size: 12px;}
div {font-size: 1.5rem;}
</style>
<body>
<div>
Test-01 (12px * 1.5 = 18px)
<div>
Test-02 (12px * 1.5 = 18px)
<div>
Test-03 (12px * 1.5 = 18px)
</div>
</div>
</div>
</body>
|
Of course, REM units are not only applied to fonts, but can also be implemented into CSS grid systems.
Iii. VH and VW
In a responsive layout, we often use percentages to lay out, but the percentage of CSS does not always solve the best scenario for each problem, and the width of the CSS is relative to the width of the parent element closest to it. If you want to use the width and height of the viewport instead of the width of the parent element, you can use VH and VW units. 1VH = Viewportheight * 1/100; 1VW = Viewportwidth * 1/100; With VH, VW can guarantee the width and height of the elements to accommodate different devices.
Iv. Vmin and Vmax
VW and VH correspond to the width and height of the viewport, while the vmin and Vmax correspond to the minimum and maximum values in width, height, for example, if the width/height of the browser is set to 1000px/600px, then
1vmin = 600 * 1/100;
1vmax = 1000 * 1/100;
Let's take a look at a few examples: 4.1 a square element always touches at least two edges of a screen
1234567891011 |
<style type=
"text/css"
>
.box {
height: 100vmin;
width : 100vmin;
}
</style>
<body>
<div
class
=
"box"
style=
" margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, Courier, monospace !important; min-height: inherit !important; background: none !important;">>
fdasjfdlas
</div>
</body>
|
4.2 Overlay Full Screen
123456789101112131415 |
<style type=
"text/css"
>
body {
margin: 0;
padding:0;
}
.box {
/*宽屏显示器width > height*/
height: 100vmin;
width : 100vmax;
}
</style>
<div
class
=
"box" style=
" margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, Courier, monospace !important; min-height: inherit !important; background: none !important;">>
fdasjfdlas
</div>
|
V. EX and CH
EX, CH units and EM, REM are similar in that all rely on font-size, but ex, CH also rely on font-family, based on font-specific to calculate. Citation specification:
ex unit equal to the used x-height of the First available font. [css3-fonts] the x-height is so called because it's often equal to the height of the Lowe Rcase "X". However, an '
ex
' was defined even for fonts that does not contain an "x". The x-height of a font can be found in different ways. Some fonts contain reliable metrics for the x-height. If Reliable font metrics is not available, UAs may determine the x-height from the height of a lowercase glyph. One possible heuristic is-to-look at how far the glyph for the lowercase ' o ' extends below the baseline, and subtract that Value from the top of its bounding box. In the cases where it's impossible or impractical to determine the x-height, a value of 0.5em must be assumed.
CH Unit Equal to the used advance measure of the "0" (ZERO, u+0030) glyph found in the font used to render it .
Use a diagram to explain the meaning of these two units:
These two units, for many purposes, are mostly used for micro-adjustment during printing. For example, the SUP and sub elements display superscript and subscript respectively, but we can use position and bottom simulations:
123456789101112131415161718 |
<style type=
"text/css"
>
body {
margin: 0;
padding:0;
} .sup {
position: relative;
bottom: 1ex;
}
.sub {
position: relative;
bottom: -1ex;
}
</style>
<div>
AaB<span
class
=
"sup"
>b</span>CcXxD<span
class
=
"sub"
>d</span>EeFf
</div>
|
7 CSS units you may not know