Using CSS td width absolute, position - html

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.

Related

HTML Table Problem: Static width with display:block and variuos text length

actually i got a problem with the view of a table. I need a horizontal scrollbar in my <td>. For this i found here a solution:
<table style="min-width: 100%; display: block; margin-bottom: 10px; border-collapse: collapse;" class="table table-striped table-hover table-responsive">
<tr class="list" style="width: 100%; margin-top: 5px;">
<td style="display: block; min-width: 100%; margin-bottom: 10px; white-space: nowrap; overflow:auto;" colspan="2">
<?php echo user_view_explorer_form (); ?>
</td></tr>
</table>
But now i got the problem, that some elements of the <td> are very short and some are very long. In case the elements are to short for a width:100%-view, the whole table only shows has the width of the longest element.
I cant find a solution where I can scroll in case of long elements but also got a "width:100%"-table in case of short elements.
Sorry for bad english. Thank you for your help. Stay healthy.
Picture shows you the problem.enter image description here
Applying display: block to a table element (be it table or td) destroys the automatic formating as a table in HTML - it simply doesn't make sense to do that (you could as well work with divs then)
So erase all this display: block instances! To set the table to 100% width, simply use width: 100% on table
To limit the height of a td and make it scroll vertically, you could put a div into that td and apply a fixed height to it. But all inside a working table structure without display: block on any table/tr/td element...
As i understood what you write here, I can offer you, try to wrapper element using for width configuration where you have problem on giving a width. If you write your all code here(including where you have problem) i may help you better than now :)

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

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.

Resize a table cell using css resize:both; not working with table

Resize a table cell using css {resize:both;} not working with table
I need to resize table and its cell, resize able using css ,
css {resize:both;} is working in div but not in table tags
<table border="1" style="resize:both;">
<tr>
<td>hi table</td>
<td>hi div</td>
</tr>
<tr>
<td>hi table</td>
<td>hi div</td>
</tr>
<tr>
<td>hi table</td>
<td>hi div</td>
</tr>
</table>
can any body help
To cover Chrome and Safari, use
th, td { resize: both; overflow: auto; }
Do not set initial width on the table as a whole. You can set the widths of cells or columns, though.
To cover Firefox as well, Iā€™m afraid you would need to wrap the content of each cell on a div with a class and add the corresponding class selector to the rule above. And this would introduce two resize handles on Chrome and Safari in each cell... so maybe you should use
<td><div class=cell>...</div></td>
for every cell and the CSS rule
.cell { resize: both; overflow: auto; width: 100%; height: 100%; }
(without making td elements resizable).
This way, all cells are resizable, so the table as a whole is indirectly resizable (as long as you do not set its width and height).
In principle, resize applies to all elements with overflow property other than visible. In practice, it works in a much more limited way, and differently in different browsers.
It appears that resize: is not applicable to tables.
table {
border:2px solid;
padding:10px 40px;
width:300px;
resize:both;
overflow:auto;
}ā€‹
http://jsfiddle.net/Kyle_/q4qwp/
But you can wrap the table in a div and set the resize: to the div instead, but you cannot shrink the table even with % width values.
http://jsfiddle.net/q4qwp/3/
To resize tables and table columns you have to use Javascript. There are several jQuery plugins out there doing so, for example colResizable, which allows you to drag column anchors manually.
Here is a small snippet of code:
$("#tabe").colResizable({});
you can find more examples on: http://www.bacubacu.com/colresizable/
or you can try this fiddle: http://jsfiddle.net/euka4rm3/
you can use styles of the outer div to change position of the table. For example
<div style="resize:both">
<table id="sample table"></table>
</div>

Why doesn't IE respect table width with fluid image child

Consider the following code:
HTML:
<table>
<tr>
<td><img src="http://watduck.jpg.to" /></td>
</tr>
</table>
CSS:
table { width: 10% }
img { max-width: 100% }
The image should obviously be a 10th the width of the window, which is exactly what it is in every browser except IE, where it simply falls back to its original size.
However, consider this:
HTML:
<div><img src="http://watduck.jpg.to" /></div>
CSS:
div { width: 10% }
img { max-width: 100% }
which IE does get right, and displays at a 10th of the window width.
So, here's the question: what causes this behavior, and what could possibly be done to force IE to respect the table's width?
Tested in IE8 & IE9 (don't care about IE7 and below).
If you specify table-layout: fixed; in the table css it works.
There seems to be some contradictory terminology in the standard regarding table layouts. In particular, table-layout: auto; says this:
The column width is set by the widest unbreakable content in the cells
Since the images content is unbreakable, it sets the width of the cell to the size of the content. The max-width seems to be overriden by it.

How to create a 3 columns fluid fixed fluid layout?

I'm looking for a 3 column css layout, with 1 fixed section at the middle and 2 fluid sidebar around it:
http://www.uploadup.com/di-UEFI.png
middle has 250px width (for example) and sidebars have (at minimum) 150px width. if browser width was longer than 550px (250+300), sidebars should have a longer width. (and middle always is 250px)
What is the CSS can do it? with compatibility in all browsers.
note: i saw this page, but i don't know how to change it for my wish
You can try to use inline-blocks for it. They are used rather rarely, but sometimes they are pretty good for layouts.
So, look at this: http://jsfiddle.net/kizu/UUzE9/ ā€” with inline-blocks you can create layouts with any number of fixed and fluid columns. The algorithm:
At first, you add the padding equal to the sum of all the fixed columns to the wrapper. In your case ā€” 250px.
Then, you add min-width to the wrapper equal to the sum of all the fluid columns' min-width.
Then, you add white-space: nowrap to the wrapper, so the columns won't jump.
And then just add the all columns that you need.
If you need support for IE7 and lesser, there are some additional things to know except for common inline-block fix:
You must return white-space: normal to the inner child of a column, or the columns won't stay on one line.
There can appear a phantom scroll in IE, maybe there is a better way to remove it, but I just use overflow: hidden on some wrapper.
Enjoy :)
To make this work in IE6/7 without JavaScript, the easiest way to do this is with a table.
I know, I know. It's not that bad in this case, all considered.
See: http://jsfiddle.net/thirtydot/Q2Qxz/
Tested in IE6/7 + Chrome, and it will just work in all other modern browsers.
HTML:
<table id="container" cellspacing="0" cellpadding="0">
<tr>
<td id="left">fluid</td>
<td id="mid">fixed</td>
<td id="right">fluid</td>
</tr>
</table>
CSS:
html, body {
margin: 0;
padding: 0;
border: 0
}
#container {
border: 0;
table-layout: fixed;
width: 100%
}
#container td {
vertical-align: top
}
#mid {
width: 250px;
background: #ccc
}
#left {
background: #f0f
}
#right {
background: #f0f
}
If you don't use one of the ready templates out there,
You can start by three div floated left, the middle with width: 250px and the outside ones with
min-width: 150px
You might want to replace it with the <section> tag, just give it a display: block