Style sheet preventing table cellpadding from working - html

I'm writing a web page based on someone else's stylesheet. The stylesheet includes the following:
table {
border-collapse: collapse;
}
th, td {
padding: 0;
}
Now I want to create a table that has a non-zero cell padding. But I am having trouble overriding this stylesheet:
<table cellpadding="10">
<tr>
<td padding="10">
Foo
</td>
...
...
</tr>
</table>
and none of the above works; the cell padding stays a tight zero.
How do I override the stylesheet (short of using a different stylesheet)?

You can do in-line styling:
<td style="padding: 10px">
or assign a class to your table and create a rule for it:
<table class="table">
<tr>
<td>
Foo
</td>
</tr>
</table>
and the CSS for this:
table td {
padding: 10px;
}

try to be more specific in your selector, for example
table td {
padding:10px;
}
The above will override
th, td {
padding: 0;
}
Learn more for CSS Specificity here.

Just inline the necessary styles:
<table style="border-collapse: separate; border-spacing: 10px;">
...
</table>
If you do this, you don't need the padding="10" on the td either. See http://jsbin.com/exovat/edit#html for a working example.
An alternative to inlining the styles is if you have access to your own custom stylesheet that loads after their stylesheet. Then you can set an id on the table like <table id="foo"> and then just override their styles in your custom stylesheet like this:
table#foo {
border-collapse: separate;
border-spacing: 10px;
}
[Note: The border-spacing css property does not work with IE7 or below; if you need those browsers to be supported, you are better off using some hackier method]

You could use inline styles (ie styles declared on the element), inline stylesheets (ie styles declared in the same page but not directly on elements) or external stylesheets. Unfortunately CSS styles override attributes in most cases (I believe attributes such as you are using here are deprecated, meaning in essence use stylesheets instead).

Create a new class for the table and apply it only to the tables you want to have this style.
table {
border-collapse: collapse;
}
th, td {
padding: 0;
}
table.newStyle { padding:10px; }
<table class="newStyle">
<tr>
<td padding="10">
Foo
</td>
...
...
</tr>
</table>

Related

How do I edit HTML styles with CSS? Unknown property name [duplicate]

