简体   繁体   English

HTML表格宽度与CSS

[英]Html Table width with css

I have data coming in and I parse it into a table on a webpage. 我收到了数据,并将其解析为网页上的表格。 There are 3 cells side by side to each other. 彼此并排有3个单元。 (Cell 0,0 : Cell 0,1 : Cell 0,2) (单元格0,0:单元格0,1:单元格0,2)

I'm trying to making it so that the cells are evenly split across the div making it so the first cell's text is aligned left, middle cell is aligned center and last cell is aligned right. 我正在尝试使其在div中均匀分布,从而使第一个单元格的文本左对齐,中间单元格的中心对齐,最后一个单元格的右对齐。

I've tried the following for css 我已经尝试了以下CSS

 td { width: calc(100%/3); } 
 <table border=1 style="border: 1px red dashed; width: 250px;"> <tr> <td>first</td> <td>second with wider contents</td> <td>third</td> </tr> </table> <table border=1 style="border: 1px blue dashed; width: 250px;"> <tr> <td>first</td> <td>second</td> <td>third with wider contents</td> </tr> </table> 

It doesn't give me the output I'm looking for. 它没有给我我想要的输出。 What I'm trying to accomplish is I have multiple tables on one page and want the cells to be aligned all the way down 我要完成的工作是在一页上有多个表,并希望单元格一直向下对齐

Are you setting your table to the full width of the div that it's nested in? 您是否将表格设置为嵌套表格的div的整个宽度?

For example: 例如:

div {
  width: 300px;
}

table {
  width: 100%;
  border: 1px solid black;
}


td {
  width: calc(100%/3);
  border: 1px solid black;
}

Just made this quick, I think this is what you're looking for: https://jsfiddle.net/nsxLfv00/ 刚刚讲完了,我想这就是您要寻找的: https : //jsfiddle.net/nsxLfv00/

If you want to set the alignment of the content in the three cells use the n-th child css selector 如果要在三个单元格中设置内容的对齐方式,请使用第n个子css选择器

td:nth-child(1){
   text-align: left;
}

and accordingly for the middle and the right column. 并相应地用于中间和右侧列。

While it's possible in pure CSS, I would advice you Bootstrap, Flexbox or any grid framework to achieve that easily. 虽然在纯CSS中是可行的,但我建议您使用Bootstrap,Flexbox或任何网格框架轻松实现这一目标。 In BS it would be something like : 在BS中,它将类似于:

<div class=container>
  <div class="row">
    <div class="col-sm-4">
      Your first cell content
    </div>
    <div class="col-sm-4">
      second
    </div>
    <div class="col-sm-4">
      third
    </div>    
  </div>
</div>

You may, of course, insert your table structure if you need to. 当然,您可以根据需要插入表结构。

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

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