Increase width for fixed height HTML table data cell?
Asked Answered
O

3

5

A similar question like Fixed Height and Changing Width for Header (HTML Table) - except I'd like to ask: is there a different way to achieve this, other than using   instead of space? Basically, I'd like increasing text content in the table data cell to keep the cell height fixed, and instead increase the cell width..

Below is a minimal HTML example, which behaves like this upon changing the browser (Firefox 43) width:

animate-browser.gif

As you can see, regardless of height/max-height specification in CSS, the table td fields increase their height, while decreasing the width.

What I'd like to happen is in this case, the specified height - and the corresponding width - of td cells remains the same upon change of browser width, and what changes instead is the bottom scrollbar.

Is there any way I could achieve this with CSS, or even JS?


In response to @tgallimore's questions:

  • Are you able to give a fixed width to the table? - no, I'd like it to resize width depending on content
  • Do you know how wide you would like each cell to remain? - no, I'd like it to have a fixed width, then if it's enough for two rows of text, these should be adjusted for optimal width (i.e. each row has approximately the same amount of text)
  • Can this width be given to each cell? - no, cells would have differing text contents, as in the example

In response to @Leothelion's post: I wanted to specify a fixed height of 2em (or let's say, 2.5em), is because I'd expect it to allow enough vertical space for max two lines of text. So what I want to achieve is: * If the text in the cell is short (i.e. one word), then there's no line breaking, text is in single line, and cell height is 2.5em * If the text in the cell is long (a whole sentence), then I'd want the layout to figure out that the in a cell height of 2.5em it can fit max two lines of text; thereafter it would try to break the text such that there are approximately the same amount of characters in both lines (so now we have a "paragraph"; and finally it would set the width of the cell to the width of this newly line-broken "paragraph".

In other words, I would like this layout:

ffox-tab-layout.png

... regardless of how I scale the browser width; if the browser width is too small, then only the horizontal scrollbar adjusts.


The sample HTML code:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" src="../jquery-1.12.3.min.js"></script>
  <style type="text/css">
.mytbl,
.mytbl tr th,
.mytbl tr td {
  border-style: solid;
  border-color: #000;
  border-spacing: 0;
  border-collapse: collapse;
  padding: 4px;
  border-width: 1px;
  font: 12px helvetica,arial,sans-serif;
}
.mytbl tr td {
  height: 2em;
  max-height: 2em;
}
.mtytblwrap {
  border-width: 1px;
  border-color: #000;
  padding: 4px;
  overflow: auto;
  overflow-y: hidden;
}
  </style>
  <script type="text/javascript">
ondocready = function() {
  // placeholder - nothing for now...
}
$(document).ready(ondocready);
  </script>
</head>

<body>
  <h1>Hello World!</h1>

  <div id="wrapper1" class="mtytblwrap">
    <table id="table1" class="mytbl">
      <thead>
        <tr>
         <th> Dendrologist </th>
         <th> Iciness </th>
         <th> JoyfulDistortion </th>
         <th> Suburbicarian </th>
         <th> Ecballium </th>
         <th> AbulicNonviolence </th>
         <th> GrowlerTheocracy </th>
         <th> Necessitattion </th>
        </tr>
      </thead>
      <tbody>
        <tr>
         <td> A1 </td>
         <td> Just testing some longer content here, so long that it might not fit on a single line </td>
         <td> Molybdenum </td>
         <td> D1 </td>
         <td> Scanty Distensibility </td>
         <td> Arithmetical </td>
         <td> G1 </td>
         <td> Hypallelomorph </td>
        </tr>
      </tbody>
    </table>
  </div>

</body>
</html>
Orbadiah answered 21/4, 2016 at 9:27 Comment(0)
T
4

Are you able to give a fixed width to the table? Do you know how wide you would like each cell to remain? Can this width be given to each cell?

A table-cell will ALWAYS expand its height if it's content doesn't fit, regardless of wether you set a height or not (so a height in this case would work as a min-height).

Also, you will probably need to use .mytbl { table-layout: fixed; }. This tells the table to use the widths that you have defined, rather than try to fix it's content in each cell. See this for more info: https://css-tricks.com/fixing-tables-long-strings/

