How to get table cells evenly spaced?

前端 未结 8 1921
长发绾君心
长发绾君心 2020-12-09 15:01

I\'m trying to create a page with a number of static html tables on them.

What do I need to do to get them to display each column the same size as each other column

相关标签:
8条回答
  • 2020-12-09 15:14

    In your CSS file:

    .TableHeader { width: 100px; }
    

    This will set all of the td tags below each header to 100px. You can also add a width definition (in the markup) to each individual th tag, but the above solution would be easier.

    0 讨论(0)
  • 2020-12-09 15:16

    I was designing a html email and had a similar problem. But having every cell with the fixed width is not what I want. I'd like to have the equal spacing between the contents of the columns, like the following

    |---something---|---a very long thing---|---short---|

    After a lot of trial and error, I came up with the following

    <style>
        .content {padding: 0 20px;}
    </style>
    
    table width="400"
        tr
            td
                a.content something
            td 
                a.content a very long thing
            td 
                a.content short
    

    Issues of concern:

    1. Outlook 2007/2010/2013 don't support padding. Having the width of the table set will allow the widths of the columns to automatically set. This way, though the contents will not have equal spacing. They at least have some spacing between them.

    2. Automatic width setting for table columns will not give equal spacing between the contents The padding added for the contents will force the equal spacing.

    0 讨论(0)
  • 2020-12-09 15:17

    You can use CSS. One way is to set table-layout to fixed, which stops the table and it's children from sizing according to their content. You can then set a fixed width on the relevant td elements. This should do the trick:

    table.PerformanceTable {
        table-layout: fixed;
        width: 500px;
    }
        table.PerformanceTable td.PerformanceCell {
            width: 75px;
        }
    

    Suggestions for for tidying up? You don't need the cellpadding or cellspacing attributes, or the TableRow and TableHeader classes. You can cover those off in CSS:

    table {
        /* cellspacing */
        border-collapse: collapse;
        border-spacing: 0;
    }
    th {
        /* This covers the th elements */
    }
    tr {
        /* This covers the tr elements */
    }
    th, td {
        /* cellpadding */
        padding: 0;
    }
    

    You should use a heading (e.g. <h2>) instead of <span class="Emphasis"> and a <p> or a table <caption> instead of the Source <span>. You wouldn't need the <br> elements either, because you'd be using proper block level elements.

    0 讨论(0)
  • 2020-12-09 15:17

    Take the width of the table and divide it by the number of cell ().

    PerformanceTable {width:500px;}    
    PerformanceTable.td {width:100px;}
    

    If the table dynamically widens or shrinks you could dynamically increase the cell size with a little javascript.

    0 讨论(0)
  • 2020-12-09 15:23

    If you want all your columns a fixed size, you could use CSS:

    td.PerformanceCell
    {
        width: 100px;
    }
    

    Or better, use th.TableHeader (I didn't notice that the first time around).

    0 讨论(0)
  • 2020-12-09 15:23

    I'm also open to suggestion on how to tidy this up, if there are any? :-)

    Well, you could not use the span element, for semantic reasons. And you don't have to define the class PerformanceCell. The cells and rows can be accessed by using PerformanceTable tr {} and PerformanceTable tr {}, respectively.

    For the spacing part, I have got the same problem several times. I shamefully admit I avoided the problem, so I am very curious to any answers too.

    0 讨论(0)
提交回复
热议问题