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.
I have a webpage with code I cannot change which is set up something like this:
<table style="width: 750px; margin-left: auto; margin-right: auto">
<tr>
<td>
<table>
<tr><td><div id=1></td></tr>
<tr><td><div id=2></td></tr>
<tr><td><div id=3></td></tr>
<tr><td><div id=4></td></tr>
</table>
</td>
</tr>
</table>
I'd like to add an element (via userscript) to the right of the outer table which lines up with div 1, 2, 3, or 4. Is there a way to do this? Maybe measure the current position of the elements then absolutely position relative to the outer table?
(The actual HTML is far more messy; there are many more siblings and parents, but this is the heart of the code.)
Position an element adjacent to table rows
Create and append the desired element to your table. Then create and add custom styles that reflect the position of the table row. I've written down some comments below to help navigate the code.
// Setup additional element
const yourContent = document.createElement("div");
yourContent.setAttribute("class", "content");
const yourText = document.createTextNode("Lorem ipsum");
yourContent.appendChild(yourText);
// Find the element you would like to attach to
const graphElem = document.getElementsByClassName("rows");
// Set the styles to be added
const cssTemplateString = `td > .rows > .content{
position: absolute;
left: ${graphElem[0].clientWidth}px; // For element to appear on the left side set to "-" ( negative value )
height: calc(${graphElem[0].clientHeight}px +2px); // The 2 extra pixels are for the border
width: calc(${graphElem[0].clientWidth}px + 2px);
background: white;
padding: 3rem;
border: 1px solid gray;
top: 0;
}`;
const styleTag = document.createElement("style");
styleTag.innerHTML = cssTemplateString;
// Add styles to the head tag
document.head.insertAdjacentElement("beforeend", styleTag);
// Only required for this example
for (let i = 0; i < graphElem.length; i++) {
graphElem[i].addEventListener("click", function (event) {
event.target.appendChild(yourContent);
});
}
td > .rows {
border: 1px solid gray;
width: 8rem;
padding: 3rem;
background: whitesmoke;
}
td {
position: relative; /* The additional content will be positioned relative to this */
}
Demo : https://jsfiddle.net/hexzero/82bpaL4e/
If you click on the table row in the demo, the content will appear on the right.
Question: I want to hover my mouse on a row and again in that particular row, if I hover my mouse on icons(pencil, trash), then the corresponding icons should get rounded borders as shown in first snapshot..
Can you please help me how can I achieve this ? I want everthing to be controlled using JS/jquery actions. Appreciate your help
Explanation: so far what i did and my code..
1) Below is my td element, Now on mouseover the css class icon-hover(below code pasted) should get appended to the span class below
<td class='j-td-edit font-color-meta'><span class="glyphicon glyphicon-pencil"></span></td>
CSS snippet responsible to get the rounded border is below
/*icon hover style*/
.icon-hover {
border: 1px solid #bfbfbf;
padding: 0.4vw;
border-radius: 0.3vw;
}
2) When I hover my mouse on a row, the row gets highlighted with below code and screenshot attached..
/*row hover*/
.hover-color{
background-color: #D0CFCF;
}
=> corresponding JS action for "mouseenter, mouseleave" and image are below
$(document).on('mouseenter', '.row', function () {
var $this = $(this), row = $this.closest("tr");
row.addClass("hover-color");
});
$(document).on('mouseleave', '.row', function () {
var $this = $(this), row = $this.closest("tr");
row.removeClass("hover-color");
});
Using CSS :hover pseudo-class
.row {
height: 35px;
background: #f5f5f5;
}
.row:hover {
background: #dddddd;
}
.row .glyphicon {
padding: 5px;
}
.row .glyphicon:hover {
outline: 1px solid #000000;
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<table width="100%">
<tr class="row">
<td>Test</td>
<td class='j-td-edit font-color-meta'><span class="glyphicon glyphicon-pencil"></span></td>
</tr>
<tr class="row">
<td>Test</td>
<td class='j-td-edit font-color-meta'><span class="glyphicon glyphicon-pencil"></span></td>
</tr>
<tr class="row">
<td>Test</td>
<td class='j-td-edit font-color-meta'><span class="glyphicon glyphicon-pencil"></span></td>
</tr>
</table>
After #jeto words... I found the solution of my code..
//below snippet find the nth(which is 4th td in my case and appends the class below.
$(this).find('td').eq(4).find('span').addClass('icon-hover'));
and
$(this).find('td').eq(4).find('span').removeClass('icon-hover'));
I'm trying to remove a bit of css from a certain page as I'm using an Google Org Chart in my drupal site and some of my css is overriding what is already there. Here is the code below.
What I want to remove:
.MainBlock table td {
border-right: 1px solid #045273;
border-bottom: 1px solid #045273;
vertical-align: top;
}
I've tried a number of things, but nothing has worked. All attempts that I haven't removed are below.
<script type="text/javascript">
if (document.URL.indexOf("/node/7794/draft") >= 0) {
//$('.MainBlock').find("table tr").css("border-bottom","");
//$(".MainBlock table td").css("border-bottom":"12px Solid #000");
$(".MainBlock table td").css({ 'border-bottom' : ''});
}
I need it to ignore that line of css, as it's needed on other pages. That, and setting it to 0 or none sort of breaks it.
You can use 0 or none to remove the border, an empty string does not work.
$( '.MainBlock table td' ).css( { 'border-bottom' : 0 } );
.MainBlock table td {
border-right: 1px solid #045273;
border-bottom: 1px solid #045273;
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="MainBlock">
<table>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
</table>
</div>
Ultimately I'd stick to CSS if you can. Ensure that you place the same selector after the original and it will override it.
/* original rule */
.MainBlock table td {
border-right: 1px solid #045273;
border-bottom: 1px solid #045273;
vertical-align: top;
}
/* sometime later, maybe in a different file */
.MainBlock table td {
border-bottom: 0;
}
An alternative is to increase the selectors specificity. Since it's a Drupal site there should be a page ID to hook onto to, something like:
.page-node-2793683 .MainBlock table td {
border-bottom: 0;
}
EDIT
Per clarification and #EF it:
To prevent the styles being applied to a particular page you can use :not() pseudo selector.
div:not(.page-node-2793683) .MainBlock table td {
border-right: 1px solid #045273;
border-bottom: 1px solid #045273;
vertical-align: top;
}
<div class="page-node-2793683">
<div class="MainBlock">
<table>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
</table>
</div>
</div>
<div class="page-node-10">
<div class="MainBlock">
<table>
<tr>
<td>Content</td>
<td>Content</td>
</tr>
</table>
</div>
</div>
Note: While not required, using not() without a selector proceeding it may not work reliably. The example above may need to be modified to suite your needs.
The beauty of css and the "cascade" (the 'c' in css) is that you don't need javascript to change the way something looks. All you need is a style sheet overrides the "rule" in the other style sheet. Your new rule just has to be more specific and it only needs the properties you are trying to override. In this case
border-bottom: 0;
Have a look at Specificity to learn how to make your rule specific enough to override the old rule.
From the jquery docs http://api.jquery.com/css/#css-propertyName-value:
$( "#mydiv" ).css( "color", "" ) — removes that property from an element if it has already been directly applied
This can't work because border-bottom wasn't assigned directly (i.e. with the style attribute), but through a css rule.
12px Solid doesn't work because it is not valid (the color is missing).
Anyway, I'd suggest to tackle this problem by css directy, not by js
.node-7794 .MainBlock table td {
border-bottom: 0 none;
}
If the rule doesn't work, use the developer console of your browser to find out why
tried to apply this answer to make change the class of the cells in my table on click, yet it doesn't work :(
$('td.link').click(function() {
$('td.button_active').removeClass('button_active');
$('td.link').addClass('button');
$(this).removeClass('button');
$(this).addClass('button_active')
})
My example code in jsfiddle is here..
Could someone take a short look and point what to change?
I am trying to make font red and change background image of clicked cell, other cells leaving with (or returning to) grey font and default backround image.
Thank you in advance!
Valdas
Because you've included MooTools instead of jQuery ;)
Check out this fiddle. It works when using jquery...
$('td.link').click(function() {
$('td.button_active').removeClass('button_active');
$('td.link').addClass('button');
$(this).removeClass('button');
$(this).addClass('button_active')
});
Edit
Here you go, a proper version. What I've done: put buttons inside the table cell (instead of transforming table cells into buttons), used an active class for the active button (instead of copying the button css to the active_button class), and altered the javascript a bit (less lines = nice :))
Check it out here (fiddle)
And the relevant code:
HTML
<table>
<tr>
<td>Link One</td>
</tr>
<tr>
<td>Link Two</td>
</tr>
<tr>
<td>Link Three</td>
</tr>
<tr>
<td>Link Four</td>
</tr>
</table>
CSS
.button {
display: block;
width: 113px;
height: 30px;
text-decoration: none;
background-image: url(http://www.verslomonitorius.lt/uploads/2/1/9/2/21922640/vm_button.svg);
background-repeat: no-repeat;
background-size: 138px 33px;
border: 1px solid #e6e6e6;
text-align: right;
padding: 0 25px 0 0;
font: 16px/30px 'Ubuntu';
color: #737373;
}
.active {
background-image: url(http://www.verslomonitorius.lt/uploads/2/1/9/2/21922640/vm_button_active.svg);
color: #ff0000;
cursor: default
}
.button:not(.active):hover {
background-image: url(http://www.verslomonitorius.lt/uploads/2/1/9/2/21922640/vm_button_hover.svg);
color: #000000;
}
Javascript
$('a.link').click(function(e) {
e.preventDefault();
$('a.active').removeClass('active');
$(this).addClass('active')
});
Note: In a live version, don't forget to wrap your javascript in a $.ready or closure
Be more accurate with libraries defined in jsfiddle. Here is what you need.
$('td.link').click(function() {
$('td.button_active').each(function(index) {
$(this).removeClass('button_active');
});
$('td.link').each(function(index) {
$(this).addClass('button')
});
$(this).removeClass('button');
$(this).addClass('button_active')
})