CSS in the image using absolute positioning to achieve the center effect [second article]

Source: Internet
Author: User

Yesterday in the knowledge of various look at the front end of the post, accidentally and learned a skill, again I moved to share with you.

Still the same problem:

How do I get a div centered in the parent element?

In the previous article we used the absolute positioning Position:absolute;

Combining the negative value of margin achieves the center effect.

The second way of solving

Here we look at the new approach:

Html:

   <div class= "Container" >

<div class= "box" ></div>

</div>

Css:

. container{

width:300px;

height:300px;

position:relative;

}

. box{

width:50px;

height:50px;

Margin:auto;

Position:absolute;

top:0;

bottom:0;

left:0;

right:0;

}

It's done! ('? ') )

Principle

There's no margin to count! This method directly fix the centering problem.

We set the absolute positioning of box, up and down are 0;

Then the box will be confused, in the end where to go???

Forget it, I'll stay in the middle, qaq.

Think about it and suddenly think this box is cute, hahaha.

CSS in the image using absolute positioning to achieve the center effect [second article]

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.