This article will introduce you to multiple CSS centering solutions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
(Learning video sharing: css video tutorial)
To center an inline element (such as a link, span
or img
), use text-align: center
is enough.
<div class="desk"> <span class="plate"></span> </div>
.desk { text-align: center; }
For multiple inline elements, you can also use text-align: center
:
<div class="desk"> <span class="plate"></span> <span class="plate"></span> </div>
.desk { text-align: center; }
Using flexbox you can also quickly center elements:
.desk { display: flex; justify-content: center; }
It also works fine for multiple inline items:
When using a grid container, the plates in the diagram will be centered based on their grid area. Note that this won't work with multiple plates unless you wrap them in a single element.
.desk { display: grid; justify-content: center; }
Width and height are known Block elements can be centered by setting margin-left:auto
and margin-right:auto
.
.plate { width: 120px; height: 120px; margin-left: auto; margin-right: auto; }
For multiple block elements, they should be wrapped in a single element and then have this parent element centered.
.tray { display: flex; margin-left: auto; margin-right: auto; }
For flexbox, justify-content:cente
r is also used to center the element:
.desk { display: flex; justify-content: center; }
For multiple elements, we don’t need to wrap them in one element, flexbox can center them all.
With absolute positioning, we can easily center it horizontally via CSS transform
.
.plate { position: absolute; left: 50%; transform: translateX(-50%); }
When the width of the element is known, negative margins can be used instead of CSS transform.
.plate { position: absolute; left: 50%; margin-left: -60px; }
One of the easiest ways to vertically center an element is to use padding
:
padding-top: 24px; padding-bottom: 24px; }
# The ##vertical-align attribute can be used on one or more elements.
.desk { text-align: center; } .plate, .fork, .knife { vertical-align: middle; }
.desk { display: flex; justify-content: center; align-items: center; }
transform to center the element vertically:
.plate { position: absolute; top: 50%; transform: translateY(-50%); }
transform.
.plate { position: absolute; top: 50%; margin-top: -60px; }
align-items.
.desk { display: grid; align-items: center; }
.plate { text-align: center; padding-top: 24px; padding-bottom: 24px; }
.plate { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); }
justify-content:center and
align-items:center:
.plate { display: flex; justify-content: center; align-items: center; }
place-items attribute, which combines
justify-content and
align -items:
.desk { display: grid; place-items: center; }
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Several options for CSS centering (summary). For more information, please follow other related articles on the PHP Chinese website!