CSS for specific table rows - html

I have a simple table with several rows. The top rows contains headings, while the rows under it have the id attribute of data. I have been trying to set CSS for the table rows that only have the id attribute data, but have so far only been able to set it for the entire table.
<table width = "80%" align="center" id="example">
<tr>
<td><h3>ID</h3></td>
<td><h3>First Name</h3></td>
<td><h3>Last Name</h3></td>
</tr>
<tr id="data">
<td>1</td>
<td>Joe</td>
<td>Schmoe## Heading ##</h3><td>
</tr>
</table>
and the CSS. I tried changing #example tr to tr #data with no luck. I know that I am overlooking something simple, but I am unsure what.
table#example {
border-collapse: collapse;
}
tr #data{
background-color: #eee;
border-top: 1px solid #fff;
}
tr #data:hover {
background-color: #ccc;
}
tr #data {
background-color: #fff;
}
tr #data th, tr #data td {
padding: 3px 5px;
}
tr #data td:hover {
cursor: pointer;
}
I'm not using classes because I'm not familiar enough with CSS and being able to use id only once is not a limitation for what I need.

You could use a class, but better yet, don't use classes at all (HTML markup is your friend)! Here's a clean solution to your problem:
HTML
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>1</td>
<td>Joe</td>
<td>Schmoe</td>
</tr>
</table>
CSS
th {
font-weight: bold;
/* More styles here */
}
td {
background: #EEE;
border-top: 1px solid #FFF;
}
tr:hover td {
cursor: pointer;
background: #CCC;
}
/* Styles upon styles */
Simply use the th element to specify the table header. To get each table data row to highlight as you mouse over it, simply use the tr:hover td rule to catch that case. See this fiddle for a working example.

Related

HTML/CSS Website price table shows colored column with white lines on devices other than laptop

I'm trying to make a price list on my website. It's just a simple table like the example code below.
On my laptop it all looks good but when I view it on another device all of a sudden there appear thin white lines in the gold coloured column.
I don't know how they appear and also how to get rid of them any suggestions?
HTML
<thead>
<tr>
<th class="lasronth-th">Gelaat</th>
<th class="lasronth-th">Prijs</th>
<th class="lasronth-th">Pakketprijs ( 6 + 2 gratis )</th>
</tr>
</thead>
<tbody>
<tr class="vrouw-gelaat"
data-img="./laserontharing-img/vrouw/gelaat/wenkbrauwen-tussenstuk.png">
<td>Wenkbrauwen tussenstuk</td>
<td>€ 30</td>
<td>€ 180</td>
</tr>
<tr class="vrouw-gelaat" data-img="./laserontharing-img/vrouw/gelaat/bovenlip.png">
<td>Bovenlip</td>
<td>€ 35</td>
<td>€ 210</td>
</tr>
<tr class="vrouw-gelaat" data-img="./laserontharing-img/vrouw/gelaat/kin.png">
<td>Kin</td>
<td>€ 35</td>
<td>€ 210</td>
</tr>
</tbody>
</table>
css :
.table-container{
display: flex;
flex-direction: row;
}
table {
border-collapse: collapse;
}
.laseronth-tbl {
width: 60%;
margin-left: 5%;
}
.laseronth-tbl:not(:first-child) {
margin-bottom: 2%;
}
.laseronth-tbl thead tr th:first-child {
text-align: left;
}
.lasronth-th {
color: #513f34;
}
.laseronth-tbl tbody tr td:not(:first-child) {
text-align: center;
}
.laseronth-tbl tbody tr td:nth-child(even) {
color: #caa463;
width: 200px;
}
.laseronth-tbl tbody tr td,
.laseronth-tbl thead tr th {
padding: 8px 0px;
}
.laseronth-tbl tbody tr td:first-child {
width: 200px;
}
.laseronth-tbl tbody tr td:last-child {
background-color: #d5b26c;
margin-bottom: 0px;
border-spacing: 0px;
color: #fff;
width: 250px;
border:none;
}
.laseronth-tbl tbody tr td {
color: #513f34;
width: 74.4680851%;
}
Try font-size: 0..
Otherwise the only surefire way is either to overlap the last column backgrounds or to create a background that spans all column rows..
I had this problem.. Came to the conclusion that if you want pixel perfect graphical elements across all devices is to find weird ways.. plus less calculations for the browser if it draws one rectangle instead of many.
I found a way that works for my code.
I gave may table a collapsed border and the coloured column I gave a border of 5px in the colour of the column. That seems to do the trick
table{
border-collapse: collapse;
}
td{
border-bottom:5px solid gold
}

Grouping two <TR> together to style as TBODY not styling

For some reason, I have tried styling the <tbody> but it doesn't matter how much of the existing styles I pull out, it doesn't make any difference.
Theoretically, the following should work, but doesn't...
tbody {
border: 1px solid #2696A1;
width: 100%;
border-spacing: 0px;
margin-top: 2px;
margin-bottom: 2px;
page-break-inside: avoid;
border-collapse: collapse;
}
<table>
<tbody>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
</tbody>
</table>
The ultimate goal (unless a better suggestion presents itself) is to wrap the <tbody> with a border around two <tr> so they are visually identifiable as together. The top <tr> is always visible but the second (or more) are not (due to jQuery toggling of the additional information. Normally I would wrap this in a <div> and call it a day but I need the table format from the header of this dynamic table so everything stays in it's appropriate columns and doesn't go drifting off into neverland.
Any ideas or suggestions would be fantastic. :-)
I think the tbody for define boundary as a virtual manner and increase human readability. However instead of styling tbody you can use td tricky way to achieve the goal. Even tr also not a good place to apply styling. You should target td, th etc. Please try following way to apply border:
table {
width: 100%;
border-spacing: 0px;
margin-top: 2px;
margin-bottom: 2px;
page-break-inside: avoid;
border-collapse: collapse;
}
tbody > tr:first-child > td {
border-top: 1px solid #2696A1;
}
tbody > tr > td:first-child {
border-left: 1px solid #2696A1;
}
tbody > tr > td:last-child {
border-right: 1px solid #2696A1;
}
tbody > tr:last-child > td {
border-bottom: 1px solid #2696A1;
}

CSS - Table caption to apply to each tbody

*Note, this question has basically been overhauled from a previous version so as to be more precise. Thus some of the answers below do not completely the restructed question.
I have two sets of data which I need to display tabulated. As both sets of data need to have the column widths (but still be dynamic), I am using two <tbody>'s.
I am trying to set a heading for each of the tabulated data, in a way that the heading takes up the width of the entire <tbody>.
I have tried using table-caption, but it does not apply to the tbody, but the table itself. Meaning all captions look to go to the top of the table, regardless of where they are in the html.
To demonstrate what I am running into, see the following snippet:
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
tbody:before {
display: table-caption;
font-size: 1.25em;
padding: 16px;
background-color: #303030;
font-weight: bold;
color: white;
width: 100%;
}
#tbody1:before {
content: 'tbody1';
}
#tbody2:before {
content: 'tbody2';
}
th,
td {
padding: 4px 0px;
border: 1px solid black;
}
caption {
border: 1px dotted black;
}
<table>
<tbody id="tbody1">
<caption>Caption1</caption>
<tr>
<th>bob</th>
<th>dob</th>
</tr>
</tbody>
<tbody id="tbody2">
<caption>Caption2</caption>
<tr>
<th>dob</th>
<th>bob</th>
</tr>
</tbody>
</table>
My current attempt is to use :before. But as you can see, the :before does not take up the entire width of the tbody. Even with width: 100% it does not work.
Another way I realized it could be done is to have another row for each tbody, and set colspan to equal the amount of columns for that table. Like this:
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
th,
td {
padding: 4px 0px;
border: 1px solid black;
}
caption {
border: 1px dotted black;
}
<table>
<tbody id="tbody1">
<tr>
<th colspan="2">Title1</th>
</tr>
<tr>
<th>bob</th>
<th>dob</th>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<th colspan="2">Title2</th>
</tr>
<tr>
<th>dob</th>
<th>bob</th>
</tr>
</tbody>
</table>
However, the only problem there is that it does not become dynamic and requires you to know how many columns there will be ahead of time. Normally this would not be a problem but I am looking for a more dynamic solution in my case.
My question is: How does one add a caption to a tbody (not the table) in a way so that each caption relates to the applicable tbody and not the table
You just need to set the width to 100vw. This sets the width to 100% of the viewport width. For a more in-depth explanation of viewport width, see this article.
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
#tbody1:before, #tbody2:before {
display: table-caption;
font-size: 1.25em;
padding: 16px;
background-color: #303030;
font-weight: bold;
color: white;
width: 100vw;
}
#tbody1:before {
content: 'tbody1';
}
#tbody2:before {
content: 'tbody2';
}
th, td {
padding: 4px 0px;
border: 1px solid black;
}
<table>
<tbody id="tbody1">
<tr>
<th>bob</th>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<th>dob</th>
</tr>
</tbody>
</table>

HTML Table Border: User Agent style sheet overrides CSS?

