Horizontal vertical centering based on translate for scenes with a known width and a height that is not fixed

Source: Internet
Author: User

Horizontal vertical center of the project will encounter more, there are many solutions, today just learned to use translate to solve the horizontal vertical center of the scheme, said this scheme before I often solve the horizontal vertical center of a scheme

<style>. Box{position:fixed;top:50%;left:50%;width:100px;Height:100px; margin-left:-50px;margin-top:-50%;background:Red;}</style><Divclass= "box"></Div>

The above scheme must be in the case that the width and height are clear, can be horizontally vertically centered,

If you now only know the width, high is not fixed, need to be based on the content of the elements of the height of the changes in the case, and want to horizontal vertical center, then you can use translate to solve

<style>. Box{position:fixed;Top:50%; Left:50%;width:100px;Transform:Translate ( -50%,-50%);background:Green;}</style><Div>Translate</Div>

Translate moving elements can be summed up in three ways: horizontal movement, vertical movement up and down, diagonal movement

Move horizontally to the right to move translate (x,0); Move translate (-x,0) to the left

Move up and down move translate (0,-y); Move Down translate (0,y);

Move the translate (x, y) move diagonally to the upper-right corner of the translate (x,-y). Move translate (-x,y) to the top left corner move translate (-x,-y)

Horizontal vertical centering based on translate for scenes with a known width and a height that is not fixed

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.