0

我有 html 代码,可以打印 html 表中的所有帖子。

但是,如果没有帖子,那么我需要打印一个错误No posts found

现在关注错误消息

我正在尝试在表格中央打印错误消息。

<style>
table{border-collapse:collapse}
td{background-color:white;padding:10px}
.first_tr td{background-color:#f1f7f7 !important;border-bottom:1px red solid}
</style>
<table border="0" width="400px">
<tr class="first_tr"><td width="20%">POST ID</td><td width="79%">POST TITLE</td></tr>
<tr><td>No posts found</td></tr>
</table>

这是一个例子:http: //jsfiddle.net/9UhTd/

错误消息显示在表格的开头,但我试图将其居中!

4

3 回答 3

3

首先,您在第二行唯一的单元格上缺少 colspan 属性

<table border="0" width="400px">
<tr class="first_tr"><td width="20%">POST ID</td><td width="79%">POST TITLE</td></tr>
<tr><td colspan="2">No user found</td></tr>
</table>​​​​​

没有它,该单元格将仅占用其上方单元格的空间。解决了这个问题,您可以继续以您喜欢的方式将其居中

<table border="0" width="400px">
<tr class="first_tr"><td width="20%">POST ID</td><td width="79%">POST TITLE</td></tr>
<tr><td colspan="2" style="text-align:center">No user found</td></tr>
</table>​​​​​

或者

<style type="text/css">.center { text-align: center; }</style>
<table border="0" width="400px">
<tr class="first_tr"><td width="20%">POST ID</td><td width="79%">POST TITLE</td></tr>
<tr><td colspan="2" class="center">No user found</td></tr>
</table>​​​​​

你的jsfiddle实现了这个

于 2012-09-26T21:14:33.377 回答
1

你错过了colspan

下面的代码应该做

<style>
table{border-collapse:collapse}
td{background-color:white;padding:10px}
.first_tr td{background-color:#f1f7f7 !important;border-bottom:1px red solid}
</style>
<table border="0" width="400px">
<tr class="first_tr"><td width="20%">POST ID</td><td width="79%">POST TITLE</td></tr>
    <tr><td colspan="2">No user found</td></tr>
</table>​
于 2012-09-26T21:14:39.227 回答
1

很少有评论:1)如果你这样做,你的元素跨越 2 列,因为你的表有 2 列使用: colspan="2"

2)您可以向元素添加一个类,例如 class="error" 并将这种样式添加到您的css:

.error{
    text-align:center;
}
于 2012-09-26T21:17:36.510 回答