Html 如何将 td 文本与表格左侧对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17874504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:32:09  来源:igfitidea点击:

How to align a td text to the left of the table

htmljsphtml-table

提问by Vijay

I have created a table in JSP file to look like this:

我在 JSP 文件中创建了一个表,如下所示:

JSP Code:

JSP代码:

<table style="width: 832px;" >
    <tr>
        <td style="width: 45px;text-align: left;" align="left"><%=episodeDate %></td>
        <td style="width: 7px;">&nbsp;</td>
        <td style="width: 257px;text-align: left;" ><%=careprovidername %></td>
        <td style="width: 7px;">&nbsp;</td>
        <td style="width: 260px;text-align: left;" ><%=docatorname %></td>
        <td style="width: 6px;">&nbsp;</td>
        <td style="width: 250px;text-align: left;"><%=purposeType %></td>
    </tr>
</table>

But what I get is display in the below image.

但我得到的是显示在下图中。

Table

桌子

My problem is that the 5th and 7th td data are not shown well aligned. What is wrong with this code?

我的问题是第 5 个和第 7 个 td 数据没有很好地对齐。这段代码有什么问题?

采纳答案by Vijay

Thanks to Gintas K's comment, I solved this problem by adding table-layout: fixed;to the styles for my <table>.

感谢Gintas K 的评论,我通过添加table-layout: fixed;到我的<table>.

回答by AngelWarrior

In your JSP, you should be looping inside one table. By your code, it would seem that you are re-setting the values of your variables outside the visible code. If so, you are generating multiple 1 row tables. The fix then would be to set your java variables inside the < table > ... < / table > tags, creating just new TRs and TDs for each java object.

在您的 JSP 中,您应该在一张表内循环。根据您的代码,您似乎是在可见代码之外重新设置变量的值。如果是这样,您将生成多个 1 行表。解决方法是在 < table > ... </ table > 标签内设置你的 java 变量,为每个 java 对象只创建新的 TR 和 TD。

With a single table, your TDs should all align as you wished.

使用单个表,您的 TD 应该都按照您的意愿排列。

<table style="width: 832px;" >
<%
MyObject o; // which has those variables
for (int i=0; i<array_size; ++i){
    o = array[i]; %>
    <tr>
        <td style="width: 45px;text-align: left;" align="left"><%= o.getEpisodeDate() %></td>
        <td style="width: 7px;">&nbsp;</td>
        <td style="width: 257px;text-align: left;" ><%= o.getCareproviderName() %></td>
        <td style="width: 7px;">&nbsp;</td>
        <td style="width: 260px;text-align: left;" ><%= o.getDocatorName() %></td>
        <td style="width: 6px;">&nbsp;</td>
        <td style="width: 250px;text-align: left;"><%= o.getPurposeType() %></td>
    </tr>
<% } %>
</table>

The idea here is to have:

这里的想法是:

<table>
    <tr> <td></td>*7 </tr>
    <tr> <td></td>*7 </tr>
    <tr> <td></td>*7 </tr>
    <tr> <td></td>*7 </tr>
    <tr> <td></td>*7 </tr>
    <tr> <td></td>*7 </tr>
</table>

So the loop where you set the java variable values has to be inside the < table> ... < /table> tags, not before.

因此,您设置 java 变量值的循环必须在 < table> ... </table> 标签内,而不是之前。