html - 在 div 内水平对齐 SPAN(动态长度,无文本对齐)?

标签 html css

我有这段代码:

table
{
  border:solid 2px black;
  width:100%; /*must*/
}
td
{
  border:solid 2px red;
  padding:5px;
}

span
{
   border:solid 2px green;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<table>
    <tr>
       <td>
	    <span  >There's already existing policy for : fugfuj tyutyuty</span>
      </td>
  </tr>
</body>
</html>

enter image description here

我想将 span 对齐到 TD 的中心。

我可以做 text-align:centerTD 即将托管 future -injected-ajax -html 所以我不想碰它(和开始使用 remove/apply text-align:center )。

我也可以做 width:200px;display:block; 然后:margin:0 auto; 但我不知道它的长度。

所以在我开始 JS 解决方案之前:

问题

是否有任何仅使用 CSS 的解决方案来使跨度居中?

相关jsbin:http://jsbin.com/qokizunibifa/2/edit

最佳答案

display: table;margin: 0 auto; 添加到 span 中,使 span 水平居中div

table {
    border:solid 2px black;
    width:100%; /*must*/
}
td {
    border:solid 2px red;
    padding:5px;
}
span {
    border:solid 2px green;
    display: table;
    margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <table>
    <tr>
      <td>
        <span>There's already existing policy for : fugfuj tyutyuty</span>
      </td>
    </tr>
  </table>
</body>
</html>

关于html - 在 div 内水平对齐 SPAN(动态长度,无文本对齐)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084604/

相关文章:

html - 悬停时改变 ul li ul li 的颜色

jquery - 如何使固定元素与文档底部保持特定距离?

php - 将 MySQL 计数列添加到 PHP/HTML 表

php - 输出原始图像流而不是 jpeg,动态调整图像大小

CSS:在保持宽高比的同时使 Canvas 尽可能大

javascript - 如何通过单击移动图层

javascript - 尝试在 Chrome 中运行时来自 Sigma.js 的 XMLHTTPRequest 错误 [似乎是它运行方式的独特之处]

html - 无法使用 Chrome 在我的 ASP.NET ZERO MVC 应用程序中使用 border-radius

javascript - 在动态内容上按下后退按钮时遇到困难

html - 无法在容器内垂直居中文本