The perfect centering scheme of the flex implementation sub-blocks
Parent block using display:flex
attributes, child block margin adaptive can be implemented
Code
Index.html
<! DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>Block element vertically centered (known height)</title> <style>*{margin: 0; padding: 0; } . Parent{ display: flex; height:px; background: #03A1FA; } . Child{ width: + px; Height: + px; margin: auto; background: #BB3713; } / * This relies on setting the "margin" value to "auto" value to automatically get the remaining space in the telescopic container. So setting the vertical margin value to "Auto" allows the telescopic item to be fully focused on both upper axes of the telescopic container. */ </style></head><body> <div class="parent"> <div class="Child">Ff</div> </div></body></html>
Horizontal vertical centering scheme with unknown width and height elements
Use the Transform:translate property to adjust
Code
Index.html
<! DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>Block element horizontally vertically centered (unknown width height)</title> <style>*{margin: 0; padding: 0; } / * Simplified version reset, working environment not recommended * / #container{ position:Absolute ; top:% ;Left :% ; -webkit-transform: Translate (-%,-) ; -ms-transform: Translate (-%,-) ; -o-transform: Translate (-%,-) ; transform: Translate (-%,-) ; background-color: #A4FF00 ; color:rgb ( 172, 134) ;} / * Because we do not know the width height, we mainly use the Translate property to pull back the position on the XY axis of the Unknown element center * / /* If the element with the unknown height is only centered, it can be implemented by using Transform:translatey (-50%) */ </style></head><body> <div id="container">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae Obcaecati Expedita ducimus, rem laborum veniam qui quo facilis enim. Repellat Blanditiis Nemo, Magnam, Sequi illum perferendis consequatur Modi maiores quaerat?</div></body></html>
Flex implements horizontal vertical center of page scheme
Define the parent element of the center element Justify-content and Align-items properties are center, you need to set enough height, otherwise only the horizontal center effect
Code implementation
Index.html
<! DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8"> <title>Flex achieves perfect centering of sub-blocks</title> <style>*{ margin: 0; padding: 0; } . Parent { display: flex; justify-content: center; align-items: center; Height: +px; background: #03A1FA; } . Child { width: + px; Height: + px; background: #BB3713; } </style> </head> <body> <div class="parent"> <div class="Child">La La Demanue</div> </div> </body></html>
The center of the page Element _ (4) Use the CSS3 attribute to center the element horizontally vertically