How to make table td taking up the whole width using css? - html

I am making a site with tables mobile responsive. How do I make table td take up the whole full width(100%) using css?
<table>
<tr>
<td>Column One</td>
<td>Column Two</td>
</tr>
</table>
It is too hard to read information in two columns close to each other.

This will show the cells one below the other:
td {display:block;width:99.9%;clear:both}
or, as noted by #nux,
td {display:block; box-sizing:border-box; clear:both}
either should be enough, although microsoft browser won't oblige and you might need proprietary markup for those; then again if people plan to use a browser on their phone they wouldn't buy a microsoft phone, so the problem is minor.

When in mobile screen, change the tr to display:flex and td set to width 100%. Like below:
#media screen and (max-width: 576px){
tr {
display: flex;
flex-wrap: wrap;
}
td {
width: 100%;
}
}
If your table td has a border, you might want to remove the first columns of td border in mobile so that first column td content will looks like it is in the same columns with 2nd td in mobile:
tr td:first-child {
border: none;
}

The answers above are correct but you need to also make sure you'r td element has a reference for its 100% width otherwise it may not work. You should have this rule set at the start of your stylesheet:
body, html {
width: 100%;
}
Take a look at this thread for more info:
HTML table td not full width when using display block and 100% width attributes

First, you need to make sure that your table has 100% width, then you can do the same with the th and td
table, td {
width: 100%;
}
EDIT:
since you edited your original post, you should give the first td a class
<td class="first">
td.first {
width: 100%;
}
This will cause the first column to use as much of the page as it can and the second td will be just wide enough for the content. You may have to give it a width too if you don't want your text to wrap or something.

Related

Different width for tbody and thead

I have an HTML structure from an legacy app I have to style like our new app. For this I need to add margin only for the tbody. So setting padding in table won't work because the header should be as wide as the table.
Here's a little sketch what it should look like:
Why do I need that? I have to put two tables side by side and it should look like as there is only one header but two content tables.
I played around with padding and borders of the thead element but the problem is that the thead has a bottom border which isn't applied to the right border.
Edit:/
The picture is about what I want. The two tables are mentioned because that's the reason I want one table to look like that.
Solutions are welcome if they style the table like I showed in the picture or style two tables with two different tbodys and theads like they would have one thead.
What I need are still correct labels for the columns in tbody but the left and the right column should be a little bit wider to stretch it to the whole table.
Edit:/
Because there was some confusion what I meant here is a screenshot of the style I want to accomplish without altering the DOM structure with JS:
set display:table to tbody and use custom width for that easily.
Complete Demo
HTML:
<table>
<thead>
<tr><td>Head1</td></tr>
<tr><td>Head2</td></tr>
</thead>
<tbody>
<tr><td>Body1</td></tr>
<tr><td>Body2</td></tr>
<tr><td>Body3</td></tr>
<tr><td>Body4</td></tr>
</tbody>
</table>
CSS:
table{
width: 500px;
background: #808080;
}
thead{
width: 100%;
text-align: center;
background: #FF6347;
}
tbody{
display: table;
width: 400px;
margin: 0 auto;
text-align: center;
background: #FFF;
}
your calendar is actually two subtables (including a header with day names). from that point of view, using two cells with padding on the outer table would give you the desired effect

How to change table width

I am trying to decrease a table width to 700px
the table is underneath < h2>Cart Totals< /h2> and no matter how I apply "width: 700px;" the table will not decrease in size.
http://clients.rocoru.com/trendyheadboards/?page_id=6
Your problem is the following rule in master.css, line 687:
#costing-container, .shipping {
width: 980px;
border-top: 1px dotted #ccc;
}
Your applied the shippingclass to a tr inside your table, thus stretching it to 980px. If your remove that class or change its width-rule to 700px your table will display like you want it to.
The answer provided by Johannes sounds correct, but if you do not want to modify the master.css file, try applying an id to the table in question and change the other css to
#table_id tr {
width: 700px !important;
}
This should ensure that your styling doesn't shrink other table elements.

Using CSS td width absolute, position

