Invalid solution for text-align after float in CSS

Source: Internet
Author: User

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%;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.