I have the above table shown in this picture. I want to fix the space between the Jill and Smith. Take the last table as an example. Not sure how to explain this because English is not my native language.
http://i.stack.imgur.com/IC4CP.png
EDIT
My code
<table>
<tr>
<th>Jill</td>
<th>Smith</td>
<th>Santa</td>
</tr>
<tr>
<td>Jill</td>
<td>Smith5555</td>
<td>50555555555</td>
</tr>
<tr>
<td>Jillffff</td>
<td>Smith5555</td>
<td>50555555555</td>
</tr>
<tr>
<td>Jill5555</td>
<td>Smith5555</td>
<td>50555555555</td>
</tr>
</table>
CSS
table, th, td {
width:100%;
border:1px solid #ddd;
padding:10px;
}
table {
border-collapse:collapse;
}
table td {
font-size:13px;
}
table th {
text-align:left;
font-size:14px;
text-transform:uppercase;
font-weight:normal;
background:#36c;
color:#fff;
}
tr:nth-child(odd) {
background-color: #f2f2f2;
}
This is where your problem is,
table, th, td {
width:100%; //This makes your table too big
border:1px solid #ddd;
padding:10px;
}
I'd say remove the width attribute and your table should be fine :)
It was your CSS code
I would look at the width attribute. Currently, you have it set to 100%. That means it's going to take up the entire width of the screen. Perhaps, this
<style>table, th, td {
width:auto; //auto will adjust the table width automatically based on the size of each column
border:1px solid #ddd;
padding:10px;
}
table {
border-collapse:collapse;
}
table td {
font-size:13px;
}
table th {
text-align:left;
font-size:14px;
text-transform:uppercase;
font-weight:normal;
background:#36c;
color:#fff;
}
tr:nth-child(odd) {
background-color: #f2f2f2;
}</style>
Just change width in your CSS as mentioned in the code below.
To improve your HTML, <th> elements have been closed as </td>. Please change it to </th>.
table, th, td {
border: 1px solid #ddd;
padding: 10px;
}
table {
width: 100%; /* Make table 100% */
border-collapse: collapse;
}
table th {
width: 33.33%; /* Make column i.e <th> 33.33% */
text-align: left;
font-size: 14px;
text-transform: uppercase;
font-weight: normal;
background: #36c;
color: #fff;
}
Hope it helps!
You will want to add width attribute (this is depreciated in HTML5, forgot) style to your <th> tags.
For example
<tr>
<th style="width:35%">Jill</td>
<th style="width:35%">Smith</td>
<th style="width:30%">Santa</td>
</tr>
In addition you should not have 100% width for th or td. Change your CSS to:
table {
width:100%;
}
table, th, td {
border:1px solid #ddd;
padding:10px;
}
Related
I'm trying to create a table with a fixed first column and colouring alternate rows. I've got the first column fixed, and alternate rows coloured using nth-child. The problem is that the alternate row colours is not applied to the fixed column. See jsfiddle. This is my first attempt at css so I'm sure I'm missing something fairly obvious.
html
<div class="wb-standings-table"><table class="standings">
<tr>
<td class="wb-standings-col1"><b>Player</b></td>
<th>PTS↑</th>
<th>PPR</th>
<th>TPP</th>
<th>SPC</th>
<th>TUG</th>
<th>LOG</th>
<th>CRK</th>
<th>EUC</th>
<th>PKR↓</th>
<th>DRT</th>
<th>PNG</th>
</tr>
<tr>
<td class="wb-standings-col1">Chuck</td>
<td class="wb-gray">9</td>
<td class="wb-gray">15</td>
<td class="wb-gray">24</td>
<td class="wb-gray">-</td>
<td class="wb-gray">2</td>
<td class="wb-gray">-</td>
<td class="wb-gray">3</td>
<td class="wb-gray">3</td>
<td class="wb-green">0</td>
<td class="wb-green">1</td>
<td class="wb-green">0</td>
</tr>
<tr>
<td class="wb-standings-col1">Rico</td>
<td class="wb-gray">4</td>
<td class="wb-gray">10</td>
<td class="wb-gray">14</td>
<td class="wb-gray">-</td>
<td class="wb-gray">0</td>
<td class="wb-gray">3</td>
<td class="wb-gray">-</td>
<td class="wb-gray">0</td>
<td class="wb-green">0</td>
<td class="wb-green">1</td>
<td class="wb-gray">0</td>
</tr>
css
.wb-standings-container {
width:auto;
white-space:nowrap;
background-color:#fff;
border-radius:0px;
margin:0px;
overflow-x:auto;
}
.wb-standings-header {
font-size:1.5em;
margin:10px;
font-weight:bold;
}
.wb-standings-footer {
font-size:.8em;
margin:8px;
}
.wb-standings-table {
overflow-x:auto;
margin-left:100px;
overflow-y:visible;
}
table.standings {
border-collapse:collapse;
}
table.standings th {
white-space:nowrap;
color:#808080;
text-decoration:underline;
}
table.standings td {
white-space:nowrap;
text-align:center;
min-width: 50px;
}
table.standings tr:nth-child(even) {background: #f2f2f2}
table.standings tr:nth-child(odd) {background: #FFF}
table.standings td:nth-child(1) {
border-right: 1px solid #e6e6e6;
box-shadow: 3px 0px 3px -3px rgba(0, 0, 0, 0.4);
}
table.standings td:nth-child(4) {border-right: 1px solid #e6e6e6;}
table.standings tr:nth-child(1) {
border-bottom: 1px solid #e6e6e6;
border-top: 1px solid #e6e6e6;
}
table.standings .wb-standings-col1 {
position:absolute;
width:90px;
left:8px;
top:auto;
text-align:left;
color:#057aff;
}
You can just add a special case to the col1 class like so:
table.standings tr:nth-child(even),
table.standings tr:nth-child(even) .wb-standings-col1 {
background: #f2f2f2
}
table.standings tr:nth-child(odd),
table.standings tr:nth-child(odd) .wb-standings-col1 {
background: #FFF
}
You can add another selector to the same CSS definition by using a comma to separate. That way you can still control both definitions by modifying a single CSS spec.
As to why this is happening.... I'm not really sure. I'm guessing it has something to do with the fact that the .wb-standings-col1 class is positioned absolutely and that messes with the rendering of the table underneath. I've noticed in Chrome's dev tools that that particular cell is set to display: block but the rest of the cells are set to display: table-cell. That may be another reason for it. Someone else will have to give you that answer :)
You made the background color on "tr" tag, but your first "td" stays outside of the "tr". To fix this you should use background on the "td" instead of "tr":
table.standings tr:nth-child(even) td {background: #f2f2f2}
table.standings tr:nth-child(odd) td {background: #FFF}
and change its width to 100px:
table.standings .wb-standings-col1 {
position:absolute;
width:100px;
left:8px;
top:auto;
text-align:left;
color:#057aff;
}
Also I think you should remove the box-shadow of this:
table.standings td:nth-child(1) {
border-right: 1px solid #e6e6e6;
/*box-shadow: 3px 0px 3px -3px rgba(0, 0, 0, 0.4);*/
}
I read a lot of questions about my problem, but none were really effective for my case. I have this style in the upper part of my view (I know, I'll later transfer it to the bootstrap). My goal is to make my table have alternative row shading. I don't particularly care about the colors for now, I just want the functionality.
I run the site on Mozilla and Chrome. I have refreshed the cache.
<style>
table, th, td {
border: 2px solid gray;
border-collapse: collapse;
border-right: none;
background: white;
color: #333333; /*#333333*/
}
table {
border-left: none;
}
th {
text-align: center;
}
tr:nth-child(odd) { background:#eee; }
tr:nth-child(even) { background:#fff; }
</style>
I have a table with the following format:
<div class="tableIndex">
<table id="tableBe">
<tr>
<th></th>
</tr>
#if (Model.Count() == 0)
{
<tr>
<td colspan="25" , align="left" style="border-left:none !important">
<b>No issues match search criteria</b>
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td></td>
</tr>
}
}
</table>
</div>
Everything works, except for the nth-child styling. Please assist. Sorry for the code wall, I just need to point out that I have an if construction there.
When I try to inspect element, I get no reference to the tr:nth-child commands, although I get the other effects in the <style>. Please assist. Thanks in advance!
it is because you have background: white; on td
move background: white; from table,th,td { and put it under table {
Your code is applying color to your tr based on your condition. But it got overwrite with the styles you have applied for th and td background color. So change your style like below.
tr:nth-child(odd) td{ background:#eee; }
tr:nth-child(even) td { background:#fff; }
DEMO
You have set td's background earlier. So set td's background instead of tr. Following should work
tr:nth-child(odd) td { background:#eee; }
tr:nth-child(even) td { background:#fff; }
Full Code
table, th, td {
border: 2px solid gray;
border-collapse: collapse;
border-right: none;
background: white;
color: #333333; /*#333333*/
}
table {
border-left: none;
}
th {
text-align: center;
}
tr:nth-child(odd) td {
background: #eee;
}
tr:nth-child(even) td {
background: #fff;
}
<table>
<tr><td>111111111</td><td>2222222222</td></tr>
<tr><td>111111111</td><td>2222222222</td></tr>
<tr><td>111111111</td><td>2222222222</td></tr>
<tr><td>111111111</td><td>2222222222</td></tr>
</table>
I have a simple html table like this:
<table>
<thead>
<tr><th>Column 1</th><th>Column 2</th></tr>
</thead>
<tbody>
<tr class="odd first-row"><td>Value 1</td><td>Value 2</td></tr>
<tr class="even"><td>Value 3</td><td>Value 4</td></tr>
<tr class="odd"><td>Value 5</td><td>Value 6</td></tr>
<tr class="even last-row"><td>Value 7</td><td>Value 8</td></tr>
</tbody>
</table>
And I would like to style it the following way:
header row with a box-shadow
whitespace between the header row and the first body row
I have tried different things:
table {
/* collapsed, because the bottom shadow on thead tr is hidden otherwise */
border-collapse: collapse;
}
/* Shadow on the header row*/
thead tr { box-shadow: 0 1px 10px #000000; }
/* Background colors defined on table cells */
th { background-color: #ccc; }
tr.even td { background-color: yellow; }
tr.odd td { background-color: orange; }
/* I would like spacing between thead tr and tr.first-row */
tr.first-row {
/* This doesn't work because of border-collapse */
/*border-top: 2em solid white;*/
}
tr.first-row td {
/* This doesn't work because of border-collapse */
/*border-top: 2em solid white;*/
/* This doesn't work because of the td background-color */
/*padding-top: 2em;*/
/* Margin is not a valid property on table cells */
/*margin-top: 2em;*/
}
See also: http://labcss.net/#8AVUF
Does anyone have any tips on how I could do this? Or achieve the same visual effect (i.e. bod-shadow + spacing)?
I think I have it in this fiddle and I updated yours:
tbody:before {
content: "-";
display: block;
line-height: 1em;
color: transparent;
}
EDIT better & simpler:
tbody:before {
content:"#";
display:block;
line-height:10px;
text-indent:-99999px;
}
This way text is really invisible
Moreover you can use Zero-Width Non-Joiner to minimize sinsedrix CSS:
tbody:before {line-height:1em; content:"\200C"; display:block;}
This will give you some white space between the header and table content
thead tr {
border-bottom: 10px solid white;
}
Although setting the border colour is a bit of a cheat method, it will work fine.
Form investigation, you can't set box-shadow to a table row, but you can to table cells:
th {
box-shadow: 5px 5px 5px 0px #000000 ;
}
(I'm not sure how you want the shadow to look like, so just adjust the above.)
This worked for me on Chrome (for other browsers I don't know).
.theTargethead::after
{
content: "";
display: block;
height: 1.5em;
width: 100%;
background: white;
}
Such css code creates an empty white space between the thead and the tbody of the table.
If I set the background to transparent, the first column of the above tr > th elements shows its own color (green in my case) making about the first 1 cm of the ::after element green too.
Also using the "-" sign in the row content : "-"; instead of the empty string "" can create problems when exporting the printed pages to file, i.e. pdf. Of course this is parser/exporter dependent.
Such exported file opened with a pdf editor (for ex.: Ms word, Ms Excel, OpenOffice, LibreOffice, Adobe Acrobat Pro) could still contain the minus sign. The empty string doesn't have the same issue.
No problems in both cases if the printed html table is exported as image: nothing is rendered.
I didn't notice any issue even using
content : "\200C";
So box-shadow doesn't work well on the tr element... but it does work on a pseudo content element; sinsedrix put me on the right track and this is what I ended up with:
table {
position: relative;
}
td,th {padding: .5em 1em;}
tr.even td { background-color: yellow; }
tr.odd td { background-color: orange; }
thead th:first-child:before {
content: "-";
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: -1;
box-shadow: 0 1px 10px #000000;
padding: .75em 0;
background-color: #ccc;
color: #ccc;
}
thead th {
padding-bottom: 2em;
}
While all the solutions above are great, the result is inconsistent across browsers, so I figured out a better way to do it based on my heinous experience with email templates.
Just add a dummy tbody in-between the actual tbody and the thead, nested in the dummy tbody should be a td with height set to the desired spacing. Example below
<table>
<thead>
<tr>
<td>
</td>
</tr>
</thead>
// Dummy tbody
<tbody>
<tr>
<td class="h-5"></td>
</tr>
</tbody>
// Actual tbody
<tbody class="rounded shadow-outline">
<tr v-for="(tableRow, i) in tableBody" :key="`tableRow-${i}`">
<td v-for="tableRowItem in tableRow" :key="tableRowItem" class="table-body">
{{ tableRowItem }}
</td>
</tr>
</tbody>
</table>
This should do the trick:
table {
position: relative;
}
thead th {
// your box shadow here
}
tbody td {
position: relative;
top: 2rem; // or whatever space you want between the thead th and tbody td
}
And this should play nice with most browsers.
No sure why the borders are not displaying correctly. I tried:
/* ------ global ------ */
body {
margin: 0 auto;
padding:0 0;
font-family:Arial, Helvetica, sans-serif;
text-align:center;
color:#000;
}
/* ------ Content Wrapper ------ */
#wrapper {
margin: 0 auto;
width:760px;
text-align:left;
}
#content table {
font-size:.8em;
border-collapse:collapse;
text-align:left;
width:100%;
}
#content table td {
border:solid 1px black;
}
Do I need to list all the CSS border properties to get the borders on the whole table, like this:
border-top: 1px solid #000;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
border-left: 1px solid #000;
or more ......
I haven't done this yet, but I have had to do this in the past with some tables to get the borders on all sides for lte IE7. It was just as a last resort since I didn't know what else to do.
Consider the following jsFiddle, which works correctly in IE7 by showing a 1 pixel solid black border on table cells.
I didn't change any of your code, but added a rule to include borders on table header cells table th as well as table data cells table td.
HTML:
<div id="content">
<table>
<tr>
<th scope="col">Column</th>
<th scope="col">Column</th>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
</div>
CSS:
#content table {
font-size: 0.8em;
border-collapse: collapse;
text-align: left;
width: 100%;
}
#content table th,
#content table td {
border: solid 1px black;
padding: 5px 10px;
}
If your table cells still aren't showing any borders, you might have one or more rules in your stylesheet that appear later — or have more CSS Specificity — that are overriding your styles.
Try using border-collapse:separate for IE7 like this:
#content table {
font-size:.8em;
border-collapse:collapse;
text-align:left;
width:100%;
*border-collapse:separate;
}
apply border-collapse: collapse to the table, it should fix it :)
I want to use CSS to set a color of the outer border of the table ...
Then the inner cells would have different border color ...
I created something like this :
table {
border-collapse:collapse;
border: 1px solid black;
}
table td {
border: 1px solid red;
}
Problem is, the table's color change and become red as you can see here : http://jsfiddle.net/JaF5h/
If the border width of the table is increased to be 2px it will work : http://jsfiddle.net/rYCrp/
I've been dealing with CSS and cross browsers issues for so long ... This is the first time I face something like that and I am totally stuck ... No idea what to do!
Any one knows how to get that fixed with border-width:1px ?
I would acheive this by using adjacent selectors, like so:
table {
border: 1px solid #000;
}
tr {
border-top: 1px solid #000;
}
tr + tr {
border-top: 1px solid red;
}
td {
border-left: 1px solid #000;
}
td + td {
border-left: 1px solid red;
}
It's a little bit repetitive, but it acheives the effect you're after by setting the top and left borders of the first row and column respectively, then overwriting the 'internal' rows and cells with red.
This won't of course work in IE6 as it doesn't understand the adjacent selectors.
http://jsfiddle.net/JaF5h/36/
Try this:
tbody { display:block; margin: -1px; }
The previous answers didn't fully resolve this for me. The accepted answer allows the internal borders to overlap the outer table border. After some experimentation I came up with the following solution.
By setting the table collapse style to separate the internal borders do not overlap the outer. From there the extra and doubled borders are eliminated.
HTML:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
CSS
table {
border: 1px solid black;
border-collapse: separate;
border-spacing: 0;
}
table td, table th {
border: 1px solid red;
}
table tr td {
border-right: 0;
}
table tr:last-child td {
border-bottom: 0;
}
table tr td:first-child,
table tr th:first-child {
border-left: 0;
}
table tr td{
border-top: 0;
}
https://jsfiddle.net/o5ar81xg/
Create a div surrounding your table. Set the div border color for the outside of your table. DO NOT border-collapse your table. Instead, let your cells separate to show the (inner borders) background color of the div beneath. Then set the background cells to the background color of your choice.
HTML:
<div id="tableDiv">
<table id="studentInformationTable">
<!-- Add your rows, headers, and cells here -->
</table>
</div>
CSS:
#tableDiv {
margin-left: 40px;
margin-right: 40px;
border: 2px solid brown;
background-color: white;
}
table {
position: relative;
width: 100%;
margin-left: auto;
margin-right: auto;
border-color: brown;
}
td, th {
background-color: #e7e1d3;
padding: 10px 25px 10px 25px;
margin: 0px;
}
Try the following it worked for me:
table {
border-collapse: collapse;
border: solid #000;
}
table td {
border: 1px solid red;
}