I'm trying to do something very simple: create a table with single line borders.
There are many articles saying how to do that, and almost all of them include something like
table {
border-collapse: collapse;
}
td, th {
border: 1px solid orange;
}
Which works great.
But they all apply the styling universally to the td and th tags themselves, and therefore apply to all tables.
So I tried this
.bordered {
border-collapse: collapse;
border: 1px solid orange;
text-align: center;
color: blue;
}
<table class=bordered>
<tr> <td> ABC </td> <td> DEF </td> </tr>
<tr> <td> HIJ </td> <td> JLK </td> </tr>
</table>
I get a table with orange outer border, blue letters, and no internal borders.
I also tried
table.bordered, tr.bordered, td.bordered {
to no avail. Also putting "class=" on the tr tags didn't help.
I have learned that border properties are not inherited.
The DOM Inspector confirms that: just the color and centering are inherited by the td elements from the .bordered class.
My question is this:
How do I get borders on the cells without adding "class=" to every single td tag?
(A use case would if there are two tables on the page, and I want the borders styled differently for them).
Just take the css that works for all tables, and add table.bordered before all of them:
table.bordered {
border-collapse: collapse;
}
table.bordered td, table.bordered th {
border: 1px solid orange;
}
<table class="bordered">
<tr> <td> ABC </td> <td> DEF </td> </tr>
<tr> <td> HIJ </td> <td> JLK </td> </tr>
</table>
<table>
<tr> <td> ABC </td> <td> DEF </td> </tr>
<tr> <td> HIJ </td> <td> JLK </td> </tr>
</table>
Related
What I'm trying to achieve is the following table (which I created using Word):
Each cell basically acts as if it has two columns within it, the text-characters on the left and the digits on the right. How can I do this in HTML? The closest I have gotten is a normal table with empty cells acting as the space between the text and the digits, but that's not what I want. What's a cleaner way to do this?
If I'm understanding your intent correctly, see example below using a default auto layout and sharing a width between the wide columns to allow the skinny ones to only consume space necessary (unless you want to give them a fixed width of sorts). Though in the future a reproducible example of your effort is appreciated.
table {
table-layout: auto;
border-collapse: collapse;
border-bottom: #000 1px solid;
width: 100%;
}
th { text-align: left }
th, td {
padding: 0 .5rem;
}
table th:nth-child(odd) {
width: 50%;
border: #0f0 1px dashed;
}
table th:nth-child(2), table td:nth-child(2) {
border-right: #000 1px solid;
}
table tr:first-child {
border-bottom: #000 1px solid;
}
table tr:last-child {
border-top: #000 1px solid;
}
<table>
<tr>
<th>
wide column
</th>
<th>
skinny
</th>
<th>
wide column
</th>
<th>
skinny
</th>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
</table>
I'm trying to reproduce the following table in HTML (retrieved from a PDF):
Notice how the paragraphs on the same cell are aligned with paragraphs in other cells.
Below I show to you the table in a snippet:
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<table style="width:50%">
<tr>
<td rowspan="2">
<p>Procedimentos</p>
</td>
<td colspan="2">
<p>Taxas (euros)</p>
</td>
</tr>
<tr>
<td>
<p>Obtenção</p>
</td>
<td>
<p>Renovação</p>
</td>
</tr>
<tr>
<td>
<p>1 —</p>
<p>2 —</p>
<p>3 —</p>
<p>4 — Produtor de semente de variedades de conservação</p>
<p>5 — Acondicionador de semente de variedades de conservação</p>
</td>
<td>
<p>...</p>
<p>...</p>
<p>...</p>
<p>200</p>
<p>150</p>
</td>
<td>
<p>...</p>
<p>...</p>
<p>...</p>
<p>30</p>
<p>15</p>
</td>
</tr>
</table>
If the paragraphs fit completely the cell without line break (remove "width" in <table>), everything is ok. However, when the table shrinks (as shown in the snippet), there is a line break and the paragraphs are no longer aligned (naturally).
I see two approaches here:
via CSS, which I don't know how.
via HTML, by subdividing the whole table such that each paragraph alignment becomes a new table row.
Option 2. is quite painful to program since I'm building these tables programatically (from PDF), which means an algorithm to subdivide the table.
Does anyone knows how to force the constraint that the paragraphs should stay aligned? Is that possible in CSS (with cross browser support)?
You need to make a <tr> for each row containing 3 <td> for each column so they stay aligned, <p> are not needed.
table, th, td {
border: 1px solid black;
border-collapse: collapse;
vertical-align: middle;
}
td {border-top: none; border-bottom: none;}
th, td {padding: 12px 16px;}
<table style="width:50%">
<tr>
<th rowspan="2"> <!-- use th for border -->
Procedimentos
</th>
<th colspan="2">
Taxas (euros)
</th>
</tr>
<tr>
<th>
Obtenção
</th>
<th>
Renovação
</th>
</tr>
<tr>
<td> <!-- use td for no border -->
1 —
</td>
<td>
...
</td>
<td>
...
</td>
</tr>
<tr>
<td>
2 —
</td>
<td>
...
</td>
<td>
...
</td>
</tr>
<tr>
<td>
3 —
</td>
<td>
...
</td>
<td>
...
</td>
</tr>
<tr>
<td>
4 — Produtor de semente de variedades de conservação
</td>
<td>
200
</td>
<td>
30
</td>
</tr>
<tr>
<td>
5 — Acondicionador de semente de variedades de conservação
</td>
<td>
150
</td>
<td>
15
</td>
</tr>
</table>
As #Heah writes, you need to put the data in separate rows. This is also better for accessibility, since it corresponds to the logical structure of the data and lets items be accessed as table cells, not as paragraphs inside a cell.
In addition, to reproduce the layout of the PDF file, you need to set borders in a more detailed manner in each direction and to reduce font size in header cells. The following does this relatively accurately, except that it does not produce the rows of dots (which are a separate issue, often asked at SO—no simple answer, but several possible approaches).
<style>
table {
width: 26em;
border: solid;
border-width: 3px 0;
border-collapse: collapse;
}
th, td {
vertical-align: bottom;
text-align: center;
border: solid 2px gray;
}
td {
border-top: none;
border-bottom: none;
}
td:first-child {
text-align: left;
text-indent: -1em;
padding-left: 1em;
}
th {
padding: 0.3em 0.4em;
font-weight: normal;
vertical-align: middle;
font-size: 80%;
}
th:first-child, td:first-child {
border-left: none;
}
th:last-child, td:last-child {
border-right: none;
}
</style>
<table>
<thead>
<tr><th rowspan="2">Procedimentos
<th colspan="2">Taxas (euros)
<tr><th>Obtenção
<th>Renovação
</thead>
<tbody>
<tr>
<td>1 —
<td>…
<td>…
<tr>
<td>2 —
<td>…
<td>…
<tr>
<td>3 —
<td>…
<td>…
<tr>
<td>4 — Produtor de semente de variedades de conservação
<td>200
<td>30
<tr>
<td>5 — Acondicionador de semente de variedades de conservação
<td>150
<td>15
</tbody>
</table>
I'm generating HTML emails from Sql Server and some of the emails will contain a table with many, many rows. I'd like to implement frozen column headers, so that when scrolling the data (inside the email), the column header always remains visible. I've attempted many different solutions using css, to no avail. I understand Outlook won't recognize javascript - so I'm stuck with using css to do this - which I'm able to do in various web sites, but Outlook 2010 does not react the same way.
I understand the css engine in Outlook is really like going back to the days of 2001. Can anyone offer any suggestions on how to accomplish this. I'm only interested in Outlook functionality, as that is our only email vehicle.
I took a shot at using this code, which works in jsfiddle - but not in email:
<style type="text/css">
table, td {
text-align:center;
}
th {
font-weight:bold;
text-align:center;
}
table th {
padding:10px 10px 10px 10px;
border-top:0;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #ededed;
}
table th:first-child {
text-align: left;
}
table tr:first-child th:first-child {
border-top-left-radius:3px;
border-left: 0;
}
table tr:first-child th:last-child {
border-top-right-radius:3px;
}
table tr {
text-align: center;
}
table td:first-child {
text-align: left;
border-left: 0;
}
table td {
padding:10px;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
}
table tr:last-child td {
border-bottom:0;
}
table tr:last-child td:first-child {
border-bottom-left-radius:3px;
}
table tr:last-child td:last-child {
border-bottom-right-radius:3px;
}
table th, table td {
width: 160px;
}
#wrapper {
width: 740px;
height: 300px;
overflow-x: scroll;
overflow-y: scroll;
}
table thead {
position:fixed;
}
</style>
<body>
<div id="wrapper">
<table>
<!-- Table Header -->
<thead>
<tr>
<th>Task Details</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Progress</th>
<th>Vital Task</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<tr>
<td>Create pretty table design</td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Table Row -->
<tr>
<td>Take the dog for a walk</td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Darker Table Row -->
<tr>
<td>Waste half the day on Twitter</td>
<td> </td>
<td> </td>
<td>20%</td>
<td>No</td>
</tr>
<tr>
<td>Feel inferior after viewing Dribble</td>
<td> </td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>Wince at "to do" list</td>
<td> </td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr>
<tr>
<td>Vow to complete personal project</td>
<td> </td>
<td> </td>
<td>23%</td>
<td>yes</td>
</tr>
<tr>
<td>Procrastinate</td>
<td> </td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>Hyperlink Example</td>
<td> </td>
<td> </td>
<td>80%</td>
<td>Another</td>
</tr>
</tbody>
<!-- Table Body -->
</table>
</div>
</body>
Thanks so much for any help,
Paul
Unfortunately Outlook doesn't support CSS position or overflow, so doing this in CSS looks as though it is not possible.
Because you are targeting Outlook only, you could try messing with VML. That is mostly uncharted territory, so not sure if you can find something that works. Check out backgrounds.cm for an example of how VML can be applied to a html email. Let us know how it goes if you try that route.
My example code:
<!DOCTYPE html>
<html>
<body>
<h4>Two rows and three columns:</h4>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td>
</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>
<table border="1">
<tr>
<td>
test
</td>
<td>
<table border="1">
<tr>
<td>
test
</td>
<td>
wuut
</td>
</tr>
<tr>
<td>
test1
</td>
<td>
wuut1
</td>
</tr>
<tr>
<td>
test2
</td>
<td>
wuut2
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<style>
table {
border-collapse: collapse;
}
</style>
You can just paste it here and see what it looks like : http://www.w3schools.com/html/tryit.asp?filename=tryhtml_tables
What I need is that when tables are inside each other, tables have like joined borders. Only tables that data are separated.
At the moment the right bottom corner of table has like 3 layers of border, but that just looks ugly.
I tried using CSS:
border-collapse: collapse;
But this just removed cellspacing for borders :/
It should look like this, but this is with colspan/rowspan, which is too messy:
<!DOCTYPE html>
<html>
<body>
<h4>Two rows and three columns:</h4>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td colspan="3"> </td>
</tr>
<tr>
<td rowspan="3">400</td>
<td rowspan="3">500</td>
<td rowspan="3">test</td>
<td>test</td>
<td>wuut</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
</tr>
<tr>
<td>wuut1</td>
<td>wuut2</td>
</tr>
</table>
</body>
</html>
Modify the program code that generates the markup so that there are no border=1 attributes and there are class attributes for td elements, controlling borders around each cell. The class attribute would corresponds to CSS settings that set a border on selected sides of a cell, e.g. <td class="left top"> with CSS code:
.left { border-left-style: solid }
.top { border-top-style: solid }
The width and color of borders you can set in one rule, like:
td { border-width: 1px; border-color: #333; }
You should still set table { border-collapse: collapse } and probably set padding: 0 on each cell that contains a table.
It’s a bit tricky, because the borders of nested tables are drawn separately. But you can tune things with some CSS3 so that they work in the desired way on modern browsers. (If you wish to achieve the effect on ancient browsers, too, you would need to scatter around a lot of class attributes.)
You need to remove the default cell spacing from (at least) cells containing tables. (The spacing between borders of inner and outer table come from the cells spacing.) This requires that each td that contains a table has a suitable class attribute, say class=containsTable, because in CSS you cannot refer to an element by its descendants (contents). Moreover, you need to selectively switch off top borders from the cells of the first row of any nested table, etc.:
.tableContainer { padding: 0; }
table table { border: none }
table table tr:first-child td { border-top: none; }
table table tr:last-child td { border-bottom: none; }
table table td:first-child { border-left: none; }
table table td:last-child { border-right: none; }
Try <table style="border:0;"> wont show borders if that's what your looking for and you can also be specific about which side you want to display like for example:
<table style="border-left:1px solid black;">
You can enter to the style border-(left,right,bottom,top):"pixels" "Type of border" "color".
<td style="border:0px;">
test
</td>
<td style="border:0px;">
wuut
</td>
</tr>
it wont show them. Or give them an ID and use <style type="text/css">
<style type="text/css">
#aa {border:0px;}
</style>
...
<td ID="aa">
...
if you can add ID="aa" to that loop then it should work.
I want to place text at center and at the bottom of the cell in table. How can I do this?
This is what I want:
CSS:
td {vertical-align:bottom;text-align:center;}
example: http://jsfiddle.net/vKPG8/
valign="bottom" might work.
http://www.w3schools.com/tags/att_td_valign.asp
But I think that's deprecated.
So you should do it in CSS with:
<td style="vertical-align:bottom;">your text</td>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
td{
vertical-align:bottom;
text-align:center;
}
<table>
<tr>
<td rowspan="3" width="100%">bottom and center</td>
</tr>
<tr>
<td rowspan="1">TOTAL</td>
</tr>
<tr>
<td rowspan="1">TOTAL</td>
</tr>
</table>
use style vertical-align:bottom;text-align:center; these can be helpful for your table.