I have an HTML table that needs its border color changed. I can't figure it out.
HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Conditions</th>
<th>Characteristics</th>
</tr>
</thead>
<tbody>
<tr class="zebra-stripe">
<td class="first-field">Element</td>
<td>version}</td>
<td></td>
</tr>
<tr>
<td class="first-field">ApplicationTransactionID</td>
<td>string, up to 64 chars, Optional</td>
<td>A number assigned by the calling application to uniquely identify this request.</td>
</tr>
</tbody>
</table>
CSS
/* Global CSS */
table {
border-color: red;
}
tr {
border-color: red;
}
td {
border-color: red;
/* Table CSS */
.zebra-stripe {
border-color: red;
background: #002D38;
border: solid;
border-width: 1px;
}
.zebra-stripe table {
border-color: red;
}
.zebra-stripe tr {
border-color: red;
}
.zebra-stripe td {
border-color: red;
}
I have even tried to change the border color with an inline style. Still no luck! It doesn't work in Chrome or Safari. The border is simply just gray and its from the user agent style sheet. Am I wrongly targeting it? I targeted it like 10 different ways. The class CSS should be enough. I can change the border style or the border width just fine, but I can't change the color. But at the most, targeting the table row should be enough as well.
Cannot fathom what is going wrong.
you haven't defined the rest of the border attributes. You can use:
table {
border: 1px solid red;
}
(which is border-width, border-style and border-color)
instead of
table {
border-color: red;
}
FIDDLE
Also a note about your current CSS, you have .zebra-stripe added as a class to a tr but the CSS states .zebra-stripe table and .zebra-stripe tr which means it's targeting a table or tr inside of a parent with a class of .zebra-stripe
UPDATE
To explain, there were 2 issues, 1 being you forgot to close this
tr {
border-color: red;
}
td {
border-color: red;
<------ //no '}' tag
this prevented .zebra-stripe from working at all. Secondly, .zebra-stripe had the following issue:
.zebra-stripe
border-color: red;
background: #002D38;
border: solid; <------ this should be 'border-style', 'border' is overwriting the others
border-width: 1px;
Wow many careless mistakes on my part. Was pretty flustered, doing a lot of poking around in the dark to see if something would work.
HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Conditions</th>
<th>Characteristics</th>
</tr>
</thead>
<tbody>
<tr class="row-edge">
<td class="first-field">Element</td>
<td>version}</td>
<td></td>
</tr>
<tr>
<td class="first-field">ApplicationTransactionID</td>
<td>string, up to 64 chars, Optional</td>
<td>A number assigned by the calling application to uniquely identify this request.</td>
</tr>
</tbody>
</table>
CSS
.row-edge {
border-top: 1px solid #0A454F;
border-bottom: 1px solid #0A454F;
}
Changed a few colors and labels. But this is just what I needed.
Thanks again!

HTML: How to add a delete link inside a html table, but still visible as outside the table

I have a table in html with some tabular data.
The thing is that I have designed a delete and edit visually link outside the table (on the left side of the table), that will only be visible when the user hovers the table row.
How can I add these links without messing up the default table layout?
The current problem is that I need to create a holding the delete and edit link, and this messes up the table structure when they are not visible.
So, is there a way to add a container to a table (needs to follow the table row structure) that is not a td? Or do you know about some examples doing something similar that I could have a look at?
Thanks
It got a little complicated, but there's a demo at JS Bin to demonstrate my approach. I'm not entirely sure that -webkit-border-radius supports the notation that I used (I tested in Chrome which supports border-radius), so it might be worth checking.
Incidentally, because of the approach I took (mainly to avoid having to manually add classes) to what could be a 'clean' design, there are some near-bleeding-edge CSS selectors, such as tbody tr:nth-child(odd) td:first-child. I think all of this but the :nth-child(odd) pseudo-selector should be understood by IE7+ (with a valid doctype), but I don't have an installation of Windows on which to test my assumption. As this particular rule is only there to overrule the specificity of the earlier selector to add zebra-striping, if neither are understood nothing is broken, it's just a slightly less jazzy table is all.
The CSS is below:
body
{
background-color: #39f;
}
thead tr th,
tbody tr td
{
background-color: #fff;
border-collapse: collapse;
color: #000;
height: 2em;
margin: 0;
padding: 0.5em;
vertical-align: center;
width: auto;
}
tbody tr:nth-child(odd) td
{
background-color: #ffa;
}
th
{
font-weight: bold;
}
tr th:first-child,
tr td:first-child,
tbody tr:nth-child(odd) td:first-child
{
background-color: transparent;
border-radius: 1em 0 0 1em;
color: transparent;
moz-border-radius: 1em 0 0 1em;
padding: 0.5em 0 0.5em 0.5em;
webkit-border-radius: 1em 0 0 1em;
}
tr:hover td:first-child,
tbody tr:nth-child(odd):hover td:first-child
{
background-color: #fff;
color: #000;
}
tbody tr:nth-child(odd):hover td:first-child
{
background-color: #ffa;
color: #000;
}
And the html:
<table cellspacing="0">
<thead>
<tr>
<th></th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
</tr>
</thead>
<tbody>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
</tbody>
</table>
Edited
I've added a side-by-side comparison of the above demonstration and an additional approach, which I think might, in the presence of a valid standards-mode doctype, work reasonably well in older browsers.
The revised demo is here: JS Bin, and can, of course, be edited by clicking on the 'Edit using JS Bin' button.
The relevant CSS can also be seen by hovering over the tables (though it probably works better with larger displays).
Edited
To add in the all finished version, to the best -I think- of my ability, there's two tables (as can be seen at JS Bin (each using slightly different mark-up, and quite different css) to demonstrate at least two ways this can be achieved.
Both tables share this CSS:
body {
background-color: #39f;
}
th {
font-weight: bold;
border-bottom: 2px solid #000;
}
th.title {
border-bottom: 1px solid #000;
}
th.hidden {
border: 0 none transparent;
}
thead tr th,
tbody tr td {
width: auto;
height: 2em;
vertical-align: center;
background-color: #fff;
color: #000;
padding: 0.5em;
margin: 0;
border-collapse: collapse;
}
Next is the 'up-to-date' browsers' CSS:
#preferred thead tr th:first-child {
border: 0 none transparent;
}
#preferred tbody tr:nth-child(odd) td {
background-color: #ffa;
}
#preferred tr th:first-child,
#preferred tr td:first-child,
#preferred tbody tr:nth-child(odd) td:first-child {
color: transparent;
background-color: transparent;
padding: 0.5em 0 0.5em 0.5em;
-webkit-border-top-left-radius: 1em;
-webkit-border-bottom-left-radius: 1em;
-moz-border-radius: 1em 0 0 1em;
border-radius: 1em 0 0 1em;
}
#preferred tr:hover td:first-child,
#preferred tbody tr:nth-child(odd):hover td:first-child {
color: #000;
background-color: #fff;
}
#preferred tbody tr:nth-child(odd):hover td:first-child {
color: #000;
background-color: #ffa;
}
And the relevant html mark-up:
<table cellspacing="0" id="preferred">
<thead>
<tr>
<th></th>
<th class="title" colspan="4">id="preferred"</th>
</tr>
<tr>
<th></th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
</tr>
</thead>
<tbody>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td>X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
</tbody>
</table>
Next is the older browser's CSS:
#ieMaybe {
background-color: #39f;
}
#ieMaybe th,
#ieMaybe td {
background-color: #fff;
}
#ieMaybe th.hidden,
#ieMaybe td.hidden {
color: #39f;
background-color: transparent;
padding: 0.5em 0 0.5em 0.5em;
-webkit-border-top-left-radius: 1em;
-webkit-border-bottom-left-radius: 1em;
-moz-border-radius: 1em 0 0 1em;
border-radius: 1em 0 0 1em;
}
#ieMaybe tr:hover td.hidden,
#ieMaybe tr td.hidden:hover {
color: #000;
background-color: #fff;
}
And the older browsers' html mark-up:
<table cellspacing="0" id="ieMaybe">
<thead>
<tr>
<th class="hidden"></th>
<th class="title" colspan="4">id="ieMaybe"</th>
</tr>
<tr>
<th class="hidden"></th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
<th>visible</th>
</tr>
</thead>
<tbody>
<tr>
<td class="hidden">X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td class="hidden">X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
<tr>
<td class="hidden">X</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
<td>visible</td>
</tr>
</tbody>
</table>
I can't say, for sure, what versions of IE < 8 do in the presence of such chicanery, but IE8 (with a <!DOCTYPE html>) renders it willingly, albeit with no pretence at curved borders. Which is a shame, here's waiting for IE9! =)
As #Yi Jiang noted, in the comments, there were a couple of errors in the first-posted code, those have been left as is (because I'm starting to go CSS-blind), but the code-blocks above have been directly pasted from the latest working JS Bin demo, so unless ctrl+V's been playing up, it should, I hope, be fine.
your could add the links in the first cell and hide them with CSS. position them absolutely and move them to appear as if they are partially outside the table.
you could put all the controls inside a column that has a class "controls" and then play with the css to hide or show... something like this
example
hope this helps