html - 当有一些其他内容 float 时,如何让 html 表格单元格中的内容居中?

标签 html css css-float

我有居中的内容和向右浮动的内容,我刚刚意识到由于 float:right 内容,居中的文本移到了左边。

中心文本使用此 css:

 .tableCell {
      text-align: center;
 }

 .floatRight img
 {
    float:right;
    cursor: pointer;
 }

这是 HTML:

   <td class="tableCell" id="697">
      <span class="floatRight">
           <img src="/arrow.png">
      </span>
      <b>1006</b><hr>Some Text<br>Some other text
   </td>

无论如何,表格单元格中的内容是否居中且水平位置不受 float:right 文本的影响?

最佳答案

一个简单的方法是在箭头图标/图像上使用绝对定位 float ,如下所示。

箭头的垂直位置可能需要一些调整,取决于其余部分 的设计看起来像。

table {
  border: 1px dotted blue;
}
.tableCell {
  text-align: center;
  position: relative;
}

.tableCell:hover > .floatRight 
{
    opacity:1;
}

.floatRight {
  position: absolute;
  top: 0;
  right: 0;
  opacity:0;
  transition: opacity ease-in-out 1s;
}
<table>
  <tr>
    <td class="tableCell">
      <span class="floatRight">
           <img src="http://placehold.it/20x20">
      </span>
      <b>1006</b>
      <hr>Some Text
      <br>Some other text
    </td>
  </tr>
</table>

关于html - 当有一些其他内容 float 时,如何让 html 表格单元格中的内容居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26310239/

相关文章:

javascript - 如果另一个元素具有此元素,如何将 css 标签放入该元素中

javascript - 如何确定哪个 DOM 元素与 Knockout 可观察数组元素相对应?

CSS 中心 float

html - 某些 float 元素未适当调整大小 (CSS/XHTML)

javascript - contenteditable div 中突出显示的文本 : Get the start and the end of the highlighted text

html - 导航栏下的空白/间隙仅在 iPad View 上?

jquery - 如何使用 jScrollPane 使自定义滚动条始终可见?

css - 对齐元素列表

jquery - 哪个 HTML5/CSS3/JS-or-jQuery 框架用于这个任务?

jquery - CSS: z-index 同时使用 float: left in image gallery