Please see this JSFIDDLE
td.rhead { width: 300px; }
Why doesn't the CSS width work?
<table>
<thead>
<tr>
<td class="rhead">need 300px</td>
<td colspan="7">Week #0</td>
<td colspan="7">Week #1</td>
<!-- etc..-->
</tr>
<tr>
<td class="rhead"></td>
<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
<!-- etc... -->
</tr>
<thead>
</table>
Also, what are the effects of position:fixed, absolute etc have on td widths if any? I am looking for a reason more than a fix. I am hoping to understand how it works.
This may not be what you want to hear, but display: table-cell does not respect width and will be collapsed based on the width of the entire table. You can get around this easily just by having a display: block element inside of the table cell itself whose width you specify, e.g
<td><div style="width: 300px;">wide</div></td>
This shouldn't make much of a difference if the <table> itself is position: fixed or absolute because the position of the cells are all static relative to the table.
http://jsfiddle.net/ExplosionPIlls/Mkq8L/4/
EDIT: I can't take credit, but as the comments say you can just use min-width instead of width on the table cell instead.
You're better off using table-layout: fixed
Auto is the default value and with large tables can cause a bit of client side lag as the browser iterates through it to check all the sizes fit.
Fixed is far better and renders quicker to the page. The structure of the table is dependent on the tables overall width and the width of each of the columns.
Here it is applied to the original example: JSFIDDLE, You'll note that the remaining columns are crushed and overlapping their content. We can fix that with some more CSS (all I've had to do is add a class to the first TR):
table {
width: 100%;
table-layout: fixed;
}
.header-row > td {
width: 100px;
}
td.rhead {
width: 300px
}
Seen in action here: JSFIDDLE
The reason it doesn't work in the link your provided is because you are trying to display a 300px column PLUS 52 columns the span 7 columns each. Shrink the number of columns and it works. You can't fit that many on the screen.
If you want to force the columns to fit try setting:
body {min-width:4150px;}
see my jsfiddle: http://jsfiddle.net/Mkq8L/6/
#mike I can't comment yet.
The reason, is, because you did not specify the width of the table, and your whole bunch of td's are overflowing.
This for example, i've given the table a width of 5000px, which I thought would fit your requirements.
table{
width:5000px;
}
It is the exact same code you provided, which I merely added in the table width.
I believe what is happening, is because your TD's are way past the default table width. Which you could see, if you pull out about 45 of your td's in each tr, (i.e. the code you provided in your question, not jsfiddle) it works exactly fine
Try this it work.
<table>
<thead>
<tr>
<td width="300">need 300px</td>
Try to use
table {
table-layout: auto;
}
If you use Bootstrap, class table has table-layout: fixed; by default.
My crazy solution.)
$(document).ready(function() {
$("td").each(function(index) {
var htmlText = "<div style='width:300px;'>" + $(this).text() +"</div>";
$(this).html(htmlText);
});
});
Use table-layout property and the "fixed" value on your table.
table {
table-layout: fixed;
width: 300px; /* your desired width */
}
After setting up the entire width of the table, you can now setup the width in % of the td's.
td:nth-child(1), td:nth-child(2) {
width: 15%;
}
You can learn more about in on this link: http://www.w3schools.com/cssref/pr_tab_table-layout.asp
If table width is for example 100%, try using a percentage width on td such as 20%.
Wrap content from first cell in div e.g. like that:
HTML:
<td><div class="rhead">a little space</div></td>
CSS:
.rhead {
width: 300px;
}
Here is a jsfiddle.
You can also use:
.rhead {
width:300px;
}
but this will only with with some browsers, if I remember correctly IE8 does not allow this. Over all, It is safer to just put the width="" attribute in the <td> itself.

How limit the width of a table with CSS or event HTML?

I was curious if this is possible with CSS, or even HTML? Say, I have a parent DIV that houses a TABLE:
<div class="divClass">
<table class="tableClass">
</table>
</div>
The table displays user content, so I have no control over its text. Is there any way to limit the width of the table so that it doesn't go over, say 750 pixels?
PS. Normally table cells wrap words to the next line if there's not enough width, but problem happens forWordsLikeThisWithoutAnyActualWhiteSpacesBetweenThem. In that case the table gets too wide and destroys the page layout.
.tableClass {
max-width: 750px
word-break: break-all;
}
should do the work.
or you may also try
.tableClass {
table-layout:fixed;
}
.tableClass td {
overflow: hidden;
}
and assign width for each tds in the first line.
Write like this:
.tableClass{
display-layout:fixed;
word-wrap: break-word;
}
td{word-wrap:break-word;
max-width: 750px;}

How to create a table with fixed layout?

Hai ,
I am using a html table for my website layout .
when we press enter in one cell , the cell is resizing.
I used css for fixing the table layout. table layout is not changing .But cell is resizing.
.pageContent table
{
height:100%;
width:100%;
table-layout:fixed;
}
.pageContent table tr td
{
padding:5px 10px 5px 10px;
}
how to prevent this resizing of table cells ?
Your table is re-sizing because you are using proportional widths.
There are many ways you can control this. For example, by setting your or height in pixels:
CSS:
table td {height: 20px;}
Your cells will no longer re-size vertically.
Have you tried adding the following CSS
td { overflow:hidden;white-space:nowrap; }
tr { height:1em; } /* or set an appropriate height and width */
This will hide the overflow in the cells however, but they won't resize.
Your layout might be easier to do with semantically correct HTML, using <div> elements but would need to see the markup.
It's hard to be sure, but you haven't set a size on the td in question, only padding. So the td will be expanding to contain whatever is inside it. In some browsers this might change a wee bit with focus.
Could you try setting an explicit width and height (in ems or pixels) for the td?
.pageContent table tr td
{
padding: 5px 10px 5px 10px;
width:8em;
height:2em;
}
Could you post a bit of your markup, or let us know what the content in the cells is? (And which browsers you're seeing the problem in).