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.
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)
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.
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.
Template:
Please use CSS for structuring any layouts.
class
in thetd
tag instead of applying the style directlyth
like :<th style="width: 25%;"></th>
this will effect other columns widthtable-layout: fixed
. That's required because many templates built with BS4 apply flex to everything, including tables.Instead of applying the
col-md-*
classes to eachtd
in the row you can create acolgroup
and apply the classes to thecol
tag.Demo here
<col class="col-md-4 col-sm-6">
will have a width of 33% in medium screen size and 50% in small screen size)Hopefully this one will help someone:
https://github.com/twbs/bootstrap/issues/863
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.
th
to normal since it is nowrap currently as such headers won't break.In my case I was able to fix that issue by using
min-width: 100px
instead ofwidth: 100px
for the cellsth
ortd
.For Bootstrap 4, you can simply use the class helper:
.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.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:
Hope this will be helpful to someone else out there!
Cheers!
Try this -
text-overflow: ellipsis
to make sure the cut-off text is apparentBootstrap 4 and 5 have a width utility for resizing html element width Example:
This combined solution worked for me, I wanted equal width columns
Result :-
https://i.stack.imgur.com/L03Sy.png
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.
Ok, I just figured out where was the problem - in Bootstrap is set up as a default value
width
forselect
element, thus, the solution is:Anything else doesn't work me.
This is how I often do when I don't have to deal with IE
That way you can have a familiar 12-col grid.
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
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!)
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 withwhite-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 thetd
.So, always check the main container (like
table
ortd
) but also, always check if you don't cancel your beautifull code somewhere deeper :)use fixed table layout css in table, and set a percent of the td.
In bootstarp 4 you can use
row
andcol-*
in table, I use it as belowNone 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 aDIV
element with a specific width. For example for the cols "Person Name" and "Event date" I need a specific width, then put adiv
in the col header, the entire col width then resizes to the width specified from the div on theTH
: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.I also faced this problem and I follow this approach to overcome...So you can try
<td style="min-width:120px;">B</td>
use .something without td or th
Fixed width column
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:
Follow WeChat
Success story sharing
Want to stay one step ahead of the latest teleworks?
Subscribe Now相似问题
HuntsBot,a one-stop outsourcing task, remote job, product ideas sharing and subscription platform, which supports DingTalk, Lark, WeCom, Email and Telegram robot subscription. The platform will push outsourcing task requirements, remote work opportunities, product ideas to every subscribed user with timely, stable and reliable.
Platform
Support
Links
Contact US
Any questions or suggestions during use, you can contact us in the following ways:
Email: huntsbot@xinbeitime.com
微信公众号
Tips