- Mastering Bootstrap 4(Second Edition)
- Benjamin Jakobus Jason Marah
- 363字
- 2021-06-24 19:25:49
From pixel to root em
Unlike its predecessor, Bootstrap 4 no longer uses pixel (px) as its unit of typographic measurement. Instead, it primarily uses root em (rem). The reasoning behind choosing rem is based on a well-known problem with px; websites using px may render incorrectly, or not as originally intended, as users change the size of the browser's base font. Using a unit of measurement that is relative to the page's root element helps address this problem, as the root element will be scaled relative to the browser's base font. In turn, a page will be scaled relative to this root element.
Typographic units of measurement
Simply put, typographic units of measurement determine the size of your font and elements. The most commonly used units of measurement are px and em. The former is an abbreviation for pixel and uses a reference pixel to determine a font's exact size. This means that for displays of 96 dots per inch ( dpi), 1 px will equal an actual pixel on the screen. For higher resolution displays, the reference pixel will result in the px being scaled to match the display's resolution. For example, specifying a font size of 100 px will mean that the font is exactly 100 pixels in size (on a display with 96 dpi), irrespective of any other element on the page.
EM is a unit of measurement that is relative to the parent of the element to which it is applied. So, for example, if we were to have two nested div elements, the outer element with a font size of 100 px and the inner element with a font size of 2 em, then the inner element's font size would translate to 200 px (as in this case 1 em = 100 px). The problem with using a unit of measurement that is relative to parent elements is that it increases your code's complexity, as the nesting of elements makes size calculations more difficult.
The recently introduced rem measurement aims to address both em's and px's shortcomings by combining their two strengths; instead of being relative to a parent element, rem is relative to the page's root element.
Simply put, typographic units of measurement determine the size of your font and elements. The most commonly used units of measurement are px and em. The former is an abbreviation for pixel and uses a reference pixel to determine a font's exact size. This means that for displays of 96 dots per inch ( dpi), 1 px will equal an actual pixel on the screen. For higher resolution displays, the reference pixel will result in the px being scaled to match the display's resolution. For example, specifying a font size of 100 px will mean that the font is exactly 100 pixels in size (on a display with 96 dpi), irrespective of any other element on the page.
EM is a unit of measurement that is relative to the parent of the element to which it is applied. So, for example, if we were to have two nested div elements, the outer element with a font size of 100 px and the inner element with a font size of 2 em, then the inner element's font size would translate to 200 px (as in this case 1 em = 100 px). The problem with using a unit of measurement that is relative to parent elements is that it increases your code's complexity, as the nesting of elements makes size calculations more difficult.
The recently introduced rem measurement aims to address both em's and px's shortcomings by combining their two strengths; instead of being relative to a parent element, rem is relative to the page's root element.
推薦閱讀
- 每天5分鐘玩轉(zhuǎn)Kubernetes
- Implementing Cisco UCS Solutions
- Linux實戰(zhàn)
- 嵌入式Linux系統(tǒng)開發(fā):基于Yocto Project
- 曝光:Linux企業(yè)運維實戰(zhàn)
- Windows Server 2012網(wǎng)絡操作系統(tǒng)企業(yè)應用案例詳解
- 網(wǎng)絡操作系統(tǒng)管理與應用(第三版)
- 竹林蹊徑:深入淺出windows驅(qū)動開發(fā)
- Microsoft Operations Management Suite Cookbook
- Windows 7案例教程
- INSTANT Galleria Howto
- Advanced Infrastructure Penetration Testing
- Windows網(wǎng)絡編程(第2版)
- 辦公自動化教程(Windows7+Office2010)
- 計算機應用基礎(Windows 7+Office 2010)