Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a table column that never wraps and is auto width?

Tags:

I need to create a two-column full-width table where:

  • Column 1 wraps when it needs to
  • Column 2 never wraps and fills any empty space

See a visual example here:

enter image description here

I'm having a lot of trouble figuring this out... here's where I'm up to:

table {
  width: 100%;
}

.col-2 {
  white-space: nowrap;
  width: 60%;
  text-align: right;
}

/* Below is arbitrary example CSS, not part of solution */
.container {
  width: 250px;
}
td {
  background: lightblue;
}
<h4>Example A</h4>
<div class="container">
  <table>
    <tr>
      <td class='col-1'>Not much text</td>
      <td class='col-2'>Column 2</td>
    </tr>
  </table>

  <h4>Example B</h2>
    <table>
      <tr>
        <td class='col-1'>This has a lot of text that wraps</td>
        <td class='col-2'>Column 2</td>
      </tr>
    </table>
</div>

Notice that col-1 isn't expanding because of the width: 60% on col-2. If I remove that, then col-1 defaults to taking up all the space, which breaks Example B.

I can get this working with flexbox, but then the columns don't line up properly when col-1 wraps... so I think it needs to remain a table.

Any ideas would be massively appreciated. Thank you!!

like image 956
Jon Lemmon Avatar asked Feb 13 '21 04:02

Jon Lemmon


People also ask

How do you stop a table from expanding?

To prevent cells (rows) from expanding vertically, you have to set a fixed height for table rows. Select the relevant rows and, on the Table Tools Layout tab, click Properties. On the Row tab, select "Specify height" and then choose "Exactly" for "Row height is." Specify the desired amount. Was this reply helpful?

How do you fix column width in a table?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.

How do you create column width in a table?

HTML tables can have different sizes for each column, row or the entire table. Use the style attribute with the width or height properties to specify the size of a table, row or column.


2 Answers

CSS grid should do it:

div {
  display: grid;
  width: 250px;
  grid-gap: 2px;
  grid-template-columns: auto 1fr;
}

.col-2 {
  white-space: nowrap;
  text-align: right;
}

p {
  background: hotpink;
  margin: 0;
}
<h4>Example A</h4>

<div>
  <p class='col-1'>Not much text</p>
  <p class='col-2'>Column 2</p>
  <p class='col-1'>row2</p>
  <p class='col-2'>Column 2</p>
</div>

<h4>Example B</h4>
<div>
  <p class='col-1'>This has a lot of text that wraps</p>
  <p class='col-2'>Column 2</p>
  <p class='col-1'>row2</p>
  <p class='col-2'>Column 2</p>
</div>
like image 123
Temani Afif Avatar answered Oct 01 '22 03:10

Temani Afif


You can add "white-space: nowrap" to td because of this it will never wraps and it will take is auto width.

table {
  width: 100%;
}

.col-2 {
  white-space: nowrap;
  width: 60%;
  text-align: right;
}

/* Below is arbitrary example CSS, not part of solution */
.container {
  width: 250px;
}
td {
  background: lightblue;white-space: nowrap;
}
<h4>Example A</h4>
<div class="container">
  <table>
    <tr>
      <td class='col-1'>Not much text</td>
      <td class='col-2'>Column 2</td>
    </tr>
  </table>

  <h4>Example B</h2>
    <table>
      <tr>
        <td class='col-1'>This has a lot of text that wraps</td>
        <td class='col-2'>Column 2</td>
      </tr>
    </table>
</div>
like image 23
DVS Avatar answered Oct 01 '22 03:10

DVS