Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table cells fixed height regardless the content of the cell

I have a dynamic table that I generate after getting some inputs from the user to present some tabular data. I need to know if there is away to assign a fixed height for the cells even if some of them have some content / text. I would like all the cells to have 30px height regardless if they have content or if they are empty.

This is the CSS I have:

table {
  width: 90%;
  height:100%;

}
    table th,  table td {
      border: 1px solid gray;
      height:30px !important;
      padding: 9px !important;
      width: 16%;
    }

The table is generated by this code:

foreach ( $this->rows as $i => $row ) {
            $tbody_tr = NHtml::el ('tr class="data-row-' . $i . '"');
            foreach ( $this->fields as $field ) {
                $tbody_tr->add(NHtml::el ('td')->class($field['name'])->setHtml(@$row[$field['name']]));
            }

But when I have a cell that has some text, the cell height expands anyway. Any ideas?

like image 520
digitup Avatar asked Aug 25 '11 12:08

digitup


People also ask

How do you make a cell fixed height in a table?

The height of rows 'tr' in a table can be fixed very easily. This can be done by adding the height attribute in the tr tag. If the height is not specified, the height of the row changes according to the content. The height can be specified either in pixels, or percentage.

How do you change the cell height of a table in CSS?

To set the cell width and height, use the CSS style. The height and width attribute of the <td> cell isn't supported in HTML5. Use the CSS property width and height to set the width and height of the cell respectively. Just keep in mind, the usage of style attribute overrides any style set globally.

How fix HTML table size?

To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.


1 Answers

Table cells don't overflow so you need to wrap the content in a div to use overflow

Here is an example: http://jsfiddle.net/avVQm/

HTML:

<table>
 <tr>
  <td>
   <div>
    gf asfdg fsagfag fdsa gfdsg fdsg fds g fdg
   </div>
  </td>
 </tr>
</table>

CSS:

table {
    width: 30px;
    border: 1px solid black;
}

table td > div {
    overflow: hidden;
    height: 15px;
}

Your PHP:

foreach ((array)$this->fields as $field ) {
 $wrapperdiv = NHtml::el ('div');
 $tbody_tr->add($wrapperdiv);
 $wrapperdiv->add(NHtml::el ('td')->class($field['name'])->setHtml(@$row[$field['name']]));
}

Note that I don't exactly know the syntax for the framework that you use, but I'd guess your php will look something like this.

like image 115
Bazzz Avatar answered Sep 29 '22 13:09

Bazzz