How do I reduce the border spacing in table? - html

I have a table with two rows, where there is one h1 heading in the left column. The two other columns is normal paragraphs with strong text.
The two right columns in first row goes alright, but on the second row there is too much vertical space between them. It is pretty obvious that it is the h1 which increases the vertical space between them. But how can I fix that, if possible at all? Do I really need to leave the h1 out of the table?
table {
width: 80%;
margin-left: 2%;
border-collapse: separate;
border-spacing: 0 1px;
}
<table>
<tr>
<td><h1>Some big text</h1></td>
<td><p><strong>Some</strong></p>
<td><p><strong>smaller text</strong></p></td>
</tr>
<tr>
<td></td>
<td><p><strong>Here come</strong></p></td>
<td><p><strong>Second row</strong></p></td>
</table>

That spacing is most likely from the default margin on the h1 and p tags, reset them and see.
table {
width: 80%;
margin-left: 2%;
border-collapse: separate;
border-spacing: 0 1px;
}
td {
border: 1px solid aqua; /*for demo purpose*/
}
table h1, table p {
margin: 0; /*reset the margin*/
}
<table>
<tr>
<td><h1>Some big text</h1></td>
<td><p><strong>Some</strong></p>
<td><p><strong>smaller text</strong></p></td>
</tr>
<tr>
<td></td>
<td><p><strong>Here come</strong></p></td>
<td><p><strong>Second row</strong></p></td>
</tr>
</table>
Edit: I figured out (see comments below) what OP refers about the border spacing. It's both the empty <td>, and the border-collapse style. To combine those two <td>s, you can use rowspan. To get rid of the small space amount the lines, use border-collapse:collapse.
table {
width: 80%;
margin-left: 2%;
border-collapse: collapse; /*set it to collapse*/
}
td {
border: 1px solid aqua; /*for demo purpose*/
}
table h1, table p {
margin: 0;
}
<table>
<tr>
<td rowspan="2"><h1>Some big text</h1></td>
<td><p><strong>Some</strong></p></td>
<td><p><strong>smaller text</strong></p></td>
</tr>
<tr>
<td><p><strong>Here come</strong></p></td>
<td><p><strong>Second row</strong></p></td>
</tr>
</table>

Try removing the margin from the h1 like so.
h1 {
margin: 0;
}

There are two things you can do. First, is making the h1 smaller in height, by reducing its margin and its parent padding.
Also, you can use the rowspan attribute in the td which contains your h1 instead of having a empty td on the second row.
The HTML:
<table>
<tr>
<td rowspan="2"><h1>Some big text</h1></td>
<td><p><strong>Some</strong></p>
<td><p><strong>smaller text</strong></p></td>
</tr>
<tr>
<td><p><strong>Here come</strong></p></td>
<td><p><strong>Second row</strong></p></td>
</table>
And the CSS:
table {
width: 80%;
margin-left: 2%;
border-collapse: separate;
border-spacing: 0 1px;
}
table tr td {
padding: 0;
}
table tr td h1 {
margin: 0;
}
table tr td p {
margin: 0;
}

Related

Prevent Table from Expanding When Content Becomes Bold on Hover

I'm working on a table and one of the requirements is for each row to become bold on hover. I have this working but the widths of the columns shift when this occurs.
Is there a way I can prevent this?
table {
width: 100%;
border: 1px solid #ccc;
margin-top: 0;
border-spacing: 5px;
border-collapse: separate;
}
table tr:hover {
font-weight: 600;
}
<table>
<tr>
<td>Cell One</td>
<td>Cell Two</td>
</tr>
</table>
Specifically, it shifts the contents of the second column to the right on hover. While scrolling down the table, this ends up not looking too great.
The key is to reserve space in advance for the bold text.
In other words, when the row is hovered the text goes bold, which results in a wider table cell. But if each table cell factors in the bold text from the start (i.e., before hover), then the width will remain stable regardless of font weight.
In this answer, the content of the table cell is replicated in the title attribute, which is then used to create a bold but invisible version of the content using the ::after pseudo-element.
table {
width: 100%;
border: 1px solid #ccc;
margin-top: 0;
border-spacing: 5px;
border-collapse: separate;
}
table tr:hover {
font-weight: 600;
}
/* new */
td::after {
display: block;
content: attr(title);
font-weight: 600;
color: transparent;
height: 1px;
margin-bottom: -1px;
visibility: hidden;
overflow: hidden;
}
<table>
<tr>
<td title="Cell One">Cell One</td>
<td title="Cell Two">Cell Two</td>
</tr>
</table>

