I have a table and when I set the table to a width of 100% and the table rows to a width pf 100% nothing happens or changes to the width.
.Table-Normal {
position: relative;
display: block;
margin: 10px auto;
padding: 0;
width: 100%;
height: auto;
border-collapse: collapse;
text-align: center;
}
.Table-Normal thead tr {
background-color: #E74C3C;
font-weight: bold;
}
.Table-Normal tr {
margin: 0;
padding: 0;
border: 0;
border: 1px solid #999;
width: 100%;
}
.Table-Normal tr td {
margin: 0;
padding: 4px 8px;
border: 0;
border: 1px solid #999;
}
.Table-Normal tbody tr:nth-child(2) {
background-color: #EEE;
}
<table id="top-leader" class="Table-Normal">
<thead>
<tr>
<td>Position</td>
<td>Name</td>
<td>Kills</td>
<td>Deaths</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>16 Kills</td>
<td>0 Deaths</td>
</tr>
<tr>
<td>2</td>
<td>John Smith</td>
<td>13 Kils</td>
<td>1 Death</td>
</tr>
<tr>
<td>3</td>
<td>Bob Smith</td>
<td>11 Kills</td>
<td>0 Deaths</td>
</tr>
</tbody>
</table>
Remove display: block in .Table-Normal
Fiddle
.Table-Normal {
position: relative;
//display: block;
margin: 10px auto;
padding: 0;
width: 100%;
height: auto;
border-collapse: collapse;
text-align: center;
}
By specifying display: block you've overriding the default value from the browser stylesheet for the table element: display: table. Removing display: block or replacing it with display: table fixes this.
As people have mentioned you have to remove display:block; for this to work. If you need to keep scrolling functionality wrap the table inside a div and set overflow rules on that
<div class = "table-container">
<table>...</table>
</div>
.table-container{
height:100px
width:100px
overflow-x:scroll;
table{
display: table;
width:100%;
}
}
Related
Is it possible to set the height of rows in the table to be as the height of the first column in the row if it's position is absolute?
It of course works if I do not use 'position:absolute' (or 'position:fixed') but I need first and last columns to be fixed.
In the example you can see that first column's rows are overlapping. I would like the first and second rows to fit the text inside completely.
http://jsfiddle.net/ujwz4htg/27/
table {
border-collapse: separate;
border-top: 1px solid grey;
}
td {
height: auto;
margin: 0;
border: 1px solid grey;
border-top-width: 0px;
}
td:first-child {
white-space: intial;
position: absolute;
width: 80px;
left: 0;
top: auto;
border-top-width: 1px;
margin-top: -1px;
overflow: hidden;
}
td:last-child {
white-space: initial;
position: absolute;
width: 80px;
right: 160px;
top: auto;
overflow: hidden;
background: grey;
}
div {
width: 600px;
overflow-x: scroll;
padding-left: 5em;
overflow-y: visible;
padding-bottom: 1px;
}
.long {
background: pink;
letter-spacing: 1em;
}
<div>
<table>
<tr>
<td>Row Row Row Row Row</td>
<td class="long">QWERTYUIOPASDsdasdFGHJKLZXCVBsdfsdfNM</td>
<td>LAST</td>
</tr>
<tr>
<td>1 222222 333333333</td>
<td class="long">QWERTYUIOPASDsdasdFGHJKLZXCVBsdfsdfNM</td>
<td>LAST</td>
</tr>
<tr>
<td>3</td>
<td class="long">QWERTYUIOPASDsdasdFGHJKLZXCVBsdfsdfNM</td>
<td>LAST</td>
</tr>
<tr>
<td>4</td>
<td class="long">QWERTYUIOPASDsdasdFGHJKLZXCVBsdfsdfNM</td>
<td>LAST</td>
</tr>
<tr>
<td>5</td>
<td class="long">QWERTYUIOPASDsdasdFGHJKLZXCVBsdfsdfNM</td>
<td>LAST</td>
</tr>
</table>
</div>
I have a table with 3 columns.
Inside every column there is a table with 3 lines.
I want to make this second table like this:
first row is 80 px height
second row is 40 px height and has a black background.
third row has variable height
This table must be vertically aligned to top. It is not. It is vertically aligned to center. Meaning the internal table is at the vertical center of the cell of the outside table.
So, this is the code:
.tabelaTresLinhasInterna {
border: 0px;
table-layout: fixed;
font-size: 0.9em;
line-height: 1.5em;
text-align: center;
margin:0 auto auto auto;
}
.tabelaTresLinhasInterna tr {
padding: 0px;
margin: 0px;
word-wrap: break-word;
}
.tabelaTresLinhasInterna tr:nth-child(1) {
height:80px;
}
.tabelaTresLinhasInterna tr:nth-child(2) {
background-color: #000;
height:40px;
}
.tabelaTresLinhasInterna tr:nth-child(3) {
height:auto;
}
.tabelaTripla {
border: 0px;
table-layout: fixed;
width: 100%;
border-collapse: separate;
border-spacing: 20px;
font-size: 0.7em;
line-height: 1.3em;
background-color: #fafafa;
}
.tabelaTripla tr {
padding: 40px;
margin: 40px;
}
.tabelaTripla th, td{
border: 0px;
width: 33%;
}
.tabelaTripla td{
border: 0px;
width: 33%;
text-align: center;
vertical-align: middle;
word-wrap: break-word;
padding: 40px;
border: 1px solid #eaeaea;
border-radius: 20px;
background-color: #fdfdfd;
}
<table class="tabelaTripla">
<tr>
<td>
<table class="tabelaTresLinhasInterna">
<tr><td>INTERNAL TABLE LINE 1</td></tr>
<tr><td>INTERNAL TABLE LINE 2</td></tr>
<tr><td>INTERNAL TABLE LINE 3</td></tr>
</table>
</td>
<td>COLUMN 2</td>
<td>COLUMN 3</td>
</tr>
</table>
The internal table is ignoring the CSS...
Add vertical-align: top; to the outer table's td.
For the row heights, apply the height to the td instead of the tr, i.e. .tabelaTresLinhasInterna tr:nth-child(1) td { height:80px; }.
.tabelaTresLinhasInterna {
border: 0px;
table-layout: fixed;
font-size: 0.9em;
line-height: 1.5em;
text-align: center;
margin: 0 auto auto auto;
}
.tabelaTresLinhasInterna tr {
padding: 0px;
margin: 0px;
word-wrap: break-word;
}
.tabelaTresLinhasInterna tr:nth-child(1) td {
height: 80px;
}
.tabelaTresLinhasInterna tr:nth-child(2) td {
background-color: #000;
height: 40px;
}
.tabelaTripla {
border: 0px;
table-layout: fixed;
width: 100%;
border-collapse: separate;
border-spacing: 20px;
font-size: 0.7em;
line-height: 1.3em;
background-color: #fafafa;
}
.tabelaTripla tr {
padding: 40px;
margin: 40px;
}
.tabelaTripla th,
td {
border: 0px;
width: 33%;
}
.tabelaTripla td {
border: 0px;
width: 33%;
text-align: center;
vertical-align: top;
word-wrap: break-word;
padding: 40px;
border: 1px solid #eaeaea;
border-radius: 20px;
background-color: #fdfdfd;
}
<table class="tabelaTripla">
<tr>
<td>
<table class="tabelaTresLinhasInterna">
<tr>
<td>INTERNAL TABLE LINE 1</td>
</tr>
<tr>
<td>INTERNAL TABLE LINE 2</td>
</tr>
<tr>
<td>INTERNAL TABLE LINE 3</td>
</tr>
</table>
</td>
<td>COLUMN 2</td>
<td>COLUMN 3</td>
</tr>
</table>
The thing is that your styles for .tabelaTripla td are inherited by tds of the internal table.
So that:
minimal height of the first row is 80px, but inner content makes it bigger because of inherited padding: 40px
same reason for height. Background color applies but covers with background-color: #fdfdfd; from the rules mentioned above
it's all right here
same problem, vertical-align: middle; inherited from top-level td
Try to use more concrete and specific rules.
padding is forcing the height of the cells... yo have to change the td display property in something like "display:block" but other modifications are needed.
By setting the padding of the inner table to zero, the height of the cell dosn't have a "minimum height"
.tabelaTripla .tabelaTresLinhasInterna td {
padding:0px
}
My advice is to use a grid system and you will get this thing in a responsive way and especially with less effort.
.tabelaTresLinhasInterna {
border: 0px;
table-layout: fixed;
font-size: 0.9em;
line-height: 1.5em;
text-align: center;
margin:0 auto auto auto;
}
.tabelaTripla .tabelaTresLinhasInterna td {
padding:0px
}
.tabelaTresLinhasInterna tr {
padding: 0px;
margin: 0px;
word-wrap: break-word;
box-sizing:border-box;
}
.tabelaTresLinhasInterna tr:nth-child(1) {
height:80px;
}
.tabelaTresLinhasInterna tr:nth-child(2) {
background-color: #000;
height:40px;
}
.tabelaTresLinhasInterna tr:nth-child(3) {
height:auto;
}
.tabelaTripla {
border: 0px;
table-layout: fixed;
width: 100%;
border-collapse: separate;
border-spacing: 20px;
font-size: 0.7em;
line-height: 1.3em;
background-color: #fafafa;
}
.tabelaTripla tr {
padding: 40px;
margin: 40px;
}
.tabelaTripla th, td{
border: 0px;
width: 33%;
}
.tabelaTripla td{
border: 0px;
width: 33%;
text-align: center;
vertical-align: middle;
word-wrap: break-word;
padding: 40px;
border: 1px solid #eaeaea;
border-radius: 20px;
background-color: #fdfdfd;
}
<table class="tabelaTripla">
<tr>
<td>
<table class="tabelaTresLinhasInterna">
<tr><td>INTERNAL TABLE LINE 1</td></tr>
<tr><td>INTERNAL TABLE LINE 2</td></tr>
<tr><td>INTERNAL TABLE LINE 3</td></tr>
</table>
</td>
<td>COLUMN 2</td>
<td>COLUMN 3</td>
</tr>
</table>
I using position fixed to freeze first column in table. How to make it the first column layout(width and height) same as other column to looks like no different with other columns but with freeze status.
<div>
<table>
<tr>
<th class='td1'>h1</th>
<th>h2</th>
<th>h3</th>
</tr>
<tr>
<td class='td1'>D1</td>
<td>D2</td>
<td>D3</td>
</tr>
<tr>
<td class='td1'>D1</td>
<td>D2</td>
<td>D3</td>
</tr>
</table>
</div>
css:
table {
border-collapse: collapse;
border: 1px solid black;
width: 300px;
height: 300px;
}
tr,
td,
th {
border: 1px solid black;
text-align: center;
}
.td1 {
position: fixed;
margin-top: 10px;
margin-left: 10px;
}
div {
width: 200px;
height: 400px;
overflow: auto;
}
JSfiddle demo
Try the below -
table {
border-collapse: separate;
border-spacing: 0;
border-top: 1px solid grey;
width: 300px;
height: 100px;
}
td,
th {
margin: 0;
border: 1px solid grey;
white-space: nowrap;
border-top-width: 0px;
}
div {
width: 200px;
overflow-x: scroll;
margin-left: 5em;
overflow-y: visible;
padding: 0;
}
.headcol {
position: absolute;
width: 5em;
left: 0;
top: auto;
border-top-width: 1px;
margin-top: -1px;
}
.headcol:before {
content: 'Row ';
}
.long {
background: yellow;
}
<div>
<table>
<tr>
<th class="headcol">h1</th>
<td class="long">h2</td>
<td class="long">h3</td>
</tr>
<tr>
<th class="headcol">D1</th>
<td class="long">D2</td>
<td class="long">D3</td>
</tr>
<tr>
<th class="headcol">D1</th>
<td class="long">D2</td>
<td class="long">D3</td>
</tr>
</table>
</div>
Do you mean like this?
CSS:
table {
border-collapse: collapse;
border: 1px solid black;
width: 300px;
height: 300px;
}
tr,
td,
th {
border: 1px solid black;
text-align: center;
}
.td1 {
position: fixed;
width: 143px;
height: 96px;
margin-left: 5px;
}
div {
width: 200px;
height: 400px;
overflow: auto;
}
HTML:
<div>
<table>
<tr>
<th class='td1'>h1</th>
<th>h2</th>
<th>h3</th>
</tr>
<tr>
<td class='td1'>D1</td>
<td>D2</td>
<td>D3</td>
</tr>
<tr>
<td class='td1'>D1</td>
<td>D2</td>
<td>D3</td>
</tr>
</table>
</div>
I have a table that needs to look like the attached image.
Notice the border under Name that does not extend the full length like the row highlight does. What I can't figure out is how to get the border to be short and the row background to extend to the edge of the containing div. Currently I'm applying padding to the first and last <td> cells to get the padding. My initial attempt was to apply the padding to the <tr> and apply the border to the <th>'s in the table head but it seems <tr>'s do not take padding even with border-collapse: collapse; set.
Here is an attached jsfiddle of the problem. The red border needs to be aligned with the td content.
https://jsfiddle.net/0vhqg4xe
Any ideas would be appreciated.
You can add a <span> tag around each text in <th>, and apply the border to it.
<th><span>Test 1</span></th>
<th><span>Test 2</span></th>
thead th span {
display: block;
border-bottom: 1px solid red;
}
.wrapper {
background: blue;
color: white;
padding-top: 10px;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
text-align: left;
border: 0;
}
tr {
border: 0;
}
thead th span {
display: block;
border-bottom: 1px solid red;
}
tr.highlight {
background: green;
}
tr > td:first-child,
th:first-child {
padding-left: 20px;
}
tr > td:last-child,
th:last-child {
padding-right: 20px;
}
<div class="wrapper">
<table>
<thead>
<tr>
<th><span>Test 1</span>
</th>
<th><span>Test 2</span>
</th>
</tr>
</thead>
<tbody>
<tr class="highlight">
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
</tbody>
</table>
</div>
Or, use a pseudo element for the border, so you won't need to change the HTML.
thead tr th {
position: relative;
}
thead tr th:before {
content: "";
display: block;
height: 1px;
overflow: hidden;
background: red;
position: absolute;
left: 20px;
right: 0;
bottom: 0;
}
thead tr th:last-child:before {
left: 0;
right: 20px;
}
.wrapper {
background: blue;
color: white;
padding-top: 10px;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
text-align: left;
border: 0;
}
tr {
border: 0;
}
thead tr th {
position: relative;
}
thead tr th:before {
content: "";
display: block;
height: 1px;
overflow: hidden;
background: red;
position: absolute;
left: 20px;
right: 0;
bottom: 0;
}
thead tr th:last-child:before {
left: 0;
right: 20px;
}
tr.highlight {
background: green;
}
tr > td:first-child,
th:first-child {
padding-left: 20px;
}
tr > td:last-child,
th:last-child {
padding-right: 20px;
}
<div class="wrapper">
<table>
<thead>
<tr>
<th>Test 1</th>
<th>Test 2</th>
</tr>
</thead>
<tbody>
<tr class="highlight">
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
</tbody>
</table>
</div>
I've recently solved this problem, incase anybody comes across this post in the future, here it is.
Here is an image of the result ...
... here is the code ...
<style>
* {
font-family: sans-serif;
}
/* table wrapper for continuous border */
.table {
width: 500px;
border: solid 1px rgb(221,221,221);
border-radius: 4px;
overflow: hidden;
text-align: left;
}
/* table border */
.table table {
width: 100%;
border-collapse: collapse; /* removes gap between cells */
}
.table thead th {
font-weight: bold;
background-color: rgb(245,245,245);
border-bottom: solid 1px rgb(221, 221, 221);
}
/* cell padding */
.table th, td {
padding: 10px;
}
/* add row hover */
.table tr:hover td {
background-color: rgb(245,245,245);
}
/* create 1px gap in table for line */
.table tr.line-break td {
position: relative;
top: 1px;
}
/* create the line */
.table tr.line-break td:after {
content: '';
position: absolute;
top: -1px;
left: 0px;
height: 1px;
width: 100%;
background-color: rgb(235,235,235);
}
/* reduce width of line for first and last cells, by cell padding amount */
.table tr.line-break td:first-child:after,
.table tr.line-break td:last-child:after {
width: calc(100% - 10px);
}
/* pull line on first cell to the right */
.table tr.line-break td:first-child:after {
right: 0px;
left: auto;
}
</style>
<div class="table">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Smith</td>
<td>Male</td>
<td>35</td>
</tr>
<tr class="line-break">
<td>Steve</td>
<td>Cook</td>
<td>Male</td>
<td>27</td>
</tr>
<tr>
<td>Susan</td>
<td>Walters</td>
<td>Female</td>
<td>34</td>
</tr>
</tbody>
</table>
</div>
... and here is a link of a CodePen I made for the working solution.
https://codepen.io/latchy/pen/wvwoxXe
The reason the table is wrapped inside a DIV is so that when I push the table row down 1px to allow for the height of the line, the border on the sides is not broken. It also makes it easy to apply a border radius to the table.
Hope this helps someone!
-- Latchy
.border-table .bordered {
position: relative;
background: red;
}
.border-table .bordered:before,
.border-table .bordered:after {
width: 90%;
left: 5%;
height: 1px;
background: blue;
content: '';
position: absolute;
}
.border-table .bordered:before {
top: 0;
}
.border-table .bordered:after {
bottom: 0;
}
<div class="wrapper border-table">
<table>
<thead>
<tr>
<th class="bordered">Test 1</th>
<th class="bordered">Test 2</th>
</tr>
</thead>
<tbody>
<tr class="highlight">
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
<tr>
<td>Some Text</td>
<td>Some Text</td>
</tr>
</tbody>
</table>
</div>
Just add an ::after pseudo element to the tr's and play around with the values (preferably in percentages) so you put the "simulated border" in the desired place.
The following code is just an example of what I've added on the image above (notice that last row can be excluded of having border with :last-of-type):
tbody {
tr {
position: relative;
&::after {
content: '';
position: absolute;
width: 95%;
height: 0.5px;
background-color: #d2d4e1;
left: 2.5%;
bottom: 0;
}
&:last-of-type {
&::after {
content: none;
}
}
}
}
I'm trying to make it so that images in cells in an html table expand when you hover over them. I want this to be universal to all tables on my website. Here's my code so far:
td img {
height: 150px;
}
td img:hover{
height: 175px;
}
This code makes the images appear at their correct height, but nothing hapens when I hover over them. Obviously I'm doing something wrong, but what is it?
Consider defining the width and height of the cells and adjust the image within those parameters. This way the rows don't shift around when you hover -- change table image size on hover
html {
padding: 50px;
}
table {
width: 600px;
padding: 10px;
background: #FFF;
border: 1px solid #BBB;
}
th {
padding: 10px;
}
td {
vertical-align: middle;
text-align: center;
padding: 5px;
border: 1px solid #DDD;
}
td:first-child {
width: 50px;
}
td:last-child {
width: 200px;
height: 200px;
}
td img {
width: 75%;
height: auto;
}
td img:hover{
width: 90%;
height: auto;
}
<table>
<thead>
<th>Image Title</th>
<th>Image</th>
</thead>
<tbody>
<tr>
<td>Image 1</td>
<td><img src="http://www.logoeps.net/wp-content/uploads/2013/06/stackoverflow_logo.jpg" /></td>
</tr>
</tbody>
</table>
you can do it with CSS
td:hover img{
height:175px;
}
you can use jQuery for this also :
$('td img').on('mouseenter',function(){
$(this).css({
'height':'175px'
});
});
* {
margin: 0;
padding: 0;
}
img {
height: 150px;
width: auto
}
td:hover img {
background: red;
height: 200px;
}
<table>
<tr>
<th>Title</th>
<th>Image</th>
</tr>
<tr>
<td>Image Title</td>
<td><img src="https://torange.biz/photofx/5/8/image-profile-picture-beautiful-exotic-flower-5532.jpg" alt=""></td>
</tr>
</table>