#CSS The most commonly used length units are pixels (px), percentage (%), rem, em, vh, vw, pt, cm, mm, in, etc. These units can be used to set the element's width, height, border size, font size, etc.
div { width: 200px; height: 100px; font-size: 16px; border: 1px solid #000; }
In the code example above, the width
and height
properties use pixel units to define the width and width of the div element. The height, font-size
attribute uses pixel units to define the font size, and the width of the border
attribute also uses pixel units.
div { width: 50%; height: 50%; }
In the above code example, the width and height of the div element are set to 50% of the dimensions of the parent element.
html { font-size: 16px; } div { width: 10rem; height: 5rem; font-size: 1.2rem; }
In the above code example, the font size of the root element is set to 16px, the width and height of the div element are 10 times the font size of the root element, and the font size Is 1.2 times the font size of the root element.
div { font-size: 16px; width: 2em; height: 2em; }
In the above code example, the width and height of the div element are set to twice the font size of the element itself.
In addition to the above units, there are some other length units:
In short, it is necessary to choose the appropriate length unit in different situations to achieve the best effect.
The above is the detailed content of What are the commonly used CSS length units?. For more information, please follow other related articles on the PHP Chinese website!