Css achieves horizontal vertical center of unknown height, while css achieves horizontal vertical center of unknown height

Source: Internet
Author: User

Css achieves horizontal vertical center of unknown height, while css achieves horizontal vertical center of unknown height

In page design, it is often necessary to implement horizontal and vertical center of elements. There are many css implementation methods (such as: margin: auto, position positioning, and css expressions calc () you can use css preprocessing, table, and so on to achieve horizontal center), but most of them are for situations where the container height is not fixed and the element height is fixed.

Here we will introduce several ways to achieve horizontal vertical center when the container width and element width and height are not fixed.

Github code snippets: https://github.com/haozhaohang/library/tree/master/%E6%B0%B4%E5%B9%B3%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%ADdemo

I. flex achieves horizontal and vertical center

1 <! DOCTYPE html> 2 

Container settings display: flex;

Set jusify-content: center for elements in the container to realize horizontal center

Align-items: center; vertical center

 

2. grid achieves horizontal vertical center (this may be the simplest css style for horizontal vertical center)

1 <! DOCTYPE html> 2 

Currently, the browser's support rate can be used in an internal management system to run on a specified browser.

Container settings display: grid;

Set margin: auto for the container element to align horizontally and vertically.

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.