I have a trouble with x-scroll table, when I try to resize window to small size. I have the same table (with same css) which doesn't contain row with images and it works great.
HTML:
<div class="price_wrapper">
<table class="price">
<thead>
<tr class="head">
<th>Название</th>
<th>Вольерная 1</th>
<th>Вольерная 2</th>
<th>Вольерная 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Схема</td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer1.jpg" alt=""></td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer2.jpg" alt=""></td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer3.jpg" alt=""></td>
</tr>
<tr>
<td>Ширина</td>
<td>90</td>
<td>120</td>
<td>150</td>
</tr>
<tr>
<td>Высота</td>
<td>60</td>
<td>70</td>
<td>80</td>
</tr>
<tr>
<td>Глубина</td>
<td>60</td>
<td>80</td>
<td>90</td>
</tr>
<tr>
<td>Лаз</td>
<td>30x35</td>
<td>39x42</td>
<td>42x53</td>
</tr>
<tr>
<td>Стоимость, руб.</td>
<td>7 000</td>
<td>9 000</td>
<td>11 000</td>
</tr>
</tbody>
</table>
</div>
CSS:
.price_wrapper {
overflow-x: scroll;
overflow-y: auto;
min-width: 400px;
width: 100%;
}
table.price {
border-collapse: collapse;
background-color: #FFF;
}
table.price th,
table.price td {
border: solid 1px #999;
padding: 5px;
}
table.price img {
max-width: 180px;
width: 100%;
height: auto;
}
JSFiddle: https://jsfiddle.net/vasromand/gue888vp/
Why is it?
You must use width for the scroll to
work.price_wrapper {
overflow-x: autol;
overflow-y: auto;
max-width: 400px;
width: 100%;
}
And dont use scroll because it will not be working in ie so instead you can use auto for it and set max-width or width to enable the auto scroll
Your content is not overflowing, you're using the width 90% of its container.. there never will be an overflow to scroll..
800px Content
table.price {
width: 800px;
margin: 10px 5%;
border-collapse: collapse;
background-color: #FFF;
}
400px container
.price_wrapper {
overflow-x: scroll;
overflow-y: visible;
width: 400px;
}
This will force the content to be wider than the wrapper.. which has the overflow-x
Also as correctly suggested by the other answer, use overflow:auto
Here is an updated version which will FORCE overflow to show you an example..
JSFiddle
As you can see i have forced the child to be wider than its parent.. Therefore offering scroll.
Related
I have a table
<div class="width1200">
<table>
<th colspan="2">Title:</th>
<tr>
<td>Harry Potter</td>
<td class="td100">$10</td>
</tr>
<th colspan="2">Title:</th>
<tr>
<td>Harry Potter</td>
<td class="td100">$10</td>
</tr>
</table>
</div>
table {
border-collapse: collapse;
table-layout: auto;
width: 100%;
}
td {
border: solid 1px black;
padding: 10px;
width: 100%;
}
.width1200 {
width: 1200px;
padding: 10px;
}
.td100 {
width: 100px;
}
I need to make 3 things:
The width of the table
On big screens it should be 1200 px
The table should be 100% width
The table should be responsive
Width of td
Second td should always be fixed - 100px
First td shoul be 100% of the free and responsive
At the moment the table width 100% doesn't work
You're contradicting yourself. I think you mean to say that on a big screen the table should have a maximum width of 1200px. Right?
<style>
table {
border-collapse: collapse;
table-layout: auto;
width: 100%;
}
td {
border: solid 1px black;
padding: 10px;
}
.width1200 {
max-width: 1200px;
padding: 10px;
}
.td100 {
width: 100px;
}
</style>
I changed width: 1200px; to max-width: 1200px; and removed width: 100%; from the td styling. I did not change the HTML.
I think now it does what you want?
See: Example
Here is the right one:
php code refactored ( wrong markup inside table )
<div class="width1200">
<table>
<thead>
<tr>
<th>Title:</th>
<th class="td100">Price:</th>
</tr>
</thead>
<tbody>
<tr>
<td>Harry Potter</td>
<td class="td100">$10</td>
</tr>
<tr>
<td>Harry Potter</td>
<td class="td100">$10</td>
</tr>
</tbody>
</table>
</div>
and new css code:, set width and max width to table and set only to cells with class td100 the specific with
table {
border-collapse: collapse;
width: 100%;
}
td {
border: solid 1px black;
padding: 10px;
}
.width1200 {
max-width: 1200px;
width:100%;
padding: 10px;
}
.td100 {
width: 100px;
}
table-layout: fixed allows you to set the widths of columns explicitly. The requirement is that the widths must be set on the <th> or <td> of the first <tr>. The OPs first <tr> has col="2" making it impossible to set a width for the second column. A workaround:
is to add another <tr> at the top
add 2 cells <th> and/or <td>
do not style any borders to the row
do not add any content to the new row either
add class="td100" to the 2nd cell of the new hidden row.
BTW each colspan="2" isn't in a <tr> in OP. They are added in this example.
table {
border-collapse: collapse;
table-layout: fixed;/* IMPORTANT */
width: 100%;
}
td {
border: solid 1px black;
padding: 10px;
}
.width1200 {
width: 1200px;
padding: 10px;
}
.td100 {
width: 100px;
}
<div class="width1200">
<table>
<tr><!--NEW-->
<th></th>
<th class="td100"></th><!--100px column-->
</tr><!--ROW-->
<tr>
<th colspan="2">Title:</th>
</tr>
<tr>
<td>Harry Potter</td>
<td>$10</td>
</tr>
<tr>
<th colspan="2">Title:</th>
</tr>
<tr>
<td>Harry Potter</td>
<td class="td100">$10</td>
</tr>
</table>
</div>
I have a simple HTML table as below.
This is a responsive table and shrinks down when the browser is shrunk.
I want to set the minimum width of the column to be as wide as the header text.
What is the best way to do this in CSS?
<table>
<thead class="ui-datatable-thead">
<tr class="ui-state-default">
<th class="ui-state-default">COL HEADER</th>
</tr>
</thead>
<tbody class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-odd">
<td>DATA</td>
</tr>
</tbody>
</table>
I have looked at some other answers but haven't found a solution yet.
table {
display: table;
width: 100%;
table-layout: fixed;
margin-bottom: 20px;
}
th,td {
display: table-cell;
border: 1px dotted red;
padding: 4px 6px;
width: 2%;
margin-bottom: 0;
}
<table>
<thead class="ui-datatable-thead">
<tr class="ui-state-default">
<th class="ui-state-default">COL HEADER 1</th>
<th class="ui-state-default">COL HEADER 2</th>
<th class="ui-state-default">COL HEADER 3</th>
</tr>
</thead>
<tbody class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-odd">
<td>DATA 1</td>
<td>DATA 2</td>
<td>DATA 3</td>
</tr>
</tbody>
</table>
All you need to set the cell width th & td table-layout to 'fixed'.Just like i mentioned in css code. If total size of table is 500px and there are 5 columns then each column will have 100px.
table{
border: 1px solid black;
table-layout: fixed;
width: 500px;
}
th, td {
border: 1px solid black;
width: 100px;
}
You need to fix the size of the heading's TD. so body's TD will automatically take width as applied in heading's TD.
Suppose I have a table with 100vh height, how to set minimum height for thead according to inside and maximum tbody?
<table style="height: 100vh;">
<thead>...</thead>
<tbody>...</tbody>
</table>
Is this what you look for?
Updated
Did a few tests and noticed giving height to the body didn't work properly cross browser, which below update does (tested on Chrome, Firefox, Edge, IE11)
html, body {
margin: 0;
}
table {
width: 100%;
height: 100vh;
}
table thead tr {
height: 80px; /* on table elements, height works kind of like min-height */
background: yellow;
}
table tbody tr {
background: lime;
}
<table>
<thead>
<tr>
<td>
HEAD<br>
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
BODY
</td>
</tr>
</tbody>
</table>
you can follow the hack to achieve your goal.
Use least height for thead, but not 0.Use 0% for tbody
html,body{
margin:0;
}
table{
width:100%;
height: 100vh;
border: 1px solid black;
}
thead{
background: red;
height:1px;
}
tbody{
background: blue;
height:0%;
}
<table>
<thead>
<tr>
<td colspan="4">
<img src="http://dummyimage.com/100x100/eb00eb/fff">
</td>
</tr>
</thead>
<tbody>
<tr>
<td>BODY</td>
<td>BODY</td>
<td>BODY</td>
<td>BODY</td>
</tr>
</tbody>
</table>
A simple quick question, i have this HTML
<div id="list">
<table class="t">
<thead>
<tr>
<th class="w100"><div>ID</div></th>
<th><div>NAME</div></th>
<th class="w100"><div>EXTRA</div></th>
<th class="w100"><div>EXTRA1</div></th>
</tr>
</thead>
<tbody style="top: 0px;" page="0">
<tr>
<td class="w100">ID</td>
<td>NAME</td>
<td class="wr100">EXTRA</td>
<td class="w100">EXTRA1</td>
</tr>
<tr>
<td class="w100">ID</td>
<td>NAME</td>
<td class="wr100">EXTRA</td>
<td class="w100">EXTRA1</td>
</tr>
</tbody>
</table>
</div>
CSS
#list {
position: relative;
height: 200px;
overflow: auto;
}
.t {
height: 20000px;
overflow: auto;
width: 100%;
}
.t>tbody {
position: absolute;
border:solid 1px;
}
tr {
height: 20px;
width: 100%;
}
.w100 {
width: 100px;
}
how can I extend the NAME column to fill up the remaining space inside a position absolute tbody just like the thead does?
I need this css to remain like this:
.t>tbody {
position: absolute;
}
and the table must have a height so that I can scroll past the tbody content
everything else can be changed
here is a demo
https://jsfiddle.net/wyzixg/f3gqgjgj/5/
Can this be achieved by css and/or js/jquery?
I think that the following is what you need.
You were pretty close, except that the absolute positioning was confusing the auto sizing algorithm used in table layouts.
If you set the width of the table to 100%, then the table will resize the columns to fill up the space. Since you set the width of all columns (except the 2nd one for NAME) to 100px, any remaining width will be allocated to the 2nd column since its width will be auto.
Since you need the tbody element to be position: absolute, you can still get the auto table sizing effect by using display: table on tbody, which looks a bit bizarre but it might do the trick.
I am not sure if your JavaScript will work as expected, but the layout seems to be what you need.
There is an artifact, a second horizontal scroll bar, which can probably be removed with some experimentation, but I did not try it (yet).
body { margin: 0;}
.c-list {
position: absolute;
width: 99%;
height: 400px;
overflow: auto;
-webkit-overflow-scrolling: touch;
border: 1px dotted blue;
}
table {
}
.t tbody {
position: absolute;
width: 100%;
display: table;
}
table td {
text-align: center;
border: solid 1px;
margin: 0;
padding: 0;
}
.w100, .wr100 {
width: 100px;
}
.wr100 {
text-align: right;
}
<div class="c-list">
<table class="t">
<tbody>
<tr>
<td class="w100">ID</td>
<td>NAME</td>
<td class="wr100">EXTRA</td>
<td class="w100">EXTRA1</td>
</tr>
<tr>
<td class="w100">1</td>
<td>1</td>
<td class="wr100">1</td>
<td class="w100">1</td>
</tr>
<tr>
<td class="w100">2</td>
<td>2</td>
<td class="wr100">2</td>
<td class="w100">2</td>
</tr>
<tr>
<td class="w100">3</td>
<td>3</td>
<td class="wr100">3</td>
<td class="w100">3</td>
</tr>
<tr>
<td class="w100">4</td>
<td>4</td>
<td class="wr100">4</td>
<td class="w100">4</td>
</tr>
<tr>
<td class="w100">5</td>
<td>5</td>
<td class="wr100">5</td>
<td class="w100">5</td>
</tr>
</tbody>
</table>
</div>
Use <thead> for table headers, along with <th>, so you can set the header width to 100% to fill the remaining space, and it will apply for the entire column.
Check this snippet for a better view of the result.
Also you don't really need the height for the table, and absolute.
.c-list {
position: absolute;
width:99%;
height:400px;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
table td {
text-align: center;
border:solid 1px;
margin:0;
padding:0;
}
.t > tbody {
position: absolute;
width: 99%;
}
table {
width:100%;
}
table tr{
width:100%;
}
table tbody {
width:100%;
}
.w100 {
width:100px;
}
.wr100 {
width:100px;
text-align: right;
}
<div class="c-list" >
<table style="height: 2000px;" class="t">
<tbody style="top: 0px;" page="0">
<tr><td class="w100">ID</td><td>NAME</td><td class="wr100">EXTRA</td><td class="w100">EXTRA1</td></tr>
<tr><td class="w100">1</td><td>1</td><td class="wr100">1</td><td class="w100">1</td></tr>
<tr><td class="w100">2</td><td>2</td><td class="wr100">2</td><td class="w100">2</td></tr>
<tr><td class="w100">3</td><td>3</td><td class="wr100">3</td><td class="w100">3</td></tr>
<tr><td class="w100">4</td><td>4</td><td class="wr100">4</td><td class="w100">4</td></tr>
<tr><td class="w100">5</td><td>5</td><td class="wr100">5</td><td class="w100">5</td></tr>
</tbody>
</table>
</div>
Here's a DEMO
Here's my HTML:
<div>
<table>
<tbody>
<tr>
<td>A1</td>
<td>B1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
</tr>
</tbody>
</table>
</div>
and CSS:
div {
position: relative;
left: 100px;
}
table {
border: 1px solid black;
}
As you can see in the demo, it causes horizontal scrollbars. Is there any way to get rid of this, other than using javascript?
instead of left maybe try
margin-left: 100px;