html - 将文本中心置于其上方的边框空间

标签 html css css-position css-tables

在下面的代码中,如何将文本中心放置到其正上方的边框空间,如下面的屏幕截图所示,“一些文本 1”和“一些文本 2”位于它们上方边框空间的中心。

enter image description here

.Row {
  display: table;
  width: 100%;
  table-layout: fixed;
  border-spacing: 10px;
}

.Column {
  display: table-cell;
  background-color: red;
}

.Column:nth-child(1) {
  width:20%;
}
.Column:nth-child(2) {
  width:50%;
}
.Column:nth-child(3) {
  width:30%;
}
<div class="Row">
  <div class="Column">C1</div>
  <div class="Column">C2</div>
  <div class="Column">C3</div>
</div>

最佳答案

您可以通过将文本元素放置在单元格中,将它们设置为 position: absolute; 并使用 transform: translate 将它们自身宽度的 50% 推出单元格来实现这一点(50%, 0);.

当然,您需要适当的供应商前缀来支持旧版浏览器。

.Row {
  display: table;
  width: 100%;
  table-layout: fixed;
  border-spacing: 10px;
}

.Column {
  display: table-cell;
  position: relative;
  background-color: red;
}

.Column:nth-child(1) {
  width:20%;
}
.Column:nth-child(2) {
  width:50%;
}
.Column:nth-child(3) {
  width:30%;
}

.Column > span {
  position: absolute;
  right: 0;
  top: 1.5em;
  transform: translate(50%, 0);
  text-align: center;
}
<div class="Row">
  <div class="Column">C1<span>Some Text 1</span></div>
  <div class="Column">C2<span>Some Text 2</span></div>
  <div class="Column">C3</div>
</div>

关于html - 将文本中心置于其上方的边框空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132702/

相关文章:

javascript - 如何将图像分割成十六进制像素?

javascript - 结合 Javascript + HTML 时删除自动添加的换行符

html - Internet Explorer(6 及更高版本)缓存和临时文件

html - Firefox 在 <td> 标签之间添加水平间距

css - 动画不适用于 Safari 和 IE 11 中的页面重定向

jquery - CSS 位置 :fixed not fixed in Mobile Safari. .. 再次

html - 帮助解决文档类型问题

javascript - 如何将字体系列强制级联到 <td> 标签内的按钮元素

css - 在 Firefox 中,单击可聚焦元素内的绝对元素不会聚焦可聚焦元素,除非它具有 CSS 位置

HTML/CSS : nested list needs to be displayed elsewhere/using mother div instead of mother element for position orientation