This question already has answers here:
Using calc() with tables
(2 answers)
Closed 6 years ago.
I created two tables, and I want to set the width of the right 3 columns depending on the size of the first one. I tried calc((100% - 200px)/3) but it doesn't work in all browsers: Firefox 40 fails, IE11 fails, and Chrome 44 seems to do it right. How can I do it so that calc() is understood in all browsers? Or should I just forget it?
I created a much simpler version that fails just as well.
<table class="tableauTable">
<thead>
<tr class="tableauRow first">
<th colspan="3" rowspan="2" class="tableauCell first"><span class="xspTextComputedField">Objet - Acteurs</span>
</th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Julien GARDIN</span>
</th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Justine VINCLAIR</span>
</th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Marcel COMMAS</span>
</th>
</tr>
</thead>
<tfoot>
<tr class="tableauRow first">
<th colspan="3" header="" class="tableauCell first"></th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Julien GARDIN</span>
</th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Justine VINCLAIR</span>
</th>
<th class="tableauCell header col3"><span class="xspTextComputedField">Marcel COMMAS</span>
</th>
</tr>
</tfoot>
</table>
The same, with calc():
<table class="tableauTable">
<thead>
<tr class="tableauRow first">
<th colspan="3" rowspan="2" class="tableauCell first"><span class="xspTextComputedField">Objet - Acteurs</span>
</th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Julien GARDIN</span>
</th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Justine VINCLAIR</span>
</th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Marcel COMMAS</span>
</th>
</tr>
</thead>
<tfoot>
<tr class="tableauRow first">
<th colspan="3" header="" class="tableauCell first"></th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Julien GARDIN</span>
</th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Justine VINCLAIR</span>
</th>
<th class="tableauCell header col3x"><span class="xspTextComputedField">Marcel COMMAS</span>
</th>
</tr>
</tfoot>
The CSS:
.tableauTable {
width:100%;
border-spacing: 1px;
}
.tableauRow.first .tableauCell {
background: #d2d2d2 none repeat scroll 0 0 !important;
text-align: center;
}
.tableauCell.first {
width: 150px;
}
.tableauCell.col3 {
width: 30%;
}
.tableauCell.col3x {
width: calc(30%);
}
.tableauCell.first {
background: #d2d2d2 none repeat scroll 0 0 !important;
text-align: center;
}
.tableauCell {
background: #eee none repeat scroll 0 0;
border: 2px solid white;
color: black;
}
See http://jsfiddle.net/sjefb/19vrf52o/
why are you trying to calc a single value? calc(30%);
calc is very picky about syntax, particularly the gaps between resource values. You need to add spaces :
width: calc((100% - 200px) / 3);
^^^^ ^^^^^
Edit: This seems not to be the issue, instead approach that the width value is being applied to the cell (because firebug shows it is), but is different depending on cell contents, so this shows that the width value is being over-ridden, so try setting some other values:
in the table and the cell CSS definitions set:
box-sizing: border-box;
margin:0;
I solved my problem, using table-layout: fixed and, in order to fix column sizes, I used colgroup and col tags. Now calc() seems to behave, that is to say: the leftmost column has a fixed width and all other columns are equally sized.
Thanks all, for thinking with me!
Related
I have a Table and in the <thead> I have a background image, I have a coubple of rows and <th> this is the structure of my markup:
(Im using bootstrap by the way)
<table className="table">
<thead>
<tr>
<th colSpan="12">
<h3 className="table-title">POST APOCALYPTIC HIGHWAY</h3>
</th>
</tr>
<tr className="attributes">
<th className="position"></th>
<th className="img d-none d-sm-table-cell"></th>
<th className="fullname"></th>
<th className="bib text-center">Bib</th>
<th className="age d-none d-sm-table-cell text-center">
Age
</th>
<th className="gender text-center">Gender</th>
<th className="time text-center">Time</th>
<th className="score text-center">Score</th>
</tr>
{/** Ghost row to simulate margin */}
<tr style={{ height: "10px" }}>
<th colSpan="12"></th>
</tr>
</thead>
Im applying a background image through CSS to the <thead> this way:
.leaderboard thead {
background-image: url(./images/black-square-pattern.jpeg);
background-repeat: repeat;
color: #fff;
}
I also make sure there's 0 padding and margin in the <th> elements
.leaderboard thead th {
padding: 0;
margin: 0;
border: none;
}
It looks okay in desktop but for some reason when I test it in Google chrome inspecter the different mobile previews I see some tiny white spaces between the cells, I cannot find why it's happening, any idea how to fix it? I also tested in Safari and the same happens.
How do I go about making the 'description' column responsive? As you can see the bootstrap description is pushing my site. I would like the description go for a specific width, and the text to go behind the 'language' and have just one horizontal scroll bar for all of the description column.
I'm using bootstrap btw so the code doesn't contain a css but maybe it helps you anyway :)
<div className="table-responsive-lg">
<table className="table table-hover text-nowrap">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Language</th>
<th scope="col">Date Created</th>
<th scope="col">File Size (kb)</th>
<th scope="col">Open Issues</th>
</tr>
</thead>
<tbody>{data.map(item => (
<tr key={item.id}>
<td><a href={item.html_url}>{item.name}</a></td>
<td>{item.description}</td>
<td>{item.language}</td>
<td>{item.created_at.slice(0,10)}</td>
<td>{item.size}</td>
<td>{item.open_issues}</td>
</tr>
))}</tbody>
</table>
</div>
You can define a class with some CSS as below:
.fixed-width {
width: 400px;
display: block;
overflow-x: auto;
}
then add class fixed-width to all <th> and <td> tags in your "description" column.
Use this CSS.
.table {border-collapse:collapse; table-layout:fixed; width:310px;}
.table td {width:100px; /*add as per your requirements*/ word-wrap:break-word;}
Using Child Pseudo Or Unique Id OR Class.
Set as per screen required. Using #media.
I modified the previous answer a bit and add the .fixed-width class only on the column containing the description
.fixed-width {
max-width: 400px;
display: block;
overflow-x: scroll;
}
<div className="table-responsive-lg">
<table className="table table-hover text-nowrap">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Language</th>
<th scope="col">Date Created</th>
<th scope="col">File Size (kb)</th>
<th scope="col">Open Issues</th>
</tr>
</thead>
<tbody>
<tr key="1">
<td>Google</td>
<td class="fixed-width">some looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong description here</td>
<td>English</td>
<td>19/01/2021</td>
<td>38</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
You can define your css Like this:
tbody tr td:nth-child(2) {
width: 245px;
overflow-x: scroll;
max-width: 245px;
}
I am trying to implement cellpadding and cellspacing in my forum, but I just can't get the width="100%" to work with it.
Is it possible to set a width while using cellpadding or cellspacing in my CSS?
.categories div.category td tr{
padding: 80px;
width: 500px;
}
echo '
<table class="categories">
<caption>Category: ', $row2['category'], '</caption>
<thead>
<tr>
<td colspan="3">
<th colspan="3"><a href="create_music_sub.php?cat='.$row2['id'].'&admin='.$row2['admin'].'">
Click here to Create Sub Category
</a></th>
</td>
</tr><tr>
<div class="category"><th scope="col">Category</th></div>
<th scope="col">Creator</th>
<th scope="col">Date Created</th>
</tr>
</thead><tbody>';
If what you meant is to set the table's width to 100%, this works.
And as for the code you provided there were mistakes:
When you want to use a th you don't use a td with it.
The div.category should be inside the tr > th not wrapping it.
When you're posting a question please remove the php fragments and provide a working fiddle for us to be able to help you faster.
Please look at the code to see what I did differently. I think the problem was in your html
table{
border:1px solid black;
width: 100%;
}
tr, th, td{
border:1px solid black;
}
td,th{
padding: 10px;
}
/*If you want the last column to be smaller you can change one of them to a certain width*/
.creator{
width:20%;
}
<table class="categories">
<caption>Category: 1</caption>
<thead>
<tr>
<th colspan="3">
Click here to Create Sub Category
</th>
</tr>
<tr>
<th scope="col">
<div class="category">Category</div>
</th>
<th scope="col">Creator</th>
<th scope="col">Date Created</th>
</tr>
<tr>
<th colspan="2" scope="col">
<div class="category">Category</div>
</th>
<th scope="col">Creator</th>
</tr>
</thead>
</table>
<table class="categories">
<caption>Category: 1</caption>
<thead>
<tr>
<th colspan="3">
Click here to Create Sub Category
</th>
</tr>
<tr>
<th scope="col">
<div class="category">Category</div>
</th>
<th scope="col">Creator</th>
<th scope="col">Date Created</th>
</tr>
<tr>
<th colspan="2" scope="col">
<div class="category">Category</div>
</th>
<th scope="col" class="creator">Creator</th>
</tr>
</thead>
</table>
I'd like to make to following table adapt its width to the cells, instead of its container:
<div style='width: 700px; border: 1px solid red;'>
<table border="1" style='table-layout: fixed;'><tbody>
<tr>
<th style="" colspan="3">Group 1</th>
<th style="" colspan="5">Group 2</th>
<th style="" colspan="4">Group 3</th>
</tr>
<tr>
<th style="width: 62px;">A</th>
<th style="width: 200px;">B</th>
<th style="width: 62px;">C</th>
<th style="width: 62px;">D</th>
<th style="width: 62px;">E</th>
<th style="width: 62px;">F</th>
<th style="width: 62px;">G</th>
<th style="width: 62px;">H</th>
<th style="width: 62px;">I</th>
<th style="width: 100px;">J</th>
<th style="width: 62px;">K</th>
<th style="width: 68px;">L</th>
</tr>
</tbody></table>
</div>
The width of the container is narrower than the sum of the cell width. It is okay to show horizontal scroll bar here, but the table just can't be wider than its container. If I calculate the sum width for the colspan and the table, it can do the job, but what if the cells has non-px width, like "%" or "em"? Then static px calculation can't work.
Current effect is:
The expected effect would be the table's width exceeding the container div's width.
But setting width directly to the table also has problems: the width set for cells will be ignored. For example:
<div style='width: 700px; border: 1px solid red;'>
<table border="1" style='table-layout: fixed; width: 900px;'><tbody>
<tr>
<th style="" colspan="3">Group 1</th>
<th style="" colspan="5">Group 2</th>
<th style="" colspan="4">Group 3</th>
</tr>
<tr>
<th style="width: 62px;">A</th>
<th style="width: 200px;">B</th>
<th style="width: 62px;">C</th>
<th style="width: 62px;">D</th>
<th style="width: 62px;">E</th>
<th style="width: 62px;">F</th>
<th style="width: 62px;">G</th>
<th style="width: 62px;">H</th>
<th style="width: 62px;">I</th>
<th style="width: 100px;">J</th>
<th style="width: 62px;">K</th>
<th style="width: 68px;">L</th>
</tr>
</tbody></table>
</div>
The effect is:
Note the 2nd cell (should be 200px) is as narrow as the 1st and 3rd one.
Anybody could shed some light on this issue? Thanks in advance.
add css:
th{
min-width:50px; /*for instance*/
}
div{
overflow-x:scroll;
}
and remove style='table-layout: fixed; width: 1px;' from the table
Would positioning the table absolutely be acceptible?
<table border="1" style="position:absolute;">
This works such that the div no longer contains the table horizontally. But, of course, the div also no longer contains the table vertically. You could work around this by giving the div height manually, though.
Merijn's answer works, tweaked slightly, for the fixed-width cells case:
Instead of specifying a global th min-width, specify it per cell like so:
<th style="min-width: 62px; width: 62px;">A</th>
<th style="min-width: 200px; width: 200px;">B</th>
<th style="min-width: 62px; width: 62px;">C</th>
At least this way, your individual width's are not ignored, giving you a table wider than the container, and with irregular width cells.
I am using the following code to position table . It displays well in firefox and chrome but the table gets stick on the left edge of the internet explorer 9 but it should be on the right . Can someone suggest some modifications to this code to display it properly in internet explorer ?
Html code :
<table class="tab1">
<thead>
<tr>
<th scope="col" id="Dimension"><strong>DIMENSIONS</strong></th>
<th scope="col" id="Feet"><strong>FEET</strong></th>
</tr>
</thead>
</tbody>
<tr class="disp-Sing-Spc
">
<th class="color1" scope="col">A (Distance Between Signs)</th>
<th class="color1" scope="col" id="A">100</th>
</tr>
<tr class="disp-Sing-Spc
">
<th class="color2" scope="col">B (Distance Between Signs)</th>
<th class="color2" scope="col" id="B">100</th>
</tr>
<tr class="disp-Sing-Spc
">
<th class="color1" scope="col">C (Distance Between Signs)</th>
<th class="color1" scope="col" id="C">100</th>
</tr>
<tr id="disp-Tpr-Len">
<th class="color2" scope="col">L (Taper Length)</th>
<th class="color2" scope="col" id="Tpr-Len">None</th>
</tr>
<tr class="disp-cn-spc
">
<th class="color1" scope="col">Maximum Taper Channelizing Device Spacing</th>
<th class="color1" scope="col" id="max-taper-dv-spcing">None</th>
</tr>
<tr class="disp-cn-spc
">
<th class="color2" scope="col">Suggested Taper Channelizing Device Spacing</th>
<th class="color2" scope="col" id="Sugg-tpr-dev-spcing">None</th>
</tr>
<tr class="disp-cn-spc
">
<th class="color1" scope="col">Maximum Tangent Channelizing Device Spacing</th>
<th class="color1" scope="col" id="Max-Tgt-dev-Spcing">None</th>
</tr>
<tr class="disp-cn-spc
">
<th class="color2" scope="col">Suggested Tangent Channelizing Device Spacing</th>
<th class="color2" scope="col" id="Sugg-Tgt-dev-Spcing">None</th>
</tr>
<tr>
</tbody>
</table>
CSS Code :
.tab1{
border-collapse: collapse;
position: absolute;
right: 2%;
top: 95px;
width: 25%;
background: #EA9949;
}
In my IE9 it looks fine, however without the 2% right margin. You should try to float your div and use fixed values.
.tab1{
border-collapse: collapse;
position: absolute;
right: 30px;
float:right;
top: 95px;
width: 300px;
background: #EA9949;
}
By the way, you should get read of the tables and use divs instead.