HTML table cell spacing - only between cells, no outer one

I am trying to add cell spacing to a html table.
I want to add spacing between cells without the outer spacing.
My problem is, that the cellspacing html attribute and border-spacing CSS property adds spacing outside too.
I would like to put cell spacing without the red (outer) part - only the yellow one.
Is it possible?
Edit:
The image was drawn by hand (MS-Paint) only for illustration.
The coloring is for debugging - so that one can see where the borders, and spacing is.
I have found a roundabout solution including some additional div-s:
.inner-spacing {
border-collapse: collapse;
background-color: yellow;
border: 2px solid black;
}
.inner-spacing td {
padding: 0;
}
.inner-spacing td > div {
width: 60px;
height: 60px;
background-color: green;
border: 2px solid black;
margin: 10px;
}
.inner-spacing tr:first-child > td > div {
margin-top: 0px;
}
.inner-spacing tr:last-child > td > div {
margin-bottom: 0px;
}
.inner-spacing tr > td:first-child > div {
margin-left: 0px;
}
.inner-spacing tr > td:last-child > div {
margin-right: 0px;
}
<table class="inner-spacing">
<tr>
<td>
<div/>
</td>
<td>
<div/>
</td>
</tr>
<tr>
<td>
<div/>
</td>
<td>
<div/>
</td>
</tr>
</table>
So to summarize, I would like the table to have border spacing with the table border collapsing onto the cells (no spacing).
I wonder if there are some other solutions - so any new solution is welcome!
This will be tricky a little bit...you will need to set display:block and border-spacing:10px for spacing between cells and same negative margin:-10px to remove the outer spacing
Stack Snippet
table {
font: bold 13px Verdana;
background: black;
margin: 30px auto;
border-spacing: 0;
}
table td {
padding: 30px;
background: red;
color: #fff;
}
table tbody {
margin: -10px;
display: block;
border-spacing: 10px;
}
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
This is kinda tricky, you need to follow something like this:
table, td {border: 1px solid #999; border-collapse: collapse;}
table {margin: -5px;}
table td {width: 32px; height: 32px; margin: 5px;}
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>

CSS limit border to table cell TD content

Hello all I'm just trying to have my border around my table cell right around the text...not stretched the length of the entire table. Its the section with the border around it
CSS:
table.content_table {
width: 100%;
margin: 0px;
border-collapse: collapse;
}
table.content_table > tbody > tr > td.results {
border: 2px solid;
background-color: #eeeecc;
font-size: 8pt;
font-weight: bold;
PADDING: 0px;
}
HTML:
<table class="content_table">
<br/><br/>
<h1>Planned Vs Actual Productions Drilldown</h1>
<tr>
<td class="results">
Number of results returned: ${fn:length(beans)}
</td>
</tr>
give the text a simple span or any other block element like div p ... span with inline-block is also a block element which can have a border.
table.content_table {
width: 100%;
margin: 0px;
border-collapse: collapse;
}
.border {
border: 2px solid;
background-color: #eeeecc;
font-size: 8pt;
font-weight: bold;
PADDING: 0px;
display: inline-block;
}
Any Element inside a table needs to be in TD so that is is valid html... put another tr > td into your table like this
<table class="content_table">
<tr>
<td>
<h1>Planned Vs Actual Productions Drilldown</h1>
</td>
</tr>
<tr>
<td class="results">
<span class="border">Number of results returned: ${fn:length(beans)}</span>
</td>
</tr>
</table>
The answer lies in the fact that you have table width as 100%. Without any of styling at the TD level, the TD is automatically going to take the most width it can.
The bigger question though, is why you are using a table at all. This is a single column of data, no need for a table here, just use div's.
I had a similar problem with a WordPress theme. The "collapse" wasn't entirely working on the first column, because my theme's style.css "reset" had set the table width to 100%. At least for me, the "auto" width solved the problem.
<style>
table#donations { border-collapse: collapse; width:auto; }
</style>
<table id="donations">
<tr><td>Bitcoin BTC</td><td>1Prh5VnUJRQV3sARhEfQAMKv9UzGqgAMXg</td></tr>
</table>

Fancy border in a HTML table

So I am styling a table, and I'd like to make quite a fancy underline for the table headings.
I've though hard and had a look on the internet but couldn't find anything.
This is the table structure:
<table>
<thead>
<tr>
<td>Number</td>
<td>Name</td>
<td>Address</td>
</tr>
</thead>
<tbody></tbody>
</table>
And my current styling:
table {
width: 100%;
}
table thead {
font-weight: bold;
}
table thead td {
margin-right: 5px;
border-collapse: separate;
border-spacing: 10px 5px;
border-bottom: 2px solid #427AA8;
}
table tbody {
font-size: 90%;
}
table tbody tr {
line-height: 2em;
border-bottom: 1px solid #CCC;
}
table tbody td {
padding: 0 5px;
}
Here is a jsfiddle of the code: http://jsfiddle.net/tYA4e/1/
What I am looking for is a break in the border between the columns, but only in the thead.
And an example of what I am trying to achieve: http://i.imgur.com/OHrhJ.jpg
Is there a way to achieve this with some simple CSS?
A border will, necessarily, extend the full width of its element; therefore to extend a border only partially across the width of an element that border must be applied to a child element, sized accordingly.
That said, the only way this is achievable would seem to be with nesting an element within the td (in this case a span), and using the following CSS:
table thead td span {
display: inline-block;
width: 80%;
border-bottom: 2px solid #427AA8;
}
JS Fiddle demo.
As an aside, though, it's worth noting that, for table-headings, the th (table-heading) element might be more appropriate for your use in this case.
On further thought it is, of course, also possible to use styled hr elements, which allows you to give pixel-level control over the hr's width in that it can extend up to, in this example, 10px from the right edge of the parent td):
table thead td hr {
width: 80%;
margin: 0 10px 0 0;
border-bottom: 2px solid #427AA8;
}
JS Fiddle demo.
You could also use th for heading cells -> no more need for seperating the rows into groups with thead and tbody - less markup and less css.
<table>
<tr>
<th>headlinecell</th>
</tr>
<tr>
<td>contentcell</td>
</tr>
</table>
now just style the th and td.

Using CSS for solid color background across/between table cells

Consider the following code:
HTML:
<table>
<tr>
<td>Hello</td>
<td>Stack</td>
<td>Overflow</td>
</tr>
<tr>
<td>some</td>
<td>text</td>
<td>here</td>
</tr>
</table>
CSS:
table {
border-spacing: 40px 10px;
border-collapse: separate;
}
tr:first-child {
background-color: #aaa;
}
td {
padding: 5px;
}
I would like the background color on the first row to be between the cells also.
How could I do this ?
I'm sorry but according to this (the last paragraph of section 17.5.1), the background between the cells when you use "border-collapse: separate" is the background of the table element.
This is not possible when using border-spacing, but you could try using individual cell padding instead...
table {
border-collapse: separate;
}
tr:first-child td {
background-color: #aaa;
}
td {
padding: 10px 40px;
}
Live demo available here