Adjusting table cell width

后端 未结 5 1231
臣服心动
臣服心动 2020-12-17 18:05

Let\'s take 4 table columns - ID, Text, Date, Action. In my case table have always constant width - in example 960px.

相关标签:
5条回答
  • 2020-12-17 18:38

    My best advice to you is to not touch the widths of the table, the table automatically layouts in a way that does all cells best.

    However, if you'd like to push through, I'd use width: 1px; on the cells that needs adjusting (one of each column is enough). Also use white-space: nowrap on all cells. that will make sure the lines don't break.

    0 讨论(0)
  • 2020-12-17 18:39

    The best way that I've found for setting table column widths is to use a table head (which can be empty) and apply relative widths for each table head cell. The widths of all cells in the table body will conform to the width of their column head. Example:

    HTML

    <table>
      <thead>
        <tr>
          <th width="5%"></th>
          <th width="70%"></th>
          <th width="15%"></th>
          <th width="10%"></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Some text...</td>
          <td>May 2018</td>
          <td>Edit</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Another text...</td>
          <td>April 2018</td>
          <td>Edit</td>
        </tr>
      </tbody>
    </table>
    

    CSS

    table {
      width: 600px;
      border-collapse: collapse;
    }
    
    td {
      border: 1px solid #999999;
    }
    

    View Result

    Alternatively, you can use colgroup as suggested here.

    0 讨论(0)
  • 2020-12-17 18:49

    basically, it's just like this: http://jsfiddle.net/49W5A/ - you have to set the cell-width to something small (like 1px) to make them stay as small as possible.

    but as you'll see, theres one problem with the date-fields doing a line-wrap. to prevent this, just add white-space: nowrap; for your text-field: http://jsfiddle.net/ZXu7U/

    working example:

    <style type="text/css">
    .table{
        width:500px;
        border: 1px solid #ccc;
    }
    .table td{
        border: 1px solid #ccc;
    }
    .id, .date, .action{
        width:1px;
    }
    .date{
        white-space: nowrap;
    }
    </style>
    <table class="table">
        <tr>
            <td class="id">1</td>
            <td class="text">Some Text...</td>
            <td class="date">May 2011</td>
            <td class="action">Edit</td>
        </tr>
        <tr>
            <td class="id">2</td>
            <td class="text">Another Text...</td>
            <td class="date">April 2011</td>
            <td class="action">Edit</td>
        </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-17 18:55

    Using a 100% width on the wide td and a fixed width for the table along with white-space:nowrap, this can be done:

    Demo

    HTML

    <table>
        <tr>
            <td>1</td>
            <td width="100%">Some text... </td>
            <td>May 2011</td>
            <td>Edit</td>
        </tr>
        <tr>
            <td>2</td>
            <td width="100%">Another text... </td>
            <td>April 2011</td>
            <td>Edit</td>
        </tr>
    </table>
    

    CSS

    table
    {
        ...
        width:960px;
    }
    
    td
    {
        ...
        white-space:nowrap;
    }
    
    0 讨论(0)
  • 2020-12-17 18:56

    Try this:

    .id, .date, .action is the table cells (td).

    CSS:

    .id, .date, .action {
       width: 1em;
    }
    

    It worked for me.

    The width:1em will not cut the text but force the width size to the minimum.

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