1.0 KiB
1.0 KiB
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Display table centering | snippet | css |
|
malibu | 2020-12-30T15:37:37+02:00 |
Vertically and horizontally centers a child element within its parent element, using display: table.
- Use
display: tableto make the.centerelement behave like a<table>element. - Set
heightandwidthto100%to make the element fill the available space within its parent element. - Use
display: table-cellon the child element to make it behave like a<td>elements. - Use
text-align: centerandvertical-align: middleon the child element to center it horizontally and vertically. - The outer parent (
.container) must have a fixedwidthandheight.
<div class="container">
<div class="center"><span>Centered content</span></div>
</div>
.container {
border: 1px solid #9C27B0;
height: 250px;
width: 250px;
}
.center {
display: table;
height: 100%;
width: 100%;
}
.center > span {
display: table-cell;
text-align: center;
vertical-align: middle;
}