如何使两个表td元素成为中心,无论它们的内容多长或多大,它们彼此相邻

时间:2016-11-22 09:36:47

标签: html css html5

我有一个练习让两个td元素彼此相邻,无论它们的文本/内容有多长?有任何想法吗?

here you see that it is not int the middle but slightly to the left

<table style="
border:1px solid #2ba6cb;
width:380px;
border-radius:4px;
background-color:transparent;
max-width:768px;
border-collapse:separate";

>
    <tr><td style="border: 1px solid #000000;
            text-align:right;
            vertical-align:middle;
            color:#FFFFFF;
            font-size:15px;
            text-shadow:0 -1px rgba(0, 0, 0, 0.3);
            text-decoration:none;
            max-width:768px;
            font-family:Arial, Helvetica, sans-serif;
            padding:2px;
            float: right;
            ">
            Test 1:
        </td>
        <td style="
        border:1px solid #000000;
        text-align:left;
        vertical-align:center;
        color:#ef5566;
        font-size:15px;
        text-shadow:0 -1px rgba(0, 0, 0, 0.3);
        font-weight:bold;
        text-decoration:none;
        max-width:768px;
        font-family:Arial, Helvetica, sans-serif;
        padding:2px;
        float: left;
        ">
            Test2ssssss
        </td>
    </tr>

4 个答案:

答案 0 :(得分:0)

CSS添加到您的表格td:

td {
   text-align: center;
}

答案 1 :(得分:0)

  

使用css 表格td {text-align:center;}

查看代码段

&#13;
&#13;
table{
  border-collapse:separate;
  width:400px;
  }
  
table td {
  border:solid 1px #777;
  text-align:center;
 }
&#13;
   

 <table>
    <tr>
    <td>Test1</td>
    <td>Test2</td>
    </tr>
    </table>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是你需要的吗?

table {
  border:1px solid #2ba6cb;
  width:380px;
  border-radius:4px;
  background-color:transparent;
  max-width:768px;
  border-collapse:separate";
}
td {
  border: 1px solid #000000;
  text-align:center;
  vertical-align:middle;
  color:red;
  font-size:15px;
  text-shadow:0 -1px rgba(0, 0, 0, 0.3);
  text-decoration:none;
  width:50%;
  font-family:Arial, Helvetica, sans-serif;
  padding:2px;
}
td:nth-child(2){
  color:#ef5566;
}
<table>
  <tr>
    <td>Test 1:</td>
    <td>Test2ssssss</td>
  </tr>
</table>

答案 3 :(得分:0)

Use width:50%;

检查https://jsfiddle.net/cpdstzx9/小提琴链接,我已更新您的代码