My issue is simple and i suspect the answer is its not possible as i couldn't find any examples.
I have a table built with css and HTML. I would like to re-size it proportionally as if it was an image so it fits inside a DIV. I know you guys know what a table looks like but seems like i have to submit some code to submit this questions so i added it below
<table>
<tr>
<td></td>
</tr>
</table>
Assuming that's not possible is it possible to convert a table into an image and then i can re-size the image?
You can set the width of the table to be 100% of the containing div, then set the <td>'s to be a percentage of the table.
An example would be something like this: http://jsfiddle.net/9VFQ5/
<div class="container">
<table>
<tr>
<td>Test 1</td>
<td>Test 2</td>
</tr>
</table>
</div>
.container {
width: 25%;
background-color: red;
}
table {
width: 80%;
margin: 0 auto;
background-color: blue;
}
td {
width: 50%;
color: white;
}
.container {
width: 100%;
background-color: blue;
}
table {
width: 80%;
margin: 0 auto;
background-color: red;
}
td {
width: 50%;
color: white;
padding: 1rem;
background-color: green;
}
th {
color: black;
}
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 container">
<table>
<tr>
<th>1st COLUMN</th>
<th>2nd COLUMN</th>
</tr>
<tr>
<td>Table Data1 Row1</td>
<td>Table Data2 Row1</td>
</tr>
<tr>
<td>Table Data1 Row2</td>
<td>Table Data2 Row2</td>
</tr>
</table>
</div>
Related
I have a problem that I do not understand regarding scaling of child elements inside html tables.
To start from the beginning, my goal is to have a table and to overlay the table body with a div using z-index. With the proper color this then looks like the table is inactive and it can not be clicked.
Below you find a minimal example of how this looks like due to some other requirement. As you can see, the overlay div always scales to the full table and not only a part of it. No matter if it is a child of tbody nor of one cell (as it is right now in the example code - I thought maybe a div is not allowed as a direct child of tbody but instead I could put one overlay div in each table cell).
table {
border: 1;
border-color: blue;
}
#content {
position: relative;
}
#content * {
position: inherit;
}
#overlay {
top: 0;
left: 0;
/*bottom: 0;
right: 0;*/
position: absolute !important;
background: rgba(0, 0, 0, .4);
z-index: 3;
width: 100%;
height: 100%
}
<div id="content">
<div>
Some stuff before
</div>
<br>
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
Cell
</div>
<div id="overlay"></div>
</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Footer 1</td>
<td>Footer 2</td>
</tr>
</tfoot>
</table>
<br>
<div>
Some stuff after
</div>
</div>
I do not understand why this happens which makes it hard for me to think of a fix and I hope somebody can help me with that and provide an explanation.
Cheers
Your table cells also need to be set to position: relative. Instead of #content * directly set them using td or at least #content td.
td {
position: relative;
}
#overlay {
top: 0;
left: 0;
position: absolute;
background: rgba(0, 0, 0, .4);
z-index: 3;
width: 100%;
height: 100%
}
<div id="content">
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
Cell
</div>
<div id="overlay"></div>
</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Footer 1</td>
<td>Footer 2</td>
</tr>
</tfoot>
</table>
</div>
So why does this work?
Short answer: position is not inheritable and Chrome has some problems with table tags.
But that's only half the truth. All major browsers (Tested with Chrome, Firefox and IE11) allow most (or probably even all) properties to inherit from their parent. When you test your snippet in Firefox or IE11 you will see that it indeed works. So why not in Chrome then?
It turns out that Chrome has some issues with the table-* display modes and the table-tags themselves. It always uses the default position value (which is static) when being inherited. Even if the closest parent has position: relative set, it won't work as illustrated in the next code snippet.
So to conclude: It's best to always explicitly set position: relative and in most cases avoid using inherit all together.
main {
position: relative;
border: 2px solid #000;
width: 200px;
margin-bottom: 20px;
}
table, .fake-table {
min-height: 50px;
margin: 10px;
}
table, tr, td, .fake-table div {
position: inherit;
}
.relative {
position: relative;
}
em {
position: absolute;
left: 0;
top: 0;
width: 100%;
min-width: 100px;
height: 100%;
background: rgba(166, 199, 252, .9);
text-shadow: 0 0 10px #fff;
}
.fake-table .table {
display: block;
}
.fake-table .row {
display: flex;
justify-content: flex-start;
}
.fake-table .cell {
display: inline-block;
min-width: 40px;
}
<main>
<table>
<tr>
<td>...</td>
<td>
cell
<em>Starting point (inherited table)</em>
</td>
</tr>
</table>
</main>
<main>
<table>
<tr class="relative">
<td>...</td>
<td>
cell
<em>Parent is set to relative</em>
</td>
</tr>
</table>
</main>
<main class="fake-table">
<table class="table">
<tr class="row">
<td class="cell">...</td>
<td class="cell">
cell
<em>Table tags in different display mode</em>
</td>
</tr>
</table>
</main>
<main class="fake-table">
<div class="table">
<div class="row">
<div class="cell">...</div>
<div class="cell">
cell
<em>Table using flex (behaves correctly)</em>
</div>
</div>
</div>
</main>
<main>
<table>
<tr>
<td>...</td>
<td class="relative">
cell
<em>Expected rendering</em>
</td>
</tr>
</table>
</main>
I want to center a table, but although it's technically centered, the different length of the td's content makes the table have an empty space on the right.
.wrap {
background: pink;
padding: 0 50px;
}
table {
margin: 0 auto;
text-align: center;
width: 100%;
tr {
td {
padding: 10px;
text-align: left;
}
}
}
<div class="wrap">
<table>
<tbody>
<tr>
<td>something here</td>
<td>row 1 col 2 my </td>
</tr>
<tr>
<td>row 2 col 1 hello world</td>
<td>react.js</td>
</tr>
</tbody>
</table>
</div>
I can make the table and the td to be an aligned center but the different length of the td's content makes it look uneven.
.wrap {
background: pink;
padding: 0 50px;
}
table {
margin: 0 auto;
text-align: center;
width: 100%;
tr {
td {
padding: 10px;
}
}
}
<div class="wrap">
<table>
<tbody>
<tr>
<td>something here</td>
<td>row 1 col 2 my </td>
</tr>
<tr>
<td>row 2 col 1 hello world</td>
<td>react.js</td>
</tr>
</tbody>
</table>
</div>
What I want is td's content should be align left and look center within the wrapper, like the labels on this image (i.e. Cam 1, Cam 2 etc):
You can assign width: 50% to <td> to make it of equal widths, like:
tr td {
text-align: left;
width: 50%;
}
.wrap {
background: pink;
padding: 0 50px;
}
table {
width: 100%;
text-align: center;
}
tr td {
text-align: left;
width: 50%;
}
<div class="wrap">
<table>
<tbody>
<tr>
<td>something here</td>
<td>row 1 col 2 my </td>
</tr>
<tr>
<td>row 2 col 1 hello world</td>
<td>react.js</td>
</tr>
</tbody>
</table>
</div>
Hope this helps!
You need to calculate the width of the table using javascript/jquery to have a better control with it. Check this sample.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<style>
table {
margin : 0 auto;
background-color:#DDDDDD;
}
table tr td {
padding : 10px;
text-align:left;
}
</style>
</head>
<body>
<table id="myTable">
<tr>
<td> * Sample content</td>
<td> * Sample content</td>
</tr>
<tr>
<td> * Sample content</td>
<td> * Sample content</td>
</tr>
</table>
<script>
$(document).ready( function() {
$('#myTable').css({
width: $('#myTable').outerWidth() + 'px'
});
});
</script>
</body>
</html>
I think I've worked out what you want... you want the text left-aligned but the column of text to be centered.
You can't do that with just HTML and CSS the way you have it structured, because each column isn't treated as a single unit in order to find the width to centre it.
For a pure HTML & CSS solution, you would have to restructure your table so that it treats the content of each column as a whole. You can display it as a <ul>, <p>, or even another table, e.g.
Working Snippet:
.wrap {
background: pink;
padding: 0 50px;
}
table {
margin: 0 auto;
text-align: center;
width: 100%;
table-layout: fixed;
tr{
td {
padding: 10px;
text-align: center;
width: 50%;
}
}
}
.cellcontainer {
width: auto;
display: inline-block;
text-align: left;
}
td { border: 1px solid #555555; }
<div class="wrap">
<table>
<tbody>
<tr>
<td>
<div class="cellcontainer">
<p>something here</p>
<p>row 2 col 1 hello world</p>
</div>
</td>
<td>
<div class="cellcontainer">
<p>row 1 col 2 my</p>
<p>react.js</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
I've added a border to the table columns so you can see that the left-aligned content is centered within the column.
If changing the structure of your HTML isn't possible, you would need to use something like javascript to achieve this.
Your question is complicated and unclear . But as I understood, you want to center a table not it's td's text. For that you can use margin:auto on your table and remove text-align:center and add width to your table (because full width cannot be centered), then your table will center without any .wrapper.
Example
HTML
<table>
<tr>
<td>Cam 1</td>
<td>Cam 2</td>
</tr>
<tr>
<td>Cam 3</td>
<td>Cam 4</td>
</tr>
</table>
CSS
table {
width: 70%;
margin: auto;
}
table tr td {
border: 1px solid black; /* Added Border, Remove It */
}
Added border to td, so you can check td's text alignment. And also changed td text (Sorry for that).
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.
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;