Thapsus answered 21/4, 2016 at 9:37 Comment(1)
Thanks for that, @tgallimore - I have updated the OP with answers to our questions; I will definitely take a look at table-layout: fixed; and the link you sent. Cheers!Orbadiah
F
3

What you need is Media query

See my UPDATED FIDDLE

On different resolution(i just took 1, adjust according to your need ), fixed the width and table-layout: fixed; and you will get your solution.

Farceuse answered 21/4, 2016 at 9:39 Comment(4)
Many thanks for that @Leothelion - but as far as I can see, your fiddle does not fix the cell height to say 2em, what it does instead is fix the table width, and then sets each cell to have an equal share of the width -- which is an interesting solution, but not exactly what I'm looking for... Cheers!Orbadiah
oohh..sorry if its not give you perfect answer :(Farceuse
No problem, @Leothelion - it's still good to have a reference to the solution you posted; cheers!Orbadiah
Thanks again, @Leothelion - the reason why I wanted to specify a height of 2em, is that I'd expect there is enough vertical space for the text to be broken into two lines (and two lines maximum); what happens on your fiddle is that you've used nowrap, so even if the td cell has the right height, the text is only one line. I updated my OP with a clarification.Orbadiah
X
0

I have a similar requirement. After failing to find pure HTML/CSS solution – I leveraged server-side control over my code and thought it might be useful to share the concept.

When HTML is formed, server code knows the length of the string that will be put to a given table cell, right? So we can use that string length to set min-width on the cell. It requires some manual fine-tuning, because different fonts are going to require different divisors.

Hope it helps. See WordPress-based example for illustration:

<?php
$table_data = [
    'A1',
    'Just testing some longer content here, so long that it might not fit on a single line',
    'Molybdenum',
    'D1',
    'Scanty Distensibility',
    'Arithmetical',
    'G1',
    'Hypallelomorph',
];

foreach ( $table_data as $data ) {
    $divisor   = 4; // Depends on the font styles applied to the table
    $length    = mb_strlen( $data ); // Cell content length
    $min_width = round( $length / $divisor );
    $min_width = $min_width . 'em'; // Make min-width relative to font-size
    echo "<td style='min-width: $min_width'>$data</td>" . PHP_EOL;
}

So your test HTML becomes

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" src="../jquery-1.12.3.min.js"></script>
  <style type="text/css">
.mytbl,
.mytbl tr th,
.mytbl tr td {
  border-style: solid;
  border-color: #000;
  border-spacing: 0;
  border-collapse: collapse;
  padding: 4px;
  border-width: 1px;
  font: 12px helvetica,arial,sans-serif;
}
.mytbl tr td {
  height: 2em;
  max-height: 2em;
}
.mtytblwrap {
  border-width: 1px;
  border-color: #000;
  padding: 4px;
  overflow: auto;
  overflow-y: hidden;
}
  </style>
  <script type="text/javascript">
ondocready = function() {
  // placeholder - nothing for now...
}
$(document).ready(ondocready);
  </script>
</head>

<body>
  <h1>Hello World!</h1>

  <div id="wrapper1" class="mtytblwrap">
    <table id="table1" class="mytbl">
      <thead>
        <tr>
         <th> Dendrologist </th>
         <th> Iciness </th>
         <th> JoyfulDistortion </th>
         <th> Suburbicarian </th>
         <th> Ecballium </th>
         <th> AbulicNonviolence </th>
         <th> GrowlerTheocracy </th>
         <th> Necessitattion </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td style='min-width: 1em'>A1</td>
          <td style='min-width: 21em'>Just testing some longer content here, so long that it might not fit on a single line</td>
          <td style='min-width: 3em'>Molybdenum</td>
          <td style='min-width: 1em'>D1</td>
          <td style='min-width: 5em'>Scanty Distensibility</td>
          <td style='min-width: 3em'>Arithmetical</td>
          <td style='min-width: 1em'>G1</td>
          <td style='min-width: 4em'>Hypallelomorph</td>
        </tr>
      </tbody>
    </table>
  </div>

</body>
</html>

JSFiddle illustration gif

Xerosis answered 29/4, 2024 at 6:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.