How to achieve horizontal centering of variable width in css

青灯夜游
Release: 2020-12-21 15:56:41
Original
2110 people have browsed it

Method: 1. Use flex layout and set both ustify-content and align-items attributes to center to achieve centering; 2. Use transform and position attributes to achieve centering; 3. Use table-cell and use Table cell centering effect.

How to achieve horizontal centering of variable width in css

The operating environment of this tutorial: Windows 7 system, css3 version. This method is suitable for all brands of computers.

Recommended: "css video tutorial"

css achieves horizontal centering of variable width

Method 1: Use flex

Everyone’s first reaction may be flex. Because its writing method is simple and intuitive enough, and there is no problem with compatibility. It is the first choice for centering on mobile phones.

horizontal and vertical

Copy after login
.wrapper {
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
}
.flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
}
Copy after login

Using two key attributes: justify-content and align-items, both are set to center to achieve centering.

It should be noted that the flex-center here must be hung on the parent element so that the only child element can be centered.

Method 2: Use transform position

This combination is often used to center the image.

Copy after login
.wrapper {
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
    position: relative;
}
.wrapper > img {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
Copy after login

Of course, you can also move the relative positioning of the parent element wrapper into the child element img and replace the absolute positioning. The effect is the same.

Method 3: table-cell

Use the table's cell centering effect to display. Like flex, it needs to be written on the parent element.

horizontal and vertical

Copy after login
Copy after login
.wrapper {
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
Copy after login

Method 4: grid

Like a table, a grid layout allows us to align elements by rows or columns. However, in terms of layout, grids are more possible or simpler than tables.

horizontal and vertical

Copy after login
Copy after login
.wrapper {
    width: 300px;
    height: 300px;
    border: 1px solid #ccc;
    display: grid;
}
.wrapper > p {
    align-self: center;
    justify-self: center;
}
Copy after login

But it is not as good as flex in terms of compatibility, especially the IE browser, which only supports IE10 and above.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to achieve horizontal centering of variable width in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!