Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set the width of columns in a table using css?

Status
Not open for further replies.

darrenmorton93

IS-IT--Management
Apr 25, 2012
4
GB
Just wondering if anyone can tell me the code that is used to set column widths in a table so they are all different not just all the same width using HTML coding or via a style sheet and where the code would be entered?

Many Thanks
Darren
 
Hi Darren,

so they are all different not just all the same width
Huh? Don't get this part.
If you have a table with no parameters, e.g. no set width, the width of the columns will be defined by the content.

Please clarify as exactly as possible what you wish to achieve.

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Hiya,

I just mean how do you code it so that you can set the width of each column. I have set the table width to 1000 but I'm looking for a way to make the columns the following width:

Col1=100
Col2=100
Col3=100
Col4=200
Col5=500

Does that clarify it?

Thanks
Darren
 
Hi Darren,

Yes that clarifys it.
You can set the column width for the entire table with the help of a columngroup:
Code:
<table border="1" width="1000">
[b]<colgroup>
    <col width="100">
    <col width="100">
    <col width="100">
    <col width="200">
    <col width="500">
</colgroup>[/b]
<tr>
    <td>bla yadda
    ...
Now you don't have to set any more width at the td level.

Hope this helps.

Cheers,
MakeItSo


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
The "natural" behaviour of table column width is to be table_width/number_of_columns. BUT you want it to be different you can set the fixed width on either a column header <th> or a cell <td> in the column you wish to set. The column will then be as wide as the widest element is.

So setting a width on a table header or a cell for column four and five will mean that cols one to three should each be 1/3 of the remaining width.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Cheers buddy.

That helps a lot. Been puzzled at this for a little bit so gets me by it

Thanks

Darren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top