In addition to inserting the table label in the first two sections and changing the display type of the element, you can center the indeterminate-width block element Horizontally, this section describes the 3 ways to achieve this effect, set the float and relative positioning to achieve.
Method Three: By setting float to the parent element and then setting position:relative and left:50% for the parent element , the child elements are set position:relative and left : - 50% to achieve horizontal centering.
We can understand this: supposedly the parent layer of the UL layer (that is, the DIV layer in the example below) has a split line in the middle of the UL layer of the parent layer (div layer) is divided into two parts, the UL layer of the CSS code is the top left of the UL layer and the parent layer of the UL layer (div layer) line alignment While the LI layer's CSS code is to align the Li Layer's split line with the top left of the UL layer (also the split line of the DIV layer) to achieve the center of the Li Layer.
The code is as follows:
<body><div class= "Container" > <ul> <li><a href= "#" >1</a></li> <li><a href= "#" >2</a></li> <li><a href= "#" >3</a></li> </ul></div></body>
CSS code:
<style>.container{ Float:left; position:relative; Left:50%}.container ul{ List-style:none; margin:0; padding:0; position:relative; left:-50%;}. Container li{float:left;display:inline;margin-right:8px;} </style>
These three methods are widely used, each with advantages and disadvantages, the specific choice of which method, depending on the circumstances.
Horizontal Center Summary-variable width block element method (iii)