Inline element Horizontal centering scheme
- The inline element is wrapped in an attribute display as the parent element of the block, and the parent block is
text-align:center implemented
Code implementation
Index.html
<! DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8"> <title>Inline elements are centered horizontally</title> <style type="Text/css"> P { display: ineline-block; } . Testdiv { text-align: center; width: px; height: px; background: rgb ( 243, 145); } / * Applicable elements: text, links, and other inline or inline-* type elements (inline-block,inline-table,inline-flex) * / </style> </head> <body> <div class="Testdiv"> <span>I am the Test text</span> <b>| me too</b> <p>I was testing, too.</P> </div> </body></html>
A block element horizontally centered scheme
Set the margin of the block for auto, up and down margin can be set according to demand{margin:0 auto}
Code implementation
Index.html
<! DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8"> <title>Horizontal center of one block element</title> <style type="Text/css"> #container { width: + px; Height: + px; background: RGB (129, $) ; margin: 0 auto; } </style> </head> <body> <div id="container"> </div> </body></html>
Multi-block element horizontal centering scheme
- Refer to the first implementation, internal Div set to inline block, wrap block settings
text-align:center
Code implementation
Index.html
<! DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8"> <title>Multiple block elements are centered horizontally</title> <style type="Text/css"> #container { text-align: center; background: rgb (+, 188, ); width: px; height: px; } #testDiv1 { display: inline-block; width: + px; Height: + px; background: #03232A; } #testDiv2 { display: inline-block; width: + px; Height: + px; background: #122A03; } #testDiv3 { display: inline-block; width: + px; Height: + px; background: #189FBD; } #testDiv4 { display: inline-block; width: + px; Height: + px; background: #E20FAD; } </style> </head> <body> <div id="container"> <div id="TestDiv1"></div> <div id="TestDiv2"></div> <div id="TestDiv3"></div> <div id="TestDiv4"></div> </div> </body></html>
Center of the page element _ (1) element is centered horizontally