CSS centering problem

Source: Internet
Author: User

Learning process encountered CSS center problem, also consulted the data, everyone's methods are not the same, and at that time to understand, after the confusion; so as a front-end small white, also to write about the saying that good brains as bad as the written, after all, they know not much, in fact, is holding the attitude of learning and communication. Say not much, Direct (note: The properties in this article are all tested under chrome);

One: A parent box, how to center a child box (highly known)

The simplest one: the use of positioning, the parent box relative, the child box absolute, and then the sub-box left and top values each 50%; When you set the margin value, the left and top values are half the height of the width of the box (son), but negative; look at the code. , right result)

The second kind: the principle is similar to see the code can understand (only CSS3 attribute application)

And, of course, a third way.

Of course, the above is a block-level element, if it is an inline element, to block-level elements, using the second method above; the first and the third do not work;

Here's a demonstration

Two. Highly unknown case: (Since the height is unknown, then a picture to replace it);

1, or use the CSS3 property, since two times, then say transform, is the transformation, through the translate () method, the element is moved from its current position, according to the given left (x-coordinate) and top (y-coordinate);

2. A way to use the Internet, for the time being, I'm back.

For the time being first so much, in the spirit of learning; If you are like the code of that predecessor, please forgive me, the first time to write a blog, roar

CSS centering problem

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.