div{height: 90px;width: 90px;text-align: center;border: 1px solid silver;display: table-cell; // This says treat this element like a table cellvertical-align:middle; // Now we can center vertically like in a TD}
还有一种替代的居中方法:不为flex容器的主轴和十字轴上的分布指定center,而是在flex项目上指定margin: auto以占用所有四个方向的所有额外空间,均匀分布的边距将使flex项目在所有方向居中。除非有多个flex项目,否则这种方法有效。此外,这种技术适用于MS Edge,但不适用于Internet Explorer 11。
2016/2017年更新:
它可以更常用于transform,即使在Internet Explorer 10和Internet Explorer 11等旧浏览器中也能很好地工作。它可以支持多行文本:
In supported browsers, set the display of the targeted element to flex and use align-items: center for vertical centering and justify-content: center for horizontal centering. Just don't forget to add vendor prefixes for additional browser support (see example).
In some cases, you will need to ensure that the html/body element's height is set to 100%.
For vertical alignment, set the parent element's width/height to 100% and add display: table. Then for the child element, change the display to table-cell and add vertical-align: middle.
For horizontal centering, you could either add text-align: center to center the text and any other inline children elements. Alternatively, you could use margin: 0 auto assuming the element is block level.
This approach assumes that the text has a known height - in this instance, 18px. Just absolutely position the element 50% from the top, relative to the parent element. Use a negative margin-top value that is half of the element's known height, in this case - -9px.
In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a line-height value on the child element equal to the fixed height of the parent element.
Though this solution will work in some cases, it's worth noting that it won't work when there are multiple lines of text - like this.
# Parent{display:table;}
# Child{display: table-cell;width: 100%; // As large as its parent to center the text horizontallytext-align: center;vertical-align: middle; // Vertically align this element on its parent}
<div class="mydiv"><p>Item to be centered!</p></div>
css:
.mydiv {height: 100%; /* Or other */position: relative;}
.mydiv p {margin: 0;position: absolute;top: 50%;left: 50%;margin-right: -50%;transform: translate(-50%, -50%); /* To compensate own width and height */}
<div><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Haec et tu ita posuisti, et verba vestra sunt. Non enim iam stirpis bonum quaeret, sed animalis. </span></div>
.centered {width: 150px;height: 150px;display: flex;align-items: center;justify-content: center;text-align: center;background: red; /* Not necessary just to see the result clearly */}
<div class="centered">This text is centered horizontally and vertically</div>
.parent {display: flex;/* You can change this to `row` if you want the items side by side instead of stacked */flex-direction: column;}
/* Change the `p` tag to what your items child items are */.parent p {margin: auto;}
.center-horizontally{justify-content: center;}
<Card.Footer className="card-body-padding center-horizontally"><label className="support-expand-text-light">Call or email Customer Support to change</label></Card.Footer>