The problem of horizontal and vertical center in html. (Fixed height and height), html Center

Source: Internet
Author: User

The problem of horizontal and vertical center in html. (Fixed height and height), html Center

In the layout process, we need to center some elements. I believe that there is no problem in horizontal ranking, that is, the use

Margin: 0 auto; text center text-align: center.

What I want to tell you is about overall Center (horizontal center and vertical center ),

Here, I will first tell you that I have used the absolute positioning method,

Now I will create a box for you to observe directly, as shown below:

The outer and inner boxes are squares with a side length of PX and PX respectively.

We need to center the inner box. (The height of the inner frame is fixed)

1 When the height is fixed.

We usually perform Code operations like this,

Position: absolute;

Top: 50%;

Left: 50%;

At first glance, there is no problem, but after the operation, we will find

The inner box is not centered. In this case, we ignore that the inner box is a shape and not a line.

To solve this problem, we need to use the negative value of margin to move the whole, and the moving value is half the length of the border.

The Code is as follows:

Position: absolute;

Top: 50% margin-top:-50px;

Left: 50% margin-left:-50px;

The effect is as follows:

The use of negative values can bring the label closer to the parent element, so that we can achieve the center effect.

2. When the height is not fixed.

In fact, the principles of the two are the same, but in a highly irregular manner, we can not select half of the height, just change part of the Code:

Position: absolute;

Top: 50%;

Left: 50%;

Transform: translate (-50%,-50% );

In this way, the center effect is also completed.

Let's try it.

 

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.