<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table>
<tr>
<th>
<div>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem a rerum alias labore, sed dolorum
at debitis sint tenetur velit fugit officiis id eos provident quae ipsum ea? Doloremque,
quaerat?</p>
</div>
</th>
<th>
<div>
<p>lorem ipsum</p>
</div>
</th>
</tr>
</table>
</body>
</html>
Solution 2 :
Please add this CSS code.
th div p {
max-height: 115px;
}
div {
vertical-align: middle;
display: table-cell;
}
I have a table with some cells with enough text content that they need to be scrollable and some cells that have content only taking up one line of text. The problem is, I need to use a div for my use case and can’t seem to center the text vertically while keeping the scrollable cells working as intended.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table>
<tr>
<th>
<div>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem a rerum alias labore, sed dolorum
at debitis sint tenetur velit fugit officiis id eos provident quae ipsum ea? Doloremque,
quaerat?</p>
</div>
</th>
<th>
<div>
<p>lorem ipsum</p>
</div>
</th>
</tr>
</table>
</body>
</html>
I have tried using
display: flex;
align-items: center;
in the div, but that makes the top portion of the scrollable div to be hidden above the top of the cell…
How can I alter my CSS to uniformly allow single line cells to be centered vertically, but scrollable cells to work the way they do now?
Comments
Comment posted by Paul T.
Trying the runnable example above, I’m not following what the issue seems to be? Looks ok to me.
Comment posted by YangTegap
@PaulT. So, the lorem ipsum in the right cell is at the top of the cell. I want it to be centered vertically, but when I try doing that, it cuts off the top of the content in my left cell
Comment posted by Sunil
try div {height: 100%; vertical-align: middle; }
Comment posted by Temani Afif
the duplicate I gave in your old question aready gives you the reason and the fix