There are three ways to center the indeterminate width of a block element: 1. Add Table Label Use the length of the table label for self-adaptability ---does not define its length nor the length of the default parent element body (the length of the table depends on the length of its text), so it can be regarded as a fixed-width block element , and then the method of the fixed-width blocky-centered margin is used to center it horizontally. <table> <tbody> <tr><td> <ul> <li> I am the first line of text </li> <li> I am the second line of text </li> <li> I am the third line of text </li> </ul> </td></tr> </tbody> </table> <style> table{ margin:0 Auto; } </style> 2, by setting the child element Display:inline method: Similar to the first, the display type is set to the inline element, the property of the variable width element is set <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>
<style>. container{ text-align:center;} /* Margin:0;padding:0 (eliminates the gap between text and Div border) */.container ul{ list-style:none; margin:0; padding:0; Display:inline; Centered, with three rows }/* margin-right:8px (sets the interval between li text) */.container li{ margin-right:8px; display:inline;//In a row, only one row } </style>
3. 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 : c23>-50% to achieve horizontal centering. <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>
<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>
|