The Text-align:center attribute is used when the content is centered, but when the inner element is float, the text-align is invalidated.
We can only have other ways to achieve the goal of centering
The code is as follows |
Copy Code |
<! DOCTYPE html> <meta charset= "UTF-8" > <title>float Best Practices in center </title> <style type= "Text/css" > *{margin:0; padding:0;} Ul{list-style-type:none;}
container{width:600px padding:5px; border:2px solid #ddd; margin:30px auto; overflow:hidden; text-align:center;} . container h2{font-size:14px; padding:10px 0;}
. sl-float-middle{position:relative; left:50%; float:left;} sl-float-middle-cell{position:relative; float:left; right:50%; height:30px; line-height:30px; margin:0 5px; border:5px solid #f2f2f2;} . Sl-float-middle-cell img{height:30px;} </style> <body> <div class= "Container" > <ul class= "Sl-float-middle" > <li class= "Sl-float-middle-cell" > Width one </li> <li class= "Sl-float-middle-cell" > Width 222 </li> <li class= "Sl-float-middle-cell" > Three </li> <li class= "Sl-float-middle-cell" ></li> </ul> </div> </body> |
This program is mainly to use the float:left+left:50% of the parent layer of UL, with the float:left+right:50% (left:-50%) of the son Layer Li to achieve the goal.
The HTML code is as follows:
The code is as follows |
Copy Code |
<ul class= "Sl-float-middle" > <li class= "Sl-float-middle-cell" > Width one </li> <li class= "Sl-float-middle-cell" > Width 222 </li> <li class= "Sl-float-middle-cell" > Three </li> <li class= "Sl-float-middle-cell" ></li> </ul> |
The CSS code is as follows:
code is as follows |
copy code |
. sl-float-middl e{position:relative; left:50%; float:left;} . sl-float-middle-cell{position:relative; float:left; right:50%;} |