CSS Layout (top)
1, CSS layout of the display1.1, dispaly
Dispaly is the most important property in CSS for controlling layout, each element has a default display, and the default value for most elements is usually block (block-level element) or inline (inline element).
Another common display is none. The default value for some special elements is it, such as script, link, and so on.
1.2 Display:none and Visibility:hidden
Display is set to none, and the space that the element should be displayed is not saved, but Visibility:hidden is preserved.
<div style="width: 100px; height: 100px; border: 1px solid red;float:left;"> <span style="display:none;">ABCD</span>EFG</div><div style="width: 100px; height: 100px; border: 1px solid red;float:left;"> <span style="visibility:hidden;">ABCD</span>EFG</div>
ABCDEfgABCDEFG 1.3, more display values
More commonly used are list-item,inline-block,table,table-cell,flex and so on.
The full list is as follows:
none inline block contents list-item inline-block inline-table table table-cell table-column table-column-group table-footer-group table-header-group table-row table-row-group flex inline-flex grid inline-grid ruby ruby-base ruby-text ruby-base-container ruby-text-containerrun-in/* Global values */display: inherit;display: initial;display: unset;
1.4 The display properties that can be overwritten
Although each element has a default display, we can rewrite it anytime and anywhere, such as modifying the LI element to inline-block and creating a horizontal menu.
2, Element Center 2.1, Horizontal Center
Horizontal centering is achieved by setting margin to auto, provided that the element must have a width
<div style="width:400px;margin:0 auto;height:10px;border:1px solid red;"></div>
2.2, Vertical Center
Because the cell of table can be set vertically, so play can simulate such effect
<div style="width: 400px;height: 200px;border: 1px solid red;display: table-cell; vertical-align: middle;"> <div style="width:100px; height:100px;background: blue;"></div></div>
2.3. Absolute centering
Knowing that the center is centered horizontally and vertically, the absolute center is easier to implement. Combine:
<div style="width: 200px;height: 200px;border: 1px solid red;display: table-cell; vertical-align: middle;"> <div style="width:100px; height:100px;background: blue;margin:0 auto;"></div></div>
Is there a better way? As follows:
By setting the Position:absolute, the top, bottom, left, and right values are 0,margin:auto, and the absolute centering is achieved. If you want to center relative to the container, set the container's position to relative.
<div style="width: 200px;height: 200px;border: 1px solid red; position:relative;"> <div style="width:100px; height:100px;background: blue;margin:auto;position:absolute;top:0;left:0;bottom:0; right: 0;"></div></div>
3. Box model
The box model (box-sizing) has two typical values, namely Content-box,border-box.
3.1, Content-box
At this point, the width of the element is set to the content width, then the element occupies a width of: width + border * 2 + padding * 2 + margin * 2. Width same
3.2, Border-box
At this point, the width of the element is set to include the width of the border, then the total width is 2. Content width is width-padding * 2-border * 2.
3.3 Example
<div style="width:100px; margin: 10px; padding: 15px; border: 5px solid blue; box-sizing:content-box"></div><div style="width:100px; margin: 10px; padding: 15px; border: 5px solid blue; box-sizing:border-box"></div>
3.4. Browser compatibility
To ensure browser compatibility, you need to add a specific browser prefix.
4. Element positioning
If you want to implement more complex layouts, then you need to understand the next position.
4.1, Position:static
Static is the default value of the Position property, and position:static elements are not specifically positioned.
4.2, Position:relative
Setting top, right, bottom, and left on relative positioning (relative) causes it to deviate from its normal position, and other elements will not adjust the position to compensate for the remaining gap after it deviates.
<div style="border:1px solid red; width: 400px; height: 200px;"> <div style="background: blue; width:100px; height: 100px;"></div> ABCDE</div><div style="border:1px solid red; width: 400px; height: 200px;"> <div style="background: blue; width:100px; height: 100px;position:relative; left: 100px;top:50px;"></div> ABCDE</div>
ABCDE ABCDE4.3, position:fixed
Fixed positioning elements are positioned relative to the window, so even if the page scrolls, it will remain in the same position. For example, look at the lower left corner.
<div style="width: 100px;height:100px; position:fixed; bottom: 0; right: 0;border: 1px solid red;">固定定位</div>
Fixed positioning 4.4, Position:absolute
The absolute positioning Element (absolute) is similar to fixed, but it is not relative to the window, but rather to the nearest positioned (the position value is not the static element is the positioned element) ancestor element, if there is no such ancestor element, Then it is relative to the BODY element of the document and scrolls as the page scrolls.
<div style="border:1px solid red; width: 400px; height: 200px;position:relative;"> <div style="background: blue; width:100px; height: 100px;position:absolute;top: 25px;right:25px;"></div></div>
CSS Layout (top)