html - 在表格内水平居中文本

标签 html css

我无法在 colspan 大于 1 的 TD 中使文本居中,我已将文本对齐居中添加到每个标签,但什么也没有。

HTML:(我删除了除其中一个不会居中的元素之外的所有元素,我保留了与此文本相关的所有标签)

div.OBJ {
  display: table-cell;
  text-align: center;
}
div.scroll {
  width: 100%;
  height: 600px;
  overflow-y: scroll;
  overflow-x: auto;
  text-align: center;
}
td {
  text-align: center;
}
table {
  border-collapse: collapse;
  table-layout: fixed;
  align: center;
  text-align: center;
}
a:link {
  color: #000000;
  text-decoration: none;
  text-align: center;
}
a:visited {
  color: #000000;
  text-decoration: none;
  text-align: center;
}
a:hover {
  color: #ff0000;
  text-decoration: underline;
  text-align: center;
}
div {
  text-align: center;
}
span {
  text-align: center;
}
<div class="scroll">
  <table border="1" style="width:100%">
    <tr id="{{ParentObject.id}}">
      <td id="{{ParentObject.id}}" colspan="{{object.size}}" style="background-color:{{object.bgColor}}" data-begin="{{object.begin}}" data-end="{{object.end}}">
        {% multiply object.size colWidth as colSize %}
        <div id="{{object.id}}" class="OBJ" style="width:{{colSize}}%">
          <a id="{{object.id}}" href="link" title="hover text">
            <span id="{{object.id}}" class="object">
                            {{object.name}}
                        </span>
          </a>
        </div>
      </td>
    </tr>
  </table>
</div>

最佳答案

你的问题是:

div.OBJ {
        display: table-cell;
        text-align: center;
    }

display: table-cell让元素表现得像 <td>并且很可能继承了不同的东西。

关于html - 在表格内水平居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31327578/

相关文章:

css - DDSmoothMenu 糟糕的 CSS 布局

html - 如何在大分辨率屏幕上对齐水平菜单链接?

jquery - Susy 响应式网格和 jQuery Masonry

xslt - 如何从一个 xhtml 文档中提取一个 div 部分到另一个 xhtml 文档中

javascript - 如何转义存储颜色名称的 Less 变量中的引号?

html - 如何用管道分离链接项

javascript - IE : "Function expected"start() '&lt;input onclick=" 中的错误 ;"... '

javascript - WindowProxy 和 Window 对象?

javascript - 音频播放器中的 Bootstrap 图标替换

javascript - JQuery 在 Firefox 中不工作,但在 Chrome 中工作