Please check this simple solution without additional markup:
.vertically-centered { border: 1px solid red; height: 6rem; overflow: hidden; font-weight: bold; font-size: 2.5rem; text-overflow: ellipsis; width: 300px; line-height: 1.2; display: flex; flex-direction: column; justify-content: space-around; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; -webkit-box-pack: center; }
<div class="vertically-centered">vertically centered with</div> <div class="vertically-centered">vertically centered with hello</div> <div class="vertically-centered">one line</div>
You can also see this solution in action in the
edited CodePen example .
How it works: your source code already uses the Flexbox layout for WebKit-based browsers (this is actually the obsolete Flexbox 2009 syntax, but unfortunately -webkit-line-clamp doesn’t work with new implementations). Flexbox has its own vertical centering mechanism. All you need to get the desired behavior in WebKit-based browsers is to remove the :after .vertically-centered add the following line of code to .vertically-centered instead:
-webkit-box-pack: center;
For other modern browsers, such as Firefox 22+ and IE11 +, you can use the same layout (except for the ellipse, but you said it was fine) is achieved using the new Flexbox version:
display: flex; flex-direction: column; justify-content: space-around;
The code should indicate display: -webkit-box above, so Webkit browsers can still use -webkit-line-clamp .
You can also make it work in IE10 by adding its prefix version of Flexbox (2011 transitional syntax):
display: -ms-flexbox; -ms-flex-direction: column; -ms-flex-pack: center;
A handle with IE10 support is here: http://codepen.io/anon/pen/otHdm
The vertical centering :after approach did not work for you in the first cell for the following reason: for ilnine-level CSS fields (e.g. plain text, inline blocks, images, etc.) vertical-align adjusts the baselines of all such boxes that form string field (see CSS2.1 specification ). The line height of the line is calculated so that all the lines of the line level in the line fit into it, so the line height of the line cannot be less than the height of the highest built-in box. Thus, your built-in :after block, which receives 100% of the container’s height, becomes the highest element in the last line of your block, and because of vertical-align: middle source text in this last line moves in accordance with the vertical middle of the text with the middle of this tall built-in unit. This is normal when there is only one lime (this is a typical use case for such centering), and when the last line is hidden with overflow , but not OK when it is visible).