简体   繁体   English

如何垂直对齐同一表单元格中的中间两个元素

[英]How to vertical align middle two elements in same table cell

I have a table with 5 columns, all of which are vertically-aligned: middle. 我有一个包含5列的表格,所有这些列都是垂直对齐的:中间。 The first 4 columns consist of just text that are vertically aligned in the middle of the cell. 前4列仅包含在单元格中间垂直对齐的文本。 These first four columns are nicely formatted; 前四列的格式很好; however, the last cell consists of two divs. 但是,最后一个单元格由两个div组成。 The first is some text that is floated left and the second is a button that is floated right. 第一个是向左浮动的文本,第二个是向右浮动的按钮。 Here is the html for the last cell: 这是最后一个单元格的html:

  <td> <div style="float:left; width:50%"> Text Not In Middle Of Cell </div> <div style="float:right; width:50%"> <button type="button">Button Text</button> </div> </td> 

Looking at the picture below, you can see that the text is not vertically aligned in the middle of the cell. 查看下面的图片,您可以看到文本在单元格的中间没有垂直对齐。 Is there a way to center the text in the center of the cell with which it lives? 有没有办法将文本居中放置在其单元格的中心? I have tried adding vertical-align: middle to the divs but that did not work. 我试过在divs中添加vertical-align:middle,但这没有用。 It makes sense since the div is the height of the text so vertically aligning the text won't move it. 这很有意义,因为div是文本的高度,因此垂直对齐文本不会移动文本。 Is there some other way to achieve this? 还有其他方法可以做到这一点吗? Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!

在此处输入图片说明

You can use a flexbox. 您可以使用flexbox。

 .container { display: flex; align-items: center; } .container button { margin-left: .5em; } 
 <td> <div class="container"> <div> Text Not In Middle Of Cell </div> <div> <button type="button">Button Text</button> </div> </div> </td> 

Yes you can using :before , but you have to set height for your div 是的,您可以使用:before,但是必须为div设置高度

here is a sample code 这是示例代码

  .text_div{ float:left; width:50%; height: 100px; background-color:#f00; } .text_div:before { content: ' '; display: inline-block; vertical-align: middle; height: 100%; } .text_div{ vertical-align: middle; display: inline-block; } 
 <td> <div class="text_div"> <a>Text Not In Middle Of Cell</a> </div> <div style="float:right; width:50%"> <button type="button">Button Text</button> </div> </td> 

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

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