简体   繁体   English

HTML表间距

[英]HTML Table Spacing

I am attempting to create a uniform series of simple HTML tables on a website that seem determined to decide their own column spacing despite having the exact same syntax as the others. 我正在尝试在网站上创建一系列统一的简单HTML表,这些表似乎决心决定自己的列间距,尽管它们的语法与其他语法完全相同。 Some of them are well-spaced, and others are comically close or far apart. 它们中的一些间隔良好,而另一些在距离上却相距甚远。 How can I fix the column widths to be the same so they appear uniform? 如何将列宽固定为相同,以使它们看起来均匀?

This is an example of one of the 8 tables: 这是8个表之一的示例:

<table class=”table4”>
  <tbody>
    <tr>
      <td><b>Qty</b></td>
      <td><b>Wire EDM</b></td>
    </tr>
    <tr>
      <td>1</td>
      <td>Mitsubishi FX 10</td>
    </tr>
  <tbody>
</table>

I have tried 'cellspacing' to no avail. 我尝试过“ cellspacing”无济于事。 I've also deleted and re-written the code multiple times, but they come out without change. 我也多次删除并重新编写了代码,但它们没有更改就可以了。

I'm brand new to HTML, and this is also not my work, I'm trying to rework someone else's code. 我是HTML的新手,这也不是我的工作,我正在尝试重新编写别人的代码。 Clear and complete answers would be greatly appreciated! 清晰完整的答案将不胜感激!

Thank you in advance. 先感谢您。

Use CSS to determine your column spacing, as well as changing anything related to the "style" or "interface" of your site. 使用CSS确定列间距,并更改与网站的“样式”或“界面”相关的任何内容。

If you want all your columns on the whole site to be uniform, a simple CSS rule like the following should do it for you: 如果您希望整个站点上的所有列都是统一的,则应使用如下所示的简单CSS规则:

td {
    width: 120px;
}

If you want percentages, you can do that as well. 如果需要百分比,也可以这样做。 For example, if your tables always have 4 columns, you could do something like the following: 例如,如果表始终有4列,则可以执行以下操作:

td {
    width: 25%;
}

Check out the example below: 查看以下示例:

 table { border: 1px solid silver; width: 500px; } td { border: 1px solid silver; width: 25%; } 
 <table> <tr> <td>test 1</td> <td>test 1</td> <td>test 1</td> <td>test 1</td> </tr> <tr> <td>test 1</td> <td>test 1</td> <td>test 1</td> <td>test 1</td> </tr> <tr> <td>test 1</td> <td>test 1</td> <td>test 1</td> <td>test 1</td> </tr> <tr> <td>test 1</td> <td>test 1</td> <td>test 1</td> <td>test 1</td> </tr> </table> 

To add CSS to your site, there are many resources online to get you started: https://developer.mozilla.org/en-US/Learn/CSS/Using_CSS_in_a_web_page 要将CSS添加到您的网站,在线上有很多资源可以帮助您入门: https//developer.mozilla.org/zh-CN/Learn/CSS/Using_CSS_in_a_web_page

A table will determine its own column widths based on its content, so if you want different tables to have the same proportions you have to tell the browser. 表格将根据其内容确定其自己的列宽,因此,如果您希望不同的表格具有相同的比例,则必须告诉浏览器。

For example: 例如:

 table { border-collapse: collapse; border: 1px solid #ccc; margin:4px; } th, td {border: 1px solid #ccc;} table.uniform { width:300px; } table.uniform th:first-child { width:30%; } 
 <p>Some uneven tables:</p> <table class="table1"> <thead> <tr> <th>eg</th> <th>A Cell Heading</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Some content</td> </tr> </tbody> </table> <table class="table2"> <thead> <tr> <th><b>Qty</b></th> <th><b>Wire EDM</b></th> </tr> </thead> <tbody> <tr> <td>123456789</td> <td>Mitsubishi FX 10</td> </tr> </tbody> </table> <p>The same tables but with some widths defined:</p> <table class="uniform"> <thead> <tr> <th>eg</th> <th>A Cell Heading</th> </tr> </thead> <tbody> <tr> <td>123456789</td> <td>Some content</td> </tr> </tbody> </table> <table class="uniform"> <thead> <tr> <th><b>Qty</b></th> <th><b>Wire EDM</b></th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Mitsubishi FX 10</td> </tr> </tbody> </table> <p>Example with inline styling:</p> <table style="width:300px"> <thead> <tr> <th style="width:30%"><b>Qty</b></th> <th><b>Wire EDM</b></th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Mitsubishi FX 10</td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM