Objective
In the overall layout of the page, the size of the page elements (length, width, padding, and so on) and page font sizes is also one of the important tasks. A reasonable setting, it will make the page look hierarchical, focus on bright, pleasing. Conversely, an unfriendly page size and font size setting will increase the complexity of the page and increase the user's understanding of the page, even in the current access to the terminal (IPhone, IPad, PC, Android ... In an endless stream of today, to adapt to a variety of access terminals, will become the hands of a "hot potato." So in recent years, "nine Gongge" style "flow Layout" return again. To provide page layout, and its maintainability, extensibility, we try to set the size of the page element and the font size to relative values, no longer an orphaned fixed pixel point. So that it can change the size of the parent element, the child element can also adapt to the change. And with a small number of the latest CSS3 @media query, to achieve "responsive layout", bootstrap such CSS framework trend rise.
However, in CSS, the documents of the company are divided into two categories: relative length units and Absolute length units.
However, relative length units according to different reference elements, but also can be divided into font relative units and Windows relative units
The relative units of the font are: Em, ex, CH, REM;
Windows relative units include: VW, VH, Vmin, Vmax several
Absolute positioning is fixed dimensions, which are in physical units of measure: cm, MM, in, PX, PT, and PC. In practical applications, however, we use the widest range of EM, REM, PX, and percent (%) to measure the size of page elements.
PX: Absolute (fixed) unit
cons : Fixed values in any case, resulting in device misalignment at different sizes
%: Position relative to the parent (self) size in relation to the position (there will be a change in influence)
Disadvantages : Can determine the range or relatively good calculation, for not too good to determine the bad use of the local percentage, and will lead to deformation, high general bad control
EM: Relative positioning (changes in impact) em= current font size
disadvantage : Depending on the current container font size changes, if each container font size is inconsistent, then the calculation will be very troublesome
Rem
R==root
em= Font Size Font-size
REM Relative units
Depends on HTML font size only
Can think of it no shortcomings! Anyway, I really like it personally.
Dynamically modifying the font-size of the root element:
Need to control by JS:
/* After obtaining the page width, dynamically modify the HTML fontsize* 320 is the page width under the iphone5 design, after setting the page on the iphone5 width screen html* font-size will change to 20px, that is 1rem = 100px 1px=0.05rem* So when setting the element size, if the design draft 15px should be set to size (0.05*15) rem = 0.75rem */! ( Function (Doc, win) { var docele = doc.documentelement, evt = "onorientationchange" in window? "Orientationchange": "Resize", fn = function () { var width = docele.clientwidth; Console.log (width) width && (docEle.style.fontSize = (width/320) + "px"); }; Win.addeventlistener (EVT, FN, false); Doc.addeventlistener ("domcontentloaded", FN, false); } (document, window));
The above file is introduced into the current project,
The code is as follows:
<! DOCTYPE html>Run the code to see the effect!!
Mobile Unit Introduction