Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Table columns width: fixed - dynamic(30%) - dynamic(70%) - fixed

Tags:

html

css

I managed to obtain this kind of table layout:

fixed - dynamic(50%) - dynamic(50%) - fixed

http://jsfiddle.net/ihtus/ksucU/

enter image description here

But how do I get this kind? fixed - dynamic(30%) - dynamic(70%) - fixed

enter image description here

Here's my CSS:

table {
    width:100%;
    border-collapse:collapse;
    table-layout:fixed;
}

td {
    border: 1px solid #333;
}
like image 941
ihtus Avatar asked Sep 28 '12 13:09

ihtus


People also ask

How do you fix a fixed width table?

By using CSS, the styling of HTML elements is easy to modify. To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.

How do I fix td width in CSS?

Complete HTML/CSS Course 2022 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.

How do I make all the columns the same width in CSS?

The CSS to make all the columns equal in width is as follows. The table-layout: fixed; rule specifies that the table is to take its dimensions from the <table> element's width and the width of the first row of cells (or the <col> elements, if you have them).


2 Answers

Like this:

<table>
    <tr>
      <td style="width:200px;">
        200px width - content
      </td>
      <td width="30%">
        dynamic width - content
      </td>
      <td width="70%">
        dynamic width - content
      </td>
      <td style="width:100px;">
         100px width - content
      </td>
    </tr>
  </table>

table {
    width:100%;
    border-collapse:collapse;
    table-layout:fixed;
}

td {
    border: 1px solid #333;
}

http://jsfiddle.net/7dSpr/

like image 84
Mr. Mr. Avatar answered Nov 15 '22 19:11

Mr. Mr.


The general approach is the same as the one Monkieboy used, but you should avoid inline styles. ( by that I mean writing style="someting" ) in your html file. You should use classes and CSS instead.

First give the td a class like this <td class="thin-column">text here</td>, then in your CSS use that to apply styles: .thin-column:{ width: 30% }

like image 24
Richard Otvos Avatar answered Nov 15 '22 21:11

Richard Otvos