In an HTML table, the cellpadding and cellspacing can be set like this:
<table cellspacing="1" cellpadding="1">
How can the same be accomplished using CSS?
Basics
For controlling "cellpadding" in CSS, you can simply use padding on table cells. E.g. for 10px of "cellpadding":
td {
padding: 10px;
}
For "cellspacing", you can apply the border-spacing CSS property to your table. E.g. for 10px of "cellspacing":
table {
border-spacing: 10px;
border-collapse: separate;
}
This property will even allow separate horizontal and vertical spacing, something you couldn't do with old-school "cellspacing".
Issues in IE ≤ 7
This will work in almost all popular browsers except for Internet Explorer up through Internet Explorer 7, where you're almost out of luck. I say "almost" because these browsers still support the border-collapse property, which merges the borders of adjoining table cells. If you're trying to eliminate cellspacing (that is, cellspacing="0") then border-collapse:collapse should have the same effect: no space between table cells. This support is buggy, though, as it does not override an existing cellspacing HTML attribute on the table element.
In short: for non-Internet Explorer 5-7 browsers, border-spacing handles you. For Internet Explorer, if your situation is just right (you want 0 cellspacing and your table doesn't have it defined already), you can use border-collapse:collapse.
table {
border-spacing: 0;
border-collapse: collapse;
}
Note: For a great overview of CSS properties that one can apply to tables and for which browsers, see this fantastic Quirksmode page.
Default
The default behavior of the browser is equivalent to:
table {border-collapse: collapse;}
td {padding: 0px;}
Cellpadding
Sets the amount of space between the contents of the cell and the cell wall
table {border-collapse: collapse;}
td {padding: 6px;}
Cellspacing
Controls the space between table cells
table {border-spacing: 2px;}
td {padding: 0px;}
Both
table {border-spacing: 2px;}
td {padding: 6px;}
Both (special)
table {border-spacing: 8px 2px;}
td {padding: 6px;}
Note: If there is border-spacing set, it indicates border-collapse property of the table is separate.
Try it yourself!
Here you can find the old HTML way of achieving this.
table
{
border-collapse: collapse; /* 'cellspacing' equivalent */
}
table td, table th
{
padding: 0; /* 'cellpadding' equivalent */
}
Setting margins on table cells doesn't really have any effect as far as I know. The true CSS equivalent for cellspacing is border-spacing - but it doesn't work in Internet Explorer.
You can use border-collapse: collapse to reliably set cell spacing to 0 as mentioned, but for any other value I think the only cross-browser way is to keep using the cellspacing attribute.
This hack works for Internet Explorer 6 and later, Google Chrome, Firefox, and Opera:
table {
border-collapse: separate;
border-spacing: 10px; /* cellspacing */
*border-collapse: expression('separate', cellSpacing = '10px');
}
table td, table th {
padding: 10px; /* cellpadding */
}
The * declaration is for Internet Explorer 6 and 7, and other browsers will properly ignore it.
expression('separate', cellSpacing = '10px') returns 'separate', but both statements are run, as in JavaScript you can pass more arguments than expected and all of them will be evaluated.
For those who want a non-zero cellspacing value, the following CSS worked for me, but I'm only able to test it in Firefox.
See the Quirksmode link posted elsewhere for compatibility details. It seems it may not work with older Internet Explorer versions.
table {
border-collapse: separate;
border-spacing: 2px;
}
The simple solution to this problem is:
table
{
border: 1px solid #000000;
border-collapse: collapse;
border-spacing: 0px;
}
table td
{
padding: 8px 8px;
}
Also, if you want cellspacing="0", don't forget to add border-collapse: collapse in your table's stylesheet.
Wrap the contents of the cell with a div and you can do anything you want, but you have to wrap every cell in a column to get a uniform effect. For example, to just get wider left & right margins:
So the CSS will be,
div.cellwidener {
margin: 0px 15px 0px 15px;
}
td.tight {
padding: 0px;
}
<table border="0">
<tr>
<td class="tight">
<div class="cellwidener">My content</div>
</td>
</tr>
</table>
Yes, it's a hassle. Yes, it works with Internet Explorer. In fact, I've only tested this with Internet Explorer, because that's all we're allowed to use at work.
This style is for full reset for tables - cellpadding, cellspacing and borders.
I had this style in my reset.css file:
table{
border:0; /* Replace border */
border-spacing: 0px; /* Replace cellspacing */
border-collapse: collapse; /* Patch for Internet Explorer 6 and Internet Explorer 7 */
}
table td{
padding: 0px; /* Replace cellpadding */
}
TBH. For all the fannying around with CSS you might as well just use cellpadding="0" cellspacing="0" since they are not deprecated...
Anyone else suggesting margins on <td>'s obviously has not tried this.
Simply use CSS padding rules with table data:
td {
padding: 20px;
}
And for border spacing:
table {
border-spacing: 1px;
border-collapse: collapse;
}
However, it can create problems in older version of browsers like Internet Explorer because of the diff implementation of the box model.
From what I understand from the W3C classifications is that <table>s are meant for displaying data 'only'.
Based on that I found it a lot easier to create a <div> with the backgrounds and all that and have a table with data floating over it using position: absolute; and background: transparent;...
It works on Chrome, Internet Explorer (6 and later) and Mozilla Firefox (2 and later).
Margins are used (or meant anyways) to create a spacer between container elements, like <table>, <div> and <form>, not <tr>, <td>, <span> or <input>. Using it for anything other than container elements will keep you busy adjusting your website for future browser updates.
CSS:
selector{
padding:0 0 10px 0; // Top left bottom right
}
You can easily set padding inside the table cells using the CSS padding property. It is a valid way to produce the same effect as the table's cellpadding attribute.
table,
th,
td {
border: 1px solid #666;
}
table th,
table td {
padding: 10px;
/* Apply cell padding */
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set Cellpadding in CSS</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>clarkkent#mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker#mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo#mail.com</td>
</tr>
</tbody>
</table>
</body>
</html>
Similarly, you can use the CSS border-spacing property to apply the spacing between adjacent table cell borders like the cellspacing attribute. However, in order to work border-spacing the value of border-collapse property muse be separate, which is default.
table {
border-collapse: separate;
border-spacing: 10px;
/* Apply cell spacing */
}
table,
th,
td {
border: 1px solid #666;
}
table th,
table td {
padding: 5px;
/* Apply cell padding */
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set Cellspacing in CSS</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>clarkkent#mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker#mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo#mail.com</td>
</tr>
</tbody>
</table>
</body>
</html>
Try this:
table {
border-collapse: separate;
border-spacing: 10px;
}
table td, table th {
padding: 10px;
}
Or try this:
table {
border-collapse: collapse;
}
table td, table th {
padding: 10px;
}
I used !important after the border-collapse like
border-collapse: collapse !important;
and it works for me in IE7. It seems to override the cellspacing attribute.
Say that we want to assign a 10px "cellpadding" and a 15px "cellspacing" to our table, in a HTML5-compliant way. I will show here two methods giving really similar outputs.
Two different sets of CSS properties apply to the same HTML markup for the table, but with opposite concepts:
the first one uses the default value for border-collapse (separate) and uses border-spacing to provide the cellspacing,
the second one switches border-collapse to collapse and uses the border property as the cellspacing.
In both cases, the cellpadding is achieved by assigning padding:10px to the tds and, in both cases, the background-color assigned to them is only for the sake of a clearer demo.
First method:
table{border-spacing:15px}
td{background-color:#00eb55;padding:10px;border:0}
<table>
<tr>
<td>Header 1</td><td>Header 2</td>
</tr>
<tr>
<td>1</td><td>2</td>
</tr>
<tr>
<td>3</td><td>4</td>
</tr>
</table>
Second method:
table{border-collapse:collapse}
td{background-color:#00eb55;padding:10px;border:15px solid #fff}
<table>
<tr>
<td>Header 1</td><td>Header 2</td>
</tr>
<tr>
<td>1</td><td>2</td>
</tr>
<tr>
<td>3</td><td>4</td>
</tr>
</table>
td {
padding: npx; /* For cellpadding */
margin: npx; /* For cellspacing */
border-collapse: collapse; /* For showing borders in a better shape. */
}
If margin didn't work, try to set display of tr to block and then margin will work.

Bootstrap's td of the table - class with "border-style: none" doesn't remove top border of td

I use Bootstrap framework.
I have HTML code:
<table class="table">
<tbody>
<tr>
<td class="non-border">First case</td>
</tr>
<tr>
<td style="border-style:none">Second case</td>
</tr>
</tbody>
</table>
And CSS code:
.non-border { border-style: none; color: red }
Why for the first case class "non-border" doesn't remove the top-border of td (for left, right and bottom border it works; CSS works because color changes for red)?
For the second case (style) all borders of td are removed.
What should I do to get working "border-style: none" of td by class?
Thank you in advance.
Use !important. Like so:
.non-border {
border-style: none !important;
color: red;
}
All styles are cascaded, but inline styles are given the highest preference. In your case, class .non-border is getting lower preference than twitter-bootstrap's predefined styles.

How can i hide certain table column in a HTML table using CSS

I have a html table which is as follows
<table class="report">
<tbody>
<tr>
<td >col1</td>
<td >col2</td>
<td>col3</td>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
<tr>
<td>R2</td>
<td >2/17</td>
<td >{2/17}</td>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
</tbody>
</table>
as you can see , after col3 , all subsequent columns are empty.
I want to show only first three columns and hide the rest.
How can i achieve this using CSS.
I dont have control over HTML table so i can not modify it.
Thanks
You can indeed do this:
http://jsfiddle.net/nEQ6g/1/
*EDIT - From my knowledge nth-child and visability: hidden don't play nice together. For what you want, you'd need to use display: none; - based upon the code I've provided you.
CSS:
table{
border-collapse: collapse;
}
table tr td{
padding: 10px;
border: 1px solid #ccc;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
table tr td:nth-child(n+4){
display: none;
}
Use something like this...
CSS
td:nth-child(4), td:nth-child(5), td:nth-child(6), td:nth-child(7)
{
display:none;
}
DEMO
td:nth-child(n+4) {
visibility: hidden
}
http://jsfiddle.net/hZRvx/
I think that that is the most clearest way to do it.
You can use css3 nth child selector, e.g.
td:nth-child(4),td:nth-child(5), ...
{
display:none;
}
jsFiddle here
I've got no idea what's the intent, the reason or the audience but if you append a data attribute to each column cell...
<td data-col="1"></td>
You can then use jQuery to hide all cells that belong to a specific column...
$('td[data-col=1]').hide ();
You could also use the ':nth-child ()' css pseudo class but it is not supported by some IE browsers
Hope it makes sense
If you always want to show the first 3 colums and hide the rest of the colums in the row you might want to use:
tr > td:nth-child(n+4)
{
visibility: hidden;
}
This will start at the index of 4 and will loop through the rest and will hide them.
With visibility: hidden; you will still keep the spacing it had.
jsFiddle
this is a jquery solution. it checks if there is only a whitespace in there... if yes, it hides it.
$('td').each(function(){
if($(this).html() == " ")
$(this).hide();
});

CSS Specificity issue in HTML table

I have a base.css that has the following CSS:
.table thead tr th, table th {
text-align:left;
font-size: 11px;
font-weight: bold;
color: #333;
padding: 4px;
background-color: #EEE;
border: #FFF solid;
border-width: 1px 1px 0 0;
border-left: #e7e7e7;
white-space:nowrap; }
I have included this CSS in my JSP. But I needed different colors than the one in my base.css so I declared something like:
#demo table.header tr {
background:#FDBB30
}
In my JSP. I had faced this issue before and found out about CSS Specificity. The specificity of the first one is 0,0,1,5 and that of the second is 0,1,1,2. According to it, the table should render the second CSS. But it's not. Any suggestions.. ? I cannot remove base.css I need it for other elements.
My table is like:
<table cellpadding="0" cellspacing="0" border="0" align="left" width="100%">
<thead>
<!-- Column Naming for the table -->
<tr>
<th class="header">SName</th>
<th class="header">Report</th>
<th class="header">Owner</th>
<th class="header">STime</th>
<th class="header">SFreq</th>
<th class="header">SDate</th>
<th class="header">EDate</th>
<!-- <th>Action</th> -->
</tr>
</thead>
</table>
Instead of applying custom CSS on tr, apply it on th. Like this:
#demo th.header {
background:#FDBB30
}
Also in your HTML, make sure that you have used id="demo" for either your table or a div containing that table.
This demo on JSFiddle may help.
This selector is wrong
#demo table.header tr
First of all I don't see any demo id declared there, also table.header is incorrect, that means select the table with a class header but since you are applying that class to th the selector will fail.
Call the class on table element
<table class="header" cellpadding="0" cellspacing="0" border="0" align="left" width="100%" >
Also make sure you've a wrapper element, say for example a div with an id of demo
Still not comfortable with above thing, simply use an id on your table like
<table id="change_color" ...>
and than use the selector below
#change_color thead tr {
background: #f00;
}
Demo
Apart from the errors in your CSS selector (see other answers), there is also the simple fact that backgrounds in table cells are drawn "on top of" backgrounds in table rows, so they will always override them, no matter if the specificity on the tr is much higher!
If the HTML is
<table id="table">
<tr id="tr"><th>hello</th></tr>
</table>
and the CSS is
th {background:cyan}
body table#table tr#tr {background:yellow !important}
the background will still be cyan! See fiddle.

Removing unwanted table cell borders with CSS

I have a peculiar and frustrating problem. For the simple markup:
<table>
<thead>
<tr><th>1</th><th>2</th><th>3</th></tr>
</thead>
<tbody>
<tr><td>a</td><td>b></td><td>c</td></tr>
<tr class='odd'><td>x</td><td>y</td><td>z</td></tr>
</tbody>
</table>
I apply different background-color values to the thead, tr, and tr odd elements. The problem is that in most browsers, every cell has an unwanted border which is not the color of any of the table rows. Only in Firefox 3.5 does the table have no borders in any cell.
I'd just like to know how to remove these borders in the other major browsers so that the only thing you see in the table are the alternating row colors.
You need to add this to your CSS:
table { border-collapse:collapse }
to remove the border , juste using css like this :
td {
border-style : hidden!important;
}
Modify your HTML like this:
<table border="0" cellpadding="0" cellspacing="0">
<thead>
<tr><td>1</td><td>2</td><td>3</td></tr>
</thead>
<tbody>
<tr><td>a</td><td>b></td><td>c</td></tr>
<tr class='odd'><td>x</td><td>y</td><td>z</td></tr>
</tbody>
</table>
(I added border="0" cellpadding="0" cellspacing="0")
In CSS, you could do the following:
table {
border-collapse: collapse;
}
Set the cellspacing attribute of the table to 0.
You can also use the CSS style, border-spacing: 0, but only if you don't need to support older versions of IE.
You may also want to add
table td { border:0; }
the above is equivalent to setting cellpadding="0"
it gets rid of the padding automatically added to cells by browsers which may depend on doctype and/or any CSS used to reset default browser styles
After trying the above suggestions, the only thing that worked for me was changing the border attribute to "0" in the following sections of a child theme's style.css (do a "Find" operation to locate each one -- the following are just snippets):
.comment-content table {
border-bottom: 1px solid #ddd;
.comment-content td {
border-top: 1px solid #ddd;
padding: 6px 10px 6px 0;
}
Thus looking like this afterwards:
.comment-content table {
border-bottom: 0;
.comment-content td {
border-top: 0;
padding: 6px 10px 6px 0;
}
Try assigning the style of border: 0px; border-collapse: collapse; to the table element.
sometimes even after clearing borders.
the reason is that you have images inside the td, giving the images display:block solves it.