简体   繁体   English

垂直对齐表中绝对定位的td的内容

[英]Vertical align the content of absolutely positioned td in table

I have a absolutely positioned td (second td), I want it to align at vertical center even there is content changes in the first td. 我有一个绝对定位的td(第二个td),即使第一个td中有内容更改,我也希望它在垂直中心对齐。

I tried using position: relative and transform: properties and all, but nothing seems to worked out. 我尝试使用position:relative和transform:属性以及所有属性,但似乎没有任何效果。

This is my code, and you can see the record payment buttons are not vertically centered. 这是我的代码,您可以看到记录付款按钮未垂直居中。

 table { width: 600px; font-size: 14px; color: black; font-family: sans-serif; background-color: white; margin-left:0%; border-style: solid;border-color:#eee; overflow-x:scroll; display:table; } table, th, td { border-width: thin; border-collapse: collapse; border-style: solid;border-color:#eee; padding: 20px 10px; } td { text-align: left; } .hover-icons{ position: absolute; right:30px; } .hover-icons ul{ position: absolute; border: 0; top: 50%; right: 0; transform: translate(5%, 33%); background: #EFEFEF; height: auto; display: none; } td{padding:0px 0} .hover-icons ul{ display: flex; } .hover-icons ul li{ margin: 0 6px; } .hover-main-icons .posi-r{position: relative;} .btn-hover-i{ } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"></link> <table class="hover-main-icons"> <tr> <td>test test<br> test<br> test<br>test<br> testtesttest testt<br>esttest<br>testt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> <tr> <td>test test test test<br>test<br> testtesttest testt<br>esttesttestt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> </table> 

Is there any way to place buttons that are vertically centered? 有没有办法放置垂直居中的按钮?

You need to change 你需要改变

.hover-icons{
  position: absolute;
  right: 30px;
}

to

.hover-icons{
 position: relative;
 right:25%;
}

Here is the working snippet: 这是工作片段:

 table { width: 600px; font-size: 14px; color: black; font-family: sans-serif; background-color: white; margin-left:0%; border-style: solid;border-color:#eee; overflow-x:scroll; display:table; } table, th, td { border-width: thin; border-collapse: collapse; border-style: solid;border-color:#eee; padding: 20px 10px; } td { text-align: left; } .hover-icons{ position: relative; right:25%; } .hover-icons ul{ position: absolute; border: 0; top: 50%; right: 0; transform: translate(5%, 33%); background: #EFEFEF; height: auto; display: none; } td{padding:0px 0} .hover-icons ul{ display: flex; } .hover-icons ul li{ margin: 0 6px; } .hover-main-icons .posi-r{position: relative;} .btn-hover-i{ } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"></link> <table class="hover-main-icons"> <tr> <td>test test<br> test<br> test<br>test<br> testtesttest testt<br>esttest<br>testt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> <tr> <td>test test test test<br>test<br> testtesttest testt<br>esttesttestt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> </table> 

You need to give a "height" and "display:block;" 您需要提供“高度”和“ display:block;” to the tr o td and than the "line-height" for the vertical align. 到tr o td,垂直对齐方式是“ line-height”。 For example If the height is "height:300;" 例如,如果高度为“ height:300;” the line-height must be "line-height:300px;" line-height必须为“ line-height:300px;”

Ps. PS。 I think position:relative is the right choise 我认为位置:相对是正确的选择

Try this: without position css 试试这个:没有位置的CSS

 table { width: 600px; font-size: 14px; color: black; font-family: sans-serif; background-color: white; margin-left: 0%; border-style: solid; border-color: #eee; overflow-x: scroll; display: table; } table, th, td { border-width: thin; border-collapse: collapse; border-style: solid; border-color: #eee; padding: 20px 10px; } td { text-align: left; } td { width: 100%; } .hover-icons ul { background: #EFEFEF; height: auto; display: none; } td { padding: 0px 0 } .hover-icons ul { display: flex; } .hover-icons ul li { margin: 0 6px; } .hover-main-icons .posi-r { position: relative; } .btn-hover-i {} 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"></link> <table class="hover-main-icons"> <tr> <td>test test<br> test<br> test<br>test<br> testtesttest testt<br>esttest<br>testt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> <tr> <td>test test test test<br>test<br> testtesttest testt<br>esttesttestt<br>est<br>test</td> <td class="hover-icons"> <div class="posi-r"> <ul> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> <li> <a href=""><button class="btn btn-red-bg btn-hover-i">Record Payment</button></a> </li> </ul> </div> </td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM