ChatGPT解决这个技术问题 Extra ChatGPT

How to set up fixed width for <td>?

Simple scheme:

  <tr class="something">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
  </tr>

I need to set up a fixed width for <td>. I've tried:

tr.something {
  td {
    width: 90px;
  }
}

Also

td.something {
  width: 90px;
}

for

<td class="something">B</td>

And even

<td style="width: 90px;">B</td>

But the width of <td> is still the same.

I just tried and it works - maybe the problem is somewhere different. What does Firebug tell you about the element?
In this site, man is telling about this topic with video. It is so clear.
use style="width: 1% !important;" to make the width as tight as the longest word in the column, it's useful for the first ID/# column. Tested in chrome(desktop&mobile), opera (desktop), IE(desktop), edge(desktop), firefox(desktop)

M
MaPePeR

For Bootstrap 4.0:

In Bootstrap 4.0.0 you cannot use the col-* classes reliably (works in Firefox, but not in Chrome). You need to use OhadR's answer:

<tr>
  <th style="width: 16.66%">Col 1</th>
  <th style="width: 25%">Col 2</th>
  <th style="width: 50%">Col 4</th>
  <th style="width:  8.33%">Col 5</th>
</tr>

For Bootstrap 3.0:

With twitter bootstrap 3 use: class="col-md-*" where * is a number of columns of width.

<tr class="something">
    <td class="col-md-2">A</td>
    <td class="col-md-3">B</td>
    <td class="col-md-6">C</td>
    <td class="col-md-1">D</td>
</tr>

For Bootstrap 2.0:

With twitter bootstrap 2 use: class="span*" where * is a number of columns of width.

<tr class="something">
    <td class="span2">A</td>
    <td class="span3">B</td>
    <td class="span6">C</td>
    <td class="span1">D</td>
</tr>

** If you have <th> elements set the width there and not on the <td> elements.


What happens if you have more than 12 columns?
you can apply this only in the in each tag and all rows will match
Is there documentation about this on the Bootstrap website?
This works, but it's not on the bootstrap site because those col-* classes aren't meant to be used this way. They are meant to be used in the responsive bootstrap grids. If you like what these do, consider looking at the CSS for them and copy it to make your own CSS.
Also, instead of setting the class for reach row, we can simply set it once for the header and the rest is taken care of automatically!
H
Hunter Medney

I was having the same issue, I made the table fixed and then specified my td width. If you have th you can do those as well.

table {
    table-layout: fixed;
    word-wrap: break-word;
}

Template:

<td style="width:10%">content</td>

Please use CSS for structuring any layouts.


This is great, but as an addition use a class in the td tag instead of applying the style directly
This works thanks better just enough to increase the width of th like : <th style="width: 25%;"></th> this will effect other columns width
Thank you! I've set col-md-x class for Bootstrap 3 but it didn't work. Setting the table layout to "fixed" resolves my issue.
This works. The key is the table-layout: fixed. That's required because many templates built with BS4 apply flex to everything, including tables.
s
stormwild

Instead of applying the col-md-* classes to each td in the row you can create a colgroup and apply the classes to the col tag.

    <table class="table table-striped">
        <colgroup>
            <col class="col-md-4">
            <col class="col-md-7">
        </colgroup>
        <tbody>
        <tr>
            <td>Title</td>
            <td>Long Value</td>
        </tr>
        </tbody>
    </table>

Demo here


I prefer this but it seems not affect to my case, the column width not extend by col-md-*
I like this solution, by the way, why one is forced to use col-md-* classes and not col-lg-* ,col-sm-* or col-xs-*?
@LucioB you can use whichever you want and even use several of them by column (example <col class="col-md-4 col-sm-6"> will have a width of 33% in medium screen size and 50% in small screen size)
Colgroup is not working in Chrome. (Bootstrap v4.1). For uniformity across browsers, use % width for elements.
This helped me a lot. Exactly what I needed. Also Very flexible to use with different variations.
M
Murat Ozgul

Hopefully this one will help someone:

<table class="table">
  <thead>
    <tr>
      <th style="width: 30%">Col 1</th>
      <th style="width: 20%">Col 2</th>
      <th style="width: 10%">Col 3</th>
      <th style="width: 30%">Col 4</th>
      <th style="width: 10%">Col 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Val 1</td>
      <td>Val 2</td>
      <td>Val 3</td>
      <td>Val 4</td>
      <td>Val 5</td>
    </tr>
  </tbody>
</table>

https://github.com/twbs/bootstrap/issues/863


m
meobyte

If you're using <table class="table"> on your table, Bootstrap's table class adds a width of 100% to the table. You need to change the width to auto.

Also, if the first row of your table is a header row, you might need to add the width to th rather than td.


As an addition on Bootstrap 3 you will also need to set the white-space property of the th to normal since it is nowrap currently as such headers won't break.
T
ThiagoPXP

In my case I was able to fix that issue by using min-width: 100px instead of width: 100px for the cells th or td.

.table td, .table th {
    min-width: 100px;
}

This may have preserved what's left of my sanity.
Yes! this sets, well, the min-width for a column in a responsive table. Thanks.
this worked better than the answered question, thanks!
min-width worked for me where width did not, I'm guessing it's because I didn't make the table "fixed width" as I still wanted the rest of the columns to resize automatically.
This works for me too!
C
CHAN

For Bootstrap 4, you can simply use the class helper:

<table class="table">
  <thead>
    <tr>
      <td class="w-25">Col 1</td>
      <td class="w-25">Col 2</td>
      <td class="w-25">Col 3</td>
      <td class="w-25">Col 4</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      ...

This fit for my use case. Thanks!
Important to note that the only w-* options are: .w-25, .w-50, .w-75, .w-100, .w-auto so if you want something else, say, w-10 you will need to add .w-10 { width: 10% !important; } to your localized css file.
r
ravi879

Bootstrap 4.0

On Bootstrap 4.0, we have to declare the table rows as flex-boxes by adding class d-flex, and also drop xs, md, suffixes to allow Bootstrap to automatically derive it from the viewport.

So it will look following:

<table class="table">
    <thead>
        <tr class="d-flex">
            <th class="col-2"> Student No. </th>
            <th class="col-7"> Description </th>
            <th class="col-3"> Amount </th>
        </tr>
    </thead>

    <tbody>
        <tr class="d-flex">
            <td class="col-2">test</td>
            <td class="col-7">Name here</td>
            <td class="col-3">Amount Here </td>
        </tr>
    </tbody>
</table>

Hope this will be helpful to someone else out there!

Cheers!


Note limitation on this: seems as though if you use d-flex you lose the ability to use the align-* helper functions for the fields. it no longer has an effect.
R
Rohit Suthar

Try this -

<style>
 table { table-layout: fixed; }
 table th, table td { overflow: hidden; }
</style>

This is the only one that works out of all the other answers.
I also added text-overflow: ellipsis to make sure the cut-off text is apparent
@Observer07 Indeed
M
Martins

Bootstrap 4 and 5 have a width utility for resizing html element width Example:

<tr class="w-50">
    <td class="w-25">A</td>
    ...
</tr>

H
Hitesh Sahu

This combined solution worked for me, I wanted equal width columns

<style type="text/css">

    table {
        table-layout: fixed;
        word-wrap: break-word;
    }

        table th, table td {
            overflow: hidden;
        }

</style>

Result :-

https://i.stack.imgur.com/L03Sy.png


U
Usman Anwar

Use d-flex instead of row for "tr" in Bootstrap 4

The thing is that "row" class takes more width then the parent container, which introduces issues.

Hello World
8 columns 4 columns


I meant "row" instead of "tr". As the row class comes with gutters on both ends. Or if you wan't to use the "row" class just add "no-gutters" class.
Note limitation on this: seems as though if you use d-flex or row classes you lose the ability to use the align-* helper functions for the fields. it no longer has an effect.
u
user984621

Ok, I just figured out where was the problem - in Bootstrap is set up as a default value width for select element, thus, the solution is:

tr. something {
  td {
    select {
      width: 90px;
    }
  }
}

Anything else doesn't work me.


v
vothaison

This is how I often do when I don't have to deal with IE

    <tr>
      <th scope="col" style="width: calc(1 * 100% / 12)">#</th>
      <th scope="col" style="width: calc(4 * 100% / 12)">Website</th>
      <th scope="col" style="width: calc(3 * 100% / 12)">Username</th>
      <th scope="col" style="width: calc(3 * 100% / 12)">Password</th>
      <th scope="col" style="width: calc(1 * 100% / 12)">Action</th>
    </tr>

That way you can have a familiar 12-col grid.


D
David Heijl

Hard to judge without the context of the page html or the rest of your CSS. There might be a zillion reasons why your CSS rule is not affecting the td element.

Have you tried more specific CSS selectors such as

tr.somethingontrlevel td.something {
  width: 90px;
}

This to avoid your CSS being overridden by a more specific rule from the bootstrap css.

(by the way, in your inline css sample with the style attribute, you misspelled width - that could explain why that try failed!)


M
Mikk

I've been struggling with the issue for a while, so just in case when someone makes the same stupid mistake as me... Inside the <td> I had the element with white-space:pre style applied. That made all my table/tr/td tricks discarded. When I removed that style, suddenly all my text was nicely formatted inside the td.

So, always check the main container (like table or td) but also, always check if you don't cancel your beautifull code somewhere deeper :)


x
xiu

use fixed table layout css in table, and set a percent of the td.


S
Shojaeddin

In bootstarp 4 you can use row and col-* in table, I use it as below

<table class="table">
    <thead>
        <tr class="row">
            <th class="col-sm-3">3 row</th>
            <th class="col-sm-4">4 row</th>
            <th class="col-sm-5">5 row</th>
        </tr>
    </thead>
    <tbody>
        <tr class="row">
            <td class="col-sm-3">some text</td>
            <td class="col-sm-4">some text</td>
            <td class="col-sm-5">some text</td>
        </tr>
    </tbody>
</table>

Note limitation on this: seems as though if you use d-flex or row you lose the ability to use the align-* helper functions for the fields. it no longer has an effect.
S
Stephen Rauch

None of this work for me, and have many cols on datatable to make % or sm class equals to 12 elements layout on bootstrap.

I was working with datatables Angular 5 and Bootstrap 4, and have many cols in table. The solution for me was in the TH to add a DIV element with a specific width. For example for the cols "Person Name" and "Event date" I need a specific width, then put a div in the col header, the entire col width then resizes to the width specified from the div on the TH:

<table datatable [dtOptions]="dtOptions" *ngIf="listData" class="table table-hover table-sm table-bordered text-center display" width="100%">
    <thead class="thead-dark">
        <tr>
            <th scope="col">ID </th>
            <th scope="col">Value</th>
            <th scope="col"><div style="width: 600px;">Person Name</div></th>
            <th scope="col"><div style="width: 800px;">Event date</div></th> ...

R
Romeo Folie

For me, setting the table's layout to auto and targeting the specific columns' <th> did the trick. I found that targeting <td> in any of the rows works as well. Feel free to throw in !important if you need to.

.table {
  table-layout: auto;
}

th {
 width: 10%;
}

R
Rakib Ahasan

I also faced this problem and I follow this approach to overcome...So you can try

<td style="min-width:120px;">B</td>


Please provide an explanation to your code - it is very hard to understand something when it isn't explained. Please also provide the full code.
g
gecko

use .something without td or th

Bootstrap Example

Fixed width column

Firstname Lastname Email
John Doe john@example.com
Mary Moe mary@example.com
July Dooley july@example.com


s
s k

in Bootstrap Table 4.x

If you are creating the table in the init parameters instead of using HTML.

You can specify the width parameters in the columns attribute:

$("table").bootstrapTable({
    columns: [
        { field: "title", title: "title", width: "100px" }
    ]
});

u
user10861319


Add some explanation too.
This answer is confusing at best. Classes are weird, non-essential ids, no explanation.
Using CSS in this manner is quite irresponsible. If you have unique identifiers, you should be using ID, first off. Secondly, using pixels is miserable when it comes to responsiveness. Thirdly, you should never define css in this manner anyway, you will never be able to maintain this in the future. And finally, as stated above, you need to explain your reasoning.