I am attempting to vertically center :before
content within a table cell. The following is the only approach that I have found that actually vertically centers the pseudo-content. However, when I use this approach, the :before
content hangs below the end of the table, which allows the user to scroll beyond the end of the table (my containing div must be scrollable, so I can't just do overflow: hidden
). Is there a way to vertically center the :before
content in all resizing conditions, without extending beyond the bottom of the table?
div {
border: 1px solid #000;
overflow: auto;
resize: both;
width: 250px;
}
table {
border: 1px solid #0F0;
vertical-align: middle;
}
td:first-child {
padding-left: 20px;
position: relative;
}
td:first-child::before {
border: 1px solid #F00;
content: ">";
height: 100%;
left: 0;
position: absolute;
top: -11px;
transform: translate(0, 50%);
}
<div>
<table>
<tr><td>First element that can wrap to multiple lines</td><td>Nowrap</td></tr>
</table>
</div>
I have tried everything I can think of. This includes pretty much all vertical-align
settings, including percentages, but these are all ignored because the element isn't inline
. Then I tried setting the display
property of the :before
content to inline
, table-cell
, inline-block
, block
; All settings seem to be ignored in this context, making vertical-align
worthless. I have also tried to set both margin-top
and padding-top
to a percentage, neither of which worked, and also tried setting line-height
thinking that may have an affect, which it doesn't.
Does anyone know how to adjust the provided code such that the caret is always vertically centered, without allowing scroll beyond the end of the table?