Use translate to set elements vertically horizontally centered

Source: Internet
Author: User

Before the mobile phone project, using a lot of custom pop-up window, and then asked to be vertical horizontal to the center, the beginning of the time to use Calc to calculate, but CSS3 Calc compatibility is not very good, so later on the use of JS to calculate,

Today by chance to see someone else's way, instantly feel that they have done a lot of useless ~ ~

Translate,translate,translate,translate.

Simple CSS code can be implemented:

. align{width:300px;       height:300px;       border:5px solid #666;       Position:absolute;       Left:%;       Top:%;        -webkit-transform:translate (-%,-)  //This is the key} 

And after testing,translateThe percentage used in the function is the content area of the element, the filler (padding), Border (border) is calculated for the standard, which means that no matter what padding or border we add will not affect the outcome.

At the same time, in terms of animation, translate   to animate smoother than an absolutely-positioned animation.

The reason for this is that an animated effect using absolute positioning is subject to displacement using pixels (px), whereas translate animations using functions can be shifted in smaller units without being affected by the pixels.

In addition, an absolutely-positioned animation effect is calculated entirely using the CPU, while using theThe translate animation of the function is the use of the GPU, so the visual use translate of the function animation can have a higher number of frames than an absolute positioning animation.

Use translate to set elements vertically horizontally centered

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.