The length units commonly used in CSS include %, px, in, cm, ch, mm, ex, pt, pc, em, rem, vw, vh, vmin, and vmax. These units can be roughly divided into absolute length units, relative length units, and percentage units based on their calculation methods.
In general, a CSS pixel represents one pixel on a screen device. However, on high-resolution screens, one CSS pixel often occupies multiple device pixels. In other words, multiple screen pixels are used to represent 1px. The formula is 1px = 1in / 96.
em represents the computed value of the font-size property of an element. If used for the font-size property itself, it is relative to the parent element's font-size. If used for other properties, it is relative to the element's own font-size. It is important to note that using em may result in the phenomenon of 1.2 * 1.2 = 1.44. For example, if the parent element's font-size property is set to 16px and the next level element is set to 1.2em, the calculated actual pixel value would be 16px * 1.2 = 19.2px. If the next level element continues to be set to 1.2em, the calculated value would be 16px * 1.2 * 1.2 = 23.04px. This is because the parent's base font-size property is recalculated to another value. When using em in a child element, the em value of the child element needs to be recalculated based on the parent element's font-size.
<styletype="text/css">div{height: 30px;background-color: blue;}#t7 > div{background-color: blue;font-size: 2em;/* relative to parent element 2 * 10px = 20px */width: 5em;/* relative to element itself 5 * 20px = 100px */}</style><sectionclass="except"><divid="t7"><div>Text</div></div><buttononclick="emChange()">Change font size</button></section><scripttype="text/javascript">functionemChange(){ document.getElementById("t7").style["font-size"]="20px";}</script>
The rem unit is determined based on the font-size of the root element html. The font-size of the root element serves as a baseline. When the size of the page changes, only the value of font-size needs to be changed, and the size of elements using rem as the fixed unit will also change accordingly. Since all elements are calculated based on the font-size of the root element, there is no phenomenon of 1.2 * 1.2 = 1.44 like em. rem is not only useful for defining font sizes, but can also be used to base the entire grid system or UI style library on the font-size of the HTML root element. This will result in more predictable font sizes and proportional scaling, achieving responsive layouts.
<styletype="text/css">html{font-size: 15px;}div{height: 30px;background-color: blue;}#t8 > div{background-color: blue;font-size: 2rem;/* relative to root element 2 * 15px = 30px */width: 6rem;/* relative to root element 6 * 15px = 90px */}</style>```html
<sectionclass="except"><divid="t8"><div>Text</div></div><buttononclick="remChange()">Change font size</button></section><scripttype="text/javascript">functionremChange(){ document.getElementsByTagName("html")[0].style["font-size"]="20px";}</script>
ex refers to the height of the lowercase letter x in the font used, but the height of x may vary depending on the font. For many fonts, 1ex ≈ 0.5em, so many browsers use half of the em value as the ex value. The ex unit is commonly used for fine-tuning in practice.
ch is similar to ex, and this unit represents the width of the 0 glyph in the font used by the element, or more precisely, the predicted size of the 0 glyph. If the size of the 0 glyph cannot be determined, it must be assumed that its width is 0.5em and its height is 1em, that is, half of the em value is taken as the ch value.
When the measurement unit is set to a percentage, the width and height of browser components can change accordingly with the size of the browser.
When a percentage is used in the height or width of a child element, it is relative to the direct parent element of the child element. The width is relative to the parent element's width, and the height is relative to the parent element's height.
If a percentage is set for the top and bottom of a child element, it is relative to the height of the directly positioned parent element that is not static. Similarly, if a percentage is set for the left and right of a child element, it is relative to the width of the directly positioned parent element that is not static.
If a percentage is set for the padding of a child element, whether in the vertical or horizontal direction, it is relative to the width of the direct parent element, and is independent of the parent element's height.
If a percentage is set for the margin of a child element, whether in the vertical or horizontal direction, it is relative to the width of the direct parent element.
When border-radius is set as a percentage, it is relative to the element's own width. Similarly, translate, background-size, and others are relative to the element's own width.