Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is browser showing td's larger than my specified width property?

Tags:

css

td width is 162px even though style="width:25px" for td and th

I've put this in my css file

table, td, th {
    border: 1px solid black;
}
table {
    border-collapse:collapse;
}

table.narrow th, td {
    width:50px;
}

It doesn't work. The last one there is supposed to take a table with class="narrow" and make all its descendent th and td elements 50px wide. And chrome shows Matched CSS Rules:

.narrow th, td { width: 50px; }

So chrome is reading it and ignoring it? Or CSS is just idiotic? Or I am? Why can't CSS just do what you tell it, like a normal programming language?

SO, I tried putting style="width:25px" right in the 1st column td AND th tags and still, doesn't work. Chrome shows:

element.style { width:25px; } 

but it won't tell me why the hell it's displaying it at 162px.

Anyone have any clues? Thank you.


Edit: Kolink gave me the clue I needed. Form text fields inside the tags were causing a large minimum width. I added this style to shrink the table:

.narrow input{
  width:80px;
}

It reduced the width of all the inputs inside the class="narrow" table and hence reduced the width of the table.

like image 628
Buttle Butkus Avatar asked Nov 06 '11 08:11

Buttle Butkus


People also ask

How do I change my TD size?

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 TD table width?

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.

What does td width do?

The HTML <td> width Attribute is used to specify the width of a table cell. If width attribute is not set then it takes default width according to content. Attribute Values: pixels: It sets the width of table in terms of pixels.

How do I change the width of a row in HTML?

Just add the property : white-space: nowrap; to your table or the cells you want to have dynamic width. That's it!


2 Answers

Set table-layout: fixed; on the table's styles. Otherwise table layout is loose and width is treated more like min-width.

like image 131
Niet the Dark Absol Avatar answered Nov 16 '22 01:11

Niet the Dark Absol


Your definition is for all TDs

table.narrow th, td {
    width:50px;
}

You have to repeat the affected element in the enumeration

table.narrow th, table.narrow td {
    width:50px;
}
like image 25
Gael Avatar answered Nov 16 '22 00:11

Gael