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.