How CSS allows floating elements to be horizontally centered _javascript tips

Source: Internet
Author: User
Tags visibility

For fixed-width, non-floating elements we can use margin:0 Auto to center horizontally in CSS, and we also have a common technique for variable-width floating elements to solve its horizontal center problem. There are a number of ways to solve the problem of horizontal center, let's share some three methods, hoping to help everyone.

Method One:

1, HTML part:

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

2, the CSS section:

. box{
 Float:left;
 position:relative;
 left:50%;
}
p{
 Float:left;
 position:relative;
 right:50%;
}

So it seems very simple, the parent element and child element float at the same time, then the parent element moves relative left 50%, then the child element moves relative to the right 50%, or the child element moves relative to the left-50% also can. So simple and so magical.

Method Two:

HTML Code

<! DOCTYPE html>  

The parent element and child element float at the same time, then the parent element moves 50% relative to the left, then the child element moves relative to the left-50%.

Method Three:

HTML code

<! DOCTYPE html>  

Here also can set up a layer of left:-50%, more reasonable, can also avoid some unnecessary IE bugs. Extrapolate, this float element is centered in a way that can be extended to many situations that require floating elements to be centered.

Above through three kinds of methods to explain how CSS let floating element level center, follow-up this site will continue to update on JS, JQ, CSS, PHP, C # and other programming knowledge, please pay attention to this site, thank you.

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.