Next, I will introduce several ways to horizontally center html elements one by one
1: Text align: Center; for inline elements
2: Using margin:0 auto; to realize horizontal center display
3: Implement with table
4, Block level elements, however, are horizontally centered by converting them to inline elements
5: Both parent and child elements adopt relative positioning, the parent element left:50%; the child element left:-50%; the relative length is reduced by 50%, so that more parts can be pulled back after the right offset
6: Using css3's flexbox, display:flex;
7: Use the display:relative; of the parent element to directly use position:absolute;left:0;right:0;margin:auto to realize horizontal to center
Here are the specific codes explained:
- <!DOCTYPE html>
- <!--
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
- -->
- <html>
- <head>
- <title>css Horizontal center of</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width">
- <style type="text/css">
- #page{
- width: 100%;
- color: white;
- }
- .he{
- width: 100%;
- height: 100px;
- background: #625050;
- text-align: center;
- line-height: 100px;
- }
- .bo{
- width: 100%;
- background: #6aa087;
- }
- .fo{
- height: 100px;
- background: #2f5d34;
- }
- .content{
- height: 100px;
- border: 2px solid #fff;
- }
- .content1{
- background: #66a05c;
- text-align: center;
- }
- .content2{
- background: #8a5841;
- text-align: center;
- }
- .content2Bo{
- height:50px;
- width: 60%;
- border: 2px solid red;
- line-height: 50px;
- margin: 0 auto;
- }
- .content3{
- background: #2f5d34;
- }
- table{
- margin: 0 auto;
- }
- .content4{
- background: #8a5841;
- text-align: center;
- }
- .contentBo4{
- display: inline;
- }
- ul li{
- list-style-type: none;
- }
- .content5{
- float: left;
- position: relative;
- left: 50%;
- }
- .contentBo5{
- position: relative;
- left: -50%;
- background: #231b40;
- }
- .content6{
- width: 100%;
- text-align: center;
- background: #9ca05c;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .content7{
- position: relative;
- }
- .contentBo7{
- position: absolute;
- left: 0;
- right: 0;
- width: 80%;
- border: 2px solid red;
- margin: 0 auto;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <div id="page">
- <div class="he">
- <h1>Here are a few examples of horizontal alignment of elements</h1>
- </div>
- <div class="bo">
- <div class="content content1">
- This is content area 1: realize the horizontal and central display of elements in the row Use text-align:center;How.
- </div>
- <div class="content content2">
- <div class="content2Bo">
- This is content area two:Red area part adopts margin:0 auto;To achieve horizontal center display, of course, to write the width of the elements.
- </div>
- </div>
- <div class="content content3">
- <table><tbody><tr><td>
- This is content area 3: use table realization.
- </td></tr></tbody></table>
- </div>
- <div class="content content4">
- <ul class="contentBo4">
- <li>This is the first line</li>
- <li>This is the second line</li>
- <li>This is content area four: originally contentBo4 The block level elements of the are converted to inline elements to achieve horizontal centering of the block level elements.</li>
- </ul>
- </div>
- <div class="content5">
- <div class="content contentBo5">
- This is content area 5: the parent and child elements are relatively positioned, and the parent element left:50%;Subelements left:-50%;Reduce your length back to 50%,This enables you to pull back more parts after offsetting to the right.
- </div>
- </div>
- <div class="content content6">
- This is content area six:Use css3 Of flexbox,display:flex;
- </div>
- <div class="content content7">
- <div class="contentBo7">
- This is content area seven:With parent element display:relative;Direct adoption position:absolute;left:0;right:0;margin:auto To achieve horizontal to center.
- </div>
- </div>
- </div>
- <div class="fo">
- <pre> In conclusion, there are only several ways to achieve the level of middle:
- 1: Centering for the simplest inline elements text-align:center;Just set it up.
- 2: Use absolute positioning after setting for the most common horizontal center left: 50%;After that, various methods are used to make up the level difference.
- 3: For ordinary element alignment, you can also use absolute positioning after left:0;right:0;With the width of the element, you can use margin:auto;Horizontal alignment is achieved.
- 4: use css3 Of Flexbox attribute
- 5:Nesting outside elements table Implementation, but there will be many layers of nesting
- 6: marin:0 auto;Convenient horizontal centering of known width
- </pre>
- </div>
- </div>
- </body>
- </html>