Float:center Center Float implementation

Source: Internet
Author: User
Tags relative

We all know Float:left and float:right, but have you ever thought about Float:center? Center float ...

<div id= "macji" >
<ul class= "Macji-skin" >
<li> List One </li>
<li> List Two </li>
<li> List Three </li>
</ul>
</div>

We want to realize that Li is floating and centered (the number of Li is not fixed, the UL width is unknown). can set the UL text-align:center, then set Li's display, can realize center, but this is not our original intention, we need to realize float:center.

Here we have to revisit the position:relative, which offsets the position in the normal document stream based on attributes such as Left,right,top,bottom. Then we can let the UL for position:relative;left:50%, and then let Li like the left float, in Let it position:relative;right:50% (or left:-50%), then Li is like floating in the middle of the center. Don't say much nonsense, try it first.

#macji {
position:relative;
width:100%;
height:80px;
Background-color: #eee;
Text-align:center;
Overflow:hidden;
}
#macji. macji-skin{
Float:left;
position:relative;
left:50%;
}
#macji. Macji-skin li{
position:relative;
right:50%;
Float:left;
margin:10px;
padding:0 10px;
Border:solid 1px #000;
line-height:60px;
}



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.