admin管理员组文章数量:1395367
How can I get the "X" in the image below to be vertically align to the middle of the th cell using only css?
I need to do this without adding additional elements (ex putting the th contents into a div) as these elements are generated by a 3rd party library where I can only control the css.
/
.headerCell {
vertical-align: middle;
height: 90px;
width: 200px;
border: 1px solid red;
}
.headerText {
border: 1px solid blue;
width: 150px;
float: left;
}
.headerX {
border: 1px solid blue;
float: right;
}
<table>
<tr>
<th class="headerCell">
<div class="headerText">Text that takes up multiple lines</div>
<div class="headerX">X</div>
</th>
</tr>
</table>
How can I get the "X" in the image below to be vertically align to the middle of the th cell using only css?
I need to do this without adding additional elements (ex putting the th contents into a div) as these elements are generated by a 3rd party library where I can only control the css.
https://jsfiddle/qt0hpnak/
.headerCell {
vertical-align: middle;
height: 90px;
width: 200px;
border: 1px solid red;
}
.headerText {
border: 1px solid blue;
width: 150px;
float: left;
}
.headerX {
border: 1px solid blue;
float: right;
}
<table>
<tr>
<th class="headerCell">
<div class="headerText">Text that takes up multiple lines</div>
<div class="headerX">X</div>
</th>
</tr>
</table>
Share
Improve this question
edited Mar 13 at 13:54
Ori Drori
194k32 gold badges238 silver badges229 bronze badges
asked Mar 13 at 13:51
innominate227innominate227
11.7k1 gold badge20 silver badges22 bronze badges
5
|
3 Answers
Reset to default 1Have .headerX
move down vertically at half of its height. Add the following to .headerX
: translate: 0 50%
.headerCell {
vertical-align: middle;
height: 90px;
width: 200px;
border: 1px solid red;
}
.headerText {
border: 1px solid blue;
width: 150px;
float: left;
}
.headerX {
border: 1px solid blue;
float: right;
translate: 0 50%; /* Move it down by 50% of its own height */
}
<table>
<tr>
<th class="headerCell">
<div class="headerText">Text that takes up multiple lines</div>
<div class="headerX">X</div>
</th>
</tr>
</table>
Inspired by zer00ne answer I ended up using a combination of absolute positioning and translation to get the X centered
.headerCell {
vertical-align:middle;
height:90px;
width: 200px;
border:1px solid red;
position: relative;
}
.headerText {
border:1px solid blue;
width: 150px;
float: left;
}
.headerX {
border:1px solid blue;
top: 50%;
left: 100%;
translate: -100% -50%;
position: absolute;
}
<table>
<tr>
<th class="headerCell">
<div class="headerText">Text that takes up multiple lines</div>
<div class="headerX">X</div>
</th>
</tr>
</table>
https://jsfiddle/o2cyjk4L/
You can format the cell by making it a grid and within that the children can align items using flex.
.headerCell {
display: grid;
grid-template-columns: auto 1fr;
height: 90px;
width: 200px;
border: 1px solid red;
}
.headerCell>* {
display: flex;
align-items: center;
}
.headerText {
border: 1px solid blue;
width: 150px;
}
.headerX {
border: 1px solid blue;
justify-content: center;
}
<table>
<tr>
<th class="headerCell">
<div class="headerText">Text that takes up multiple lines</div>
<div class="headerX">X</div>
</th>
</tr>
</table>
本文标签: cssverticalalign middle in thStack Overflow
版权声明:本文标题:css - vertical-align middle in th - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744696952a2620336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
display: flex
for example. So another harmless approach could be settingposition: relative
on eachth
and then absolute positioning the element you wish to be vertically in the middle close to the right border.. like this:position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
– Diego D Commented Mar 13 at 14:03