HTML table alternative rows shading with cellpadding - html

When using cellpadding="10" along with alternate row shading, it seems that the backgrounds of the alternative rows don't "expand" with the cellpadding. For example:
As you can see in the figure above, there are gaps between the background and the extra cellpadding. How do I make the background cover the entire row including the cellpadding?
Here's the HTML style behind it:
tr:nth-child(even) {background-color: #f2f2f2;}

They do, but you have to remove border-spacing from table and then adjust the cellpadding and/or the margins and paddings of the td and th elements.
By default tables have a spacing between cells, that spacing is not part of the 'content' of the cell but only of the table. Add a border on table, td and th to better understand it. For more information you better look at the MDN page about border-spacing.
Example without cellpadding:
table {
border-spacing: 0;
}
td, th {
padding: 10px 20px;
}

Related

remove complete border from tables cells using css

I want to remove complete border from table cells as in shown in screen shot, i have to fill color in cells and there is white border are visible. how to make it complete remove.
It is simple table with 24 td tags and 2 tr tags, second tr tag containing div tags having background color red,blue, and gray.
Use this CSS property, this will remove the border:-
table, th, td {
border:none;
border-collapse: collapse;
}

Background color is shown with Mozilla even if visibility is hidden

In my CSS, with Mozilla even if I put visibility: hidden on a <td> tag the background color is shown.
Open this jsfiddle. With Chrome it is okay, but with not with Mozilla.
Actually Firefox handles this situation properly to my mind. You set background color of tr but make invisible td. tr should still be visible. So it is.
Instead try this CSS:
table td,
table th {
background-color: beige;
}
Demo: http://jsfiddle.net/mTTzb/3/
You can use display: none; instead if it matches your need. Keep in mind however that contrary to visibility: hidden;, the place occupied by your content will be freed for next displayed block.
You can also apply the background style on td and th instead of tr:
th,td {
border: 1px black solid;
background-color: beige;
}
See result here:
http://jsfiddle.net/mTTzb/5/
Try changing the visibility:hidden; attribute for display:none;
The problem is in the following CSS rule:
table tr {
background-color: beige;
}
The rule states that every row on any table has a background color.
When you set the cell's visibility to hidden, although the cell remains hidden, it's space remains within the row.
That's why you can see color in the space where the cell is.
You can either set the display:none so the cell space isn't used or you can set the background color on the cells instead of rows:
table tr td,th {
background-color: beige;
}

issue on tr border-bottom in inline style

I am trying to add border-bottom to <tr> element of table as below but it is not working
<table>
<tr style="border-bottom: 1px solid black !important;">
it is working when I add the style to <td> but not on <tr>. Can you please let me know how to fix it?
Thanks
You cannot apply a border on tr element, you need to apply a border on td, or you need to use border-collapse for your table element
Demo (Applying border-bottom to td)
Demo 2 (Applying border-bottom to tr if used border-collapse)
table {
width: 100%;
border-collapse: collapse;
}
table tr { /* Use table tr td if not using border-collapse property */
border-bottom: 1px solid #eee;
}
Note: Avoid using inline styles, you will feel hard to change them
at certain point, also, avoid using !important unless required,
consider using more specific selectors instead

First-child selector within table

I have created a page which is a login portal to three separate websites. On this page, I have displayed the three login portals to said websites. I am trying to style it all with css and as few classes as possible (I need to fine tune my css skills).
What I have created is a table with three rows and two columns called loginPortals:
<table class="loginPortals">
<tbody>
<tr>
<td>Picture 1</td>
<td>Login form 1</td>
</tr>
<tr>
<td>Picture 2</td>
<td>Login form 2</td>
</tr>
<tr>
<td>Picture 3</td>
<td>Login form 2</td>
</tr>
</tbody>
</table>
I've only just begun the styling of the table, so it's very incomplete, however I'm struggling already. This is simplified, because I don't know exactly how it's going to look. Basically though, I want a border of some sort appearing between each row. The following doesn't quite achieve this as I had wanted:
.loginPortals{
width:100%;
}
.loginPortals tbody:first-child td{
border-top:1px solid #000;
}
.loginPortals tbody tr td{
border-bottom:1px solid #000;
padding:1em 0;
}
The first-child selector isn't working as I had thought it would. It is applying the top border to all the cells in all the rows. This is causing the line thickness to double in the middle of the rows.
How do I fix this so that the top border is only applied to the top row without creating extra classes or applying any inline style.
Thank you!
Joe
Your table borders need to be set to collapse:
.loginPortals{
width:100%;
border-collapse: collapse;
}
You can then omit the :first-child rule and just apply top and bottom borders for all your rows:
.loginPortals tbody tr td{
border-top:1px solid #000;
border-bottom:1px solid #000;
padding:1em 0;
}
Also, just for your information, this selector:
.loginPortals tbody:first-child td
Means:
Select any td element
within a tbody
that is the first child of its parent.
And not:
Select any td element
within the first child of a tbody.
So since the tbody is the first and only child of .loginPortals, all the td elements in all their tr parents will be selected. See this answer for a visual explanation. You probably wanted this selector:
.loginPortals tbody tr:first-child td
But that's not necessary because all you have to do is collapse your table borders.
If I understand correctly, you have 3 forms and only want two lines - that's what I took from I want a border of some sort appearing between each row if that's the case, try this.
.loginPortals tr:first-child td{
border-top:none;
}
.loginPortals tbody tr td{
border-top:1px solid #000;
padding:1em 0;
}​
http://jsfiddle.net/u4v3Z/
I believe this would work...
td {border-top:1px solid #000;}
First of all, if you want to improve your CSS skills you should immediately stop using tables for layout.
Second, the :first-child pseudo class affects elements that are the first child of their parents. So since you only have one tbody element and it is the first child of the table writing tbody or tbody:first-child will yield the same results.
Edit: I think what you might be after here is tr:first-child td (you only want the table cells in the first table row to have a top border).
To solve your problem you could just go with border-bottom on all tds, unless you need a border in the top as well in which case you could set that border on the actual table or you could give all the tds a border-top and border-bottom. This of course renders a double border as you describe, but you can make the borders of a table collapse using border-collapse: collapse.
So in short: table {border-collapse: collapse}. But don't use tables for layout.

How do I put a border around a tr tag?

I have a very simple html page:
<table>
<tr><th>header1</th><th>header2</th></tr>
<tr><td>item1</td><td>item2</td></tr>
<tr><td>item3</td><td>item4</td></tr>
</table>
With some simple css:
tr {
border:1px solid blue;
}
I would expect this to put a border around the trs but it doesn't put a border around it at all. How do I get a border around the tr?
Add table { border-collapse: collapse; }.
From the CSS2 specification:
In [the border-collapse: separate model], each cell has an individual border. [...] Rows, columns, row groups, and column groups cannot have borders (i.e., user agents must ignore the border properties for those elements).
Your code works, if you want a border just on the row.
However, if you are looking to have the border everywhere, you will need to do this:
tr, td, th{
border:1px solid blue;
}
Example: http://jsfiddle.net/jasongennaro/83VjH/
Borders can be added to rows of table by adding border to <td> and <th> elements [This is basically a CSS trick to achieve (hack!) that as borders cannot be added to <tr> and <tbody> elements of table]. Add following styles to your CSS to get borders around rows or headers or table cells.
table {
border-collapse: collapse;
}
table td, table th {
border: solid white;
}
td {
border-color: red (just an example, can be as per your requirement);
}
Explanation:
border-collapse rule is added to whole table. It can have two other possible properties separate (default) and inherit. For their respective effects refer https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse
Second rule i.e. adding border property to <td> (for data cells) and <th> (for header cells) is a must. If you don't add it, borders will not show up. In this rule border-color is white, it can be some other color of your choice instead of white. Basically, this rule will activate the borders around table cells and since the color is white nothing will show up.
And finally, add the color of your choice. This rule can be more specific to apply border to one <td> or a class of <td>.