Horizontal centering of CSS floating elements

Source: Internet
Author: User

For the fixed width of non-floating elements we can use margin:0 auto; To center horizontally, we also have a common technique to solve the problem of horizontal centering for variable-width floating elements. The decomposition is as follows:

1. HTML part:

<div class="box">
<p> I was floating </p>
<p> I am also the center </p>
</div>

2. CSS part:

. box{
Float:Left
position:relative;
    left:< Span style= "color: #0000ff;" >50%;
"
P{
    float:left;
    position:< Span style= "color: #000080; Font-weight:bold; " >relative;
    right:< Span style= "color: #0000ff;" >50%;
/span>

So it seems simple, the parent and child elements are floating at the same time, then the parent element moves relative to the left by 50%, then the child element moves relative to the right 50%, or the child element moves relative to the left-50%. So simple and so magical.

Horizontal centering of CSS floating elements

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.