I know this is a dumb question but I seem to have totally forgotten how to do it.
I have a HTML table and I want to remove all borders around all the cells so that there is only one border around the entire table.
My code looks like:
<table border='1' width='500'>
<tr><th><h1>Your Wheelbarrow</h1></th><tr>
<th>Product</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Delete</th>
</tr>
Just collapse the table borders and remove the borders from table cells (td elements).
table {
border: 1px solid #CCC;
border-collapse: collapse;
}
td {
border: none;
}
Without explicitly setting border-collapse cross-browser removal of table cell borders is not guaranteed.
The HTML attribute for the purpose is rules=none (to be inserted into the table tag).
Probably you just needed this CSS rule:
table {
border-spacing: 0px;
}
http://jsfiddle.net/Bz3Jt/3/
<style type="text/css">
table {
border:1px solid black;
}
</style>
You might want to try this: http://jsfiddle.net/QPKVX/
Not really sure what you want your final layout to look like- but that fixes the colspan problem too.
Just use your table inside a div with a class (.table1 for example) and don't set any border for this table in CSS. Then use CSS code for that class.
.table1 {border=1px solid black;}
Change your table declaration to:
<table style="border: 1px dashed; width: 500px;">
Here is the sample in action: http://jsfiddle.net/kc48k/
If none of the solutions on this page work and you are having the below issue:
You can simply use this snippet of CSS:
td {
padding: 0;
}
As #brezanac mentioned, you can add the border-collapse, no need for anything else. I attach and example
.table {
border: 1px solid #CCC; // only for example
border-collapse: collapse;
}
th, td {
border: 1px solid #CCC; // only for example
}
<table aria-describedby="table without borders"
class="table">
<tr>
<th id="id">id</th>
<th id="name">name</th>
<th id="price">price</th>
</tr>
<tr>
<td>1</td>
<td>Pizza</td>
<td>7.99</td>
</tr>
<tr>
<td>2</td>
<td>Burger</td>
<td>3.99</td>
</tr>
</table>
<br/>
<table aria-describedby="table with borders">
<tr>
<th id="id">id</th>
<th id="name">name</th>
<th id="price">price</th>
</tr>
<tr>
<td>1</td>
<td>Pizza</td>
<td>7.99</td>
</tr>
<tr>
<td>2</td>
<td>Burger</td>
<td>3.99</td>
</tr>
</table>
Related
I'm using Bootstrap with tables, and trying to make some minor overrides to the default CSS with limited success.
In the table below, I'm able to add a dark border at the bottom of the table head (thead), and to the bottom of the table rows in the footer (tr in tfoot), but I cannot add a border to the bottom of the last table row (tr:last-child), or alternately the bottom of the table body (tbody), or I suppose the top of the table footer (tfoot).
I've had limited success with this:
.table-sm.event-table tbody > tr:last-child {
border-bottom: 2px solid #999;
}
However this doesn't render in all browsers, and only 'works' by making the single pixel light grey line a 2 pixel dark line, which I don't want, I just want a single pixel dark border between the last row of the body and the first row of the footer (between Row Two and Total Expense).
I know this has to do with the specificity of the CSS rules, and Bootstrap's rule taking precedent over my own, but even though I was able to make the other rules work, I cannot for the life of me figure out how to specify this one.
.event-table {
width: 100%;
}
.table thead > tr > th {
border-bottom: 1px solid #333;
}
.table tfoot > tr > td {
border-bottom: 1px solid #333;
}
<table class="table table-bordered table-sm event-table">
<thead>
<tr>
<th>Unit</th>
<th>Total</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total Expense $</td>
<td class="text-right">$200</td>
</tr>
<tr>
<td>Total Revenue $</td>
<td class="text-right">$300</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Row One</td>
<td>$100</td>
</tr>
<tr>
<td>Row Two</td>
<td>$100</td>
</tr>
</tbody>
</table>
Specificity is the name of the game and if you deal with Bootstrap, you'll quickly learn that it get's very complicated and even nigh impossible. While using #ids and !important may be an immediate remedy to your situation, it will bite you in the #rse if they are used even if only moderately. Try using only a few #id if you must and avoid !important at all costs.
A safer solution is to double up on a class:
As a nonsense special case for (2), duplicate simple selectors to increase specificity when you have nothing more to specify.
MDN - The !important exception
The following demo has each table section (i.e. <thead>, <tbody>, and <tfoot>) with it's last row border-bottom a different color. Note that the bootstrap.css file is loaded as well, so it does work to the best of my knowledge and evidence at hand.
Demo
.event-table {
width: 100%;
}
.table thead>tr.rowA1.rowA1>th {
border-bottom: 1px solid red;
}
.table tbody>tr.rowB2.rowB2>td {
border-bottom: 1px solid lime;
}
.table tfoot>tr.rowC2.rowC2>td {
border-bottom: 1px solid blue;
}
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
<table class="table table-bordered table-sm event-table">
<thead>
<tr class='rowA1'>
<th>Unit</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class='rowB1'>
<td>Row One</td>
<td>$100</td>
</tr>
<tr class='rowB2'>
<td>Row Two</td>
<td>$100</td>
</tr>
</tbody>
<tfoot>
<tr class='rowC1'>
<td>Total Expense $</td>
<td>$200</td>
</tr>
<tr class='rowC2'>
<td>Total Revenue $</td>
<td>$300</td>
</tr>
</tfoot>
</table>
Give your tags an id instead of a class. This way, when you go to style the certain element with the id in your css, it will be at a higher priority than the Bootstrap style, which would erase the need for !important in most cases
So say you add an id to your table tag in the html like so
<table class="table table-bordered table-sm event-table" id="main-table">
You should be able to do this with success
#main-table {
width: 100%;
}
#main-table thead > tr > th, #main-table tfoot > tr > td {
border-bottom: 1px solid #333;
}
Sometimes you will have to use !important to override Bootstrap styles like so
border-bottom: 2px solid #999 !important;
I want to create a table with only partly separated borders. The borders above and below the thead should be without spaces in between. But others in it should be separated by a small space.
Unfortunately, the border-spacing style only applies to the whole table: https://developer.mozilla.org/en-US/docs/Web/CSS/border-spacing
For example, in the following I want to have space only between the border-top of h2.1 and h2.2. Is that possible?
HTML:
<table>
<thead>
<tr>
<th rowspan="2">h1</th>
<th colspan="2">h2</th>
</tr>
<tr>
<th>h2.1</th>
<th>h2.2</th>
</tr>
</thead>
<tbody>
<tr>
<td>b1</td>
<td>b2.1</td>
<td>b2.2</td>
</tr>
<tr>
<td>b1</td>
<td>b2.1</td>
<td>b2.2</td>
</tr>
</tbody>
</table>
CSS:
table {
border-collapse: separate;
}
th,
td {
vertical-align: top;
}
thead tr:first-child th {
border-top: 2px solid;
}
thead tr:not(:first-child) th {
border-top: 1px solid;
}
tbody tr:first-child td {
border-top: 1px solid;
}
tbody tr {
border-top: 1px solid;
}
Fiddle: https://jsfiddle.net/6ov4hadd/
Edit
Here is a more sensible example.
Fiddle: https://jsfiddle.net/Lnk929q4/
I want to look it like a "book table":
You can try using two different tables for head part and body part. Something like this
https://jsfiddle.net/6ov4hadd/1/
<table id = "table1">
<thead>
<tr>
<th rowspan="2">h1</th>
<th colspan="2">h2</th>
</tr>
<tr>
<th>h2.1</th>
<th>h2.2</th>
</tr>
</thead>
</table>
<table>
<tbody>
<tr>
<td>b1</td>
<td>b2.1</td>
<td>b2.2</td>
</tr>
<tr>
<td>b1</td>
<td>b2.1</td>
<td>b2.2</td>
</tr>
</tbody>
</table>
Is it possible to have a vertical line running down the whole length of a table on the change of a <th> tag. For example in the table below
How can I add a vertical line running from top to bottom between each month using CSS?
Depends on what HTML you've got for your table, but here is an example. Use this code to add a vertical border.
table { border-collapse: collapse; }
tr { border: none; }
.line {
border-left: solid 1px #000;
}
<table>
<tr>
<th colspan="2" class="line">Persons</th>
</tr>
<tr>
<th class="line">Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td class="line">James</td>
<td>Bond</td>
</tr>
<tr>
<td class="line">Clark</td>
<td>Kent</td>
</tr>
</table>
I have several html tables in my content area of my page. The style is weird because it doesn't start the alternating row color fresh at the start of each table, it carries it on through out the list of tables.
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
<table>
<tr>
White
</tr>
<tr>
Blue
</tr>
<tr>
White
</tr>
</table>
The colour in the rows is a representation of what the css would set as the row background. But I want css to start the alternating again for the next table. So it would be:
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
<table>
<tr>
Blue
</tr>
<tr>
White
</tr>
<tr>
Blue
</tr>
</table>
Does THBODY have anything to do with it?
Thanks,
CSS Code
table { border-collapse:collapse; text-align:center; }
table th, td { border:1px solid #759EC7; padding:3px 7px 2px; }
th { color: #fff;
background-color: #5c87b2; text-align:center; }
tr:nth-child(odd) { background-color: #CEE1F5; }
tr:nth-child(even) { background-color: #fff; }
Update
It may be a bug that has crept in, I've look on the suggested fiddles and it works perfectly so it is just some buggy code somewhere.
You can easily achieve it using combinations of :nth-child() by passing even and odd values. For eg. see this fiddle.
where, the CSS is
body {
background-color: black;
color: red;
}
table tr:nth-child(odd) {
background-color: blue;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
The only problem you have is missing the tag in the table.
It works perfectly if you add it. It shouldnt have anything to do with the tbody tag.
<table>
<tr>
<td>Blue</td>
</tr>
<tr>
<td>White</td>
</tr>
<tr>
<td>Blue</td>
</tr>
</table>
<table>
<tr>
<td>Blue</td>
</tr>
<tr>
<td>White</td>
</tr>
<tr>
<td>Blue</td>
</tr>
</table>
here is the fiddle: http://jsfiddle.net/rBwBm/
I think you're doing it using javascript, right ? Probably getting a collection of tr through jquery with $('tr') ? Try using CSS nth-child(odd) and nth-child(even) instead, most modern browsers won't have any problem with that.
The issue I was having was with two <TH> rows, which through off the alternating row colouring. So for example:
<tr>
<th colpsan="2">Name</th>
</tr>
<tr>
<th>First</th>
<th>Last</th>
</tr>
This would have the Blue start on the Name row and then start alternating. So the first line of the table body would be Blue
<tr>
<th>Name</th>
</tr>
This would have the Blue start on the Name row like before and then start alternating, However, the first line of the table body would be White
In these situations it would show a changing style which is not what I wanted to achieve. So all I did to fix this is:
<thead>
<tr>
<th colpsan="2">Name</th>
</tr>
<tr>
<th>First</th>
<th>Last</th>
</tr>
</thead>
<tbody>
<!-- Table Content in Here -->
</tbody>
And I then changed the style sheet to be:
tbody tr:nth-child(odd) {}
tbody tr:nth-child(even) {}
So basically I used the TBody and THead tags to make a more specific css style which is brilliant. More control, flexibility. So in my new example, you can have as many rows in the THead as you like, the content should always start on White, and to answer my question:
Does THead have anything to do with it?
Yes, it has EVERYTHING to do with it.
What would be the html markup to achieve the same result as the first table in http://reference.sitepoint.com/css/tableformatting#tableformatting__tbl_table-objects-display-values
I am looking for how they specified the column group and how to set the title (Women, Men). Also, how to target the specific column group in css.
thanks,
bsr.
Good question. I sat down and reflected on the last time I addressed table formatting issues, then navigated to following links:
http://www.w3.org/TR/html4/struct/tables.html#h-11.2.4
http://www.w3.org/TR/CSS2/tables.html
After some meditation and drinking water, wrote some code for you to refer:
body {
background: #e4e4e4;
font-family: sans-serif;
}
th {
background: #d5d6d6;
}
td {
background: #fff;
}
table {
border-collapse: separate;
border-spacing: 1em 0.5em;
background-color: #ddd;
}
th, td {
border: 1px solid #000;
padding: 4px;
}
tfoot {
font-weight: bold;
}
<table>
<thead>
<tr><th rowspan="2">Question</th><th colspan="2">Women</th><th colspan="2">Men</th></tr>
<tr><th>Yes</th><th>No</th><th>Yes</th><th>No</th></tr>
</thead>
<tbody>
<tr><th>Question1</th><td>42%</td><td>58%</td><td>61%</td><td>39%</td></tr>
<tr><th>Question2</th><td>53%</td><td>47%</td><td>69%</td><td>31%</td></tr>
<tr><th>Question3</th><td>26%</td><td>74%</td><td>51%</td><td>49%</td></tr>
</tbody>
<tfoot>
<tr><th>Average</th><td>40%</td><td>60%</td><td>60%</td><td>40%</td></tr>
</tfoot>
</table>
Mostly when I try to see such layouts, I attempt to count how many rows and how many columns will be necessary in the final html. This helps to construct the html properly.
CSS then simply becomes a selection of those elements with either classes or elements. For your question I chose elements.
HTH!
#gsvolt
I just write the same table as you mentioned in a link. I hope it'll help you out. Thanks
thead th,
tbody tr td:first-child {
background-color: #ccc;
}
<table border="1" style="border-collapse: collapse;">
<thead>
<tr>
<th rowspan="2">Question</th>
<th colspan="2">Women</th>
<th colspan="2">Men</th>
</tr>
<tr>
<th>Yes</th>
<th>No</th>
<th>Yes</th>
<th>No</th>
</tr>
</thead>
<tbody>
<tr>
<td>Question 1</td>
<td>42%</td>
<td>58%</td>
<td>61%</td>
<td>39%</td>
</tr>
<tr>
<td>Question 2</td>
<td>53%</td>
<td>47%</td>
<td>69%</td>
<td>31%</td>
</tr>
<tr>
<td>Question 3</td>
<td>26%</td>
<td>74%</td>
<td>51%</td>
<td>49%</td>
</tr>
<tr>
<td>Average</td>
<td>40%</td>
<td>60%</td>
<td>60%</td>
<td>40%</td>
</tr>
</tbody>
</table>
To make a <th> span a set of rows, give it a rowspan attribute. For collumns, a colspan attribute.
To then target that <th> element, use the normal CSS selector methods, such as .class, #id, tag, etc.
you can use <colgroup> and <col class="men">
see: http://www.smashingmagazine.com/2008/08/13/top-10-css-table-designs/
5. Vertical Zebra Style