I am trying to create a table that has 1 row and 8 columns. However, I want a blank spaces after the 3rd and 6th table cell. The result should be:
cell cell cell blank space cell cell cell blank space cell cell
I have tried placing margins but they don't work. I have tried implementing this code, but it doesn't work.
.brzl td:nth-child(3){
margin-right: 20px;
}
Edit:
I am trying to implement this within an AngularJS project. In the index.html i have the following code:
<div ng-switch-when="brzl">
<table class="brzl">
<tbody>
<tr>
<td ng-repeat="cell in mini.vrednost track by $index">
{{ cell }}
</td>
</tr>
</tbody>
</table>
<label class="small-label"> {{ mini.label }} </label>
</div>
The mini.vrednost basically loops through some JSON data (e.g. "123456789").
Each digit has to be placed within separate cell in the table. Now, once the '1','2', and '3' have been placed in the cells, I need to put an empty field after them and then continue with '4', '5' etc. The empty field cannot be read from the JSON data, since the whole string is already read from somewhere (I suppose the database).
I know I should have mentioned this earlier. That was my bad.
Margin doesn't work on table cells. However, padding does. Of course, if you have added borders to your table, this means you get one cell with a lot of white space.
Fiddle
What you could do (through hard coding or by injecting with Javascript or jQuery) is add a blank cell where you want the white space, remove any styling of that cell through CSS and add a width.
Working fiddle
HTML
<table>
<tbody>
<tr>
<td>cell</td>
<td>cell2</td>
<td>cell3</td>
<td></td> <!-- blank cell, no border -->
<td>cell4</td>
<td>cell5</td>
<td>cell6</td>
<td></td> <!-- blank cell, no border -->
<td>cell7</td>
</tr>
</tbody>
</table>
CSS
td {
border: 1px solid #ccc;
}
td:nth-child(4n+4){
border: none;
width: 30px; /*desired blank space*/
}
As you can see, you'll have to target every 4th cell, starting with the 4th. If you have any questions, just ask and I'll see if I can adapt the answer accordingly.
UPDATE AFTER OP'S EDIT
However, since your table is filled with data by some script, you might want to run the following jQuery AFTER the loop is done filling up the table. I have no idea how it will react on huge tables with lots of content, but it works in the updated Fiddle below.
$('tr > td:nth-child(3n+3)').after('<td></td>');
This piece of jQuery takes every 3rd child, starting with the 3rd, and adds an empty <td> to it, which is then styled by the CSS. Of course you could ad a <td> with a specific class which you then target with CSS, but as it is now, it seems to work fine.
Fiddle
Remember to add jQuery!
As i told you, you can't use margin but you can try like this method -
table {
width: 100%;
}
td{
background: #ccc;
padding: 10px;
text-align: center;
}
td:nth-child(3n){
background: #fff;
}
<table width="50%">
<tbody>
<tr>
<td>data</td>
<td>data</td>
<td> </td>
<td>data</td>
<td>data</td>
<td> </td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
</table>
Or you can try this to set border on right side on nth-child
table {
border-collapse: collapse;
}
td{
background: #ccc;
padding: 10px;
}
td:nth-child(3n){
border-right:10px solid #fff;
}
<table width="50%">
<tbody>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
</table>
Try like this: Demo
td {
border: 1px solid #ccc;
padding:10px;
}
td:nth-child(4n) {
border-color: #fff;
}
HTML:
<table>
<tr>
<td>test</td>
<td>test</td>
<td>test</td>
<td></td> <!-- 3,6,... cols should be empty to get blank space -->
<td>test</td>
<td>test</td>
<td>test</td>
<td></td>
<td>test</td>
</tr>
</table>
Related
<tr>
<td></td>
<td colspan="3">Susi Handayani Jl. Kebangsaan No.225 300.000</td>
</tr>
How to merge the two td to be inside one Td, but it's not sticking together, merged but I want the word to not stick together beside, make some space from the deleted td to be the same column as above
I tried align but it didn't Work, I've also tried dividing the tr and tried removing td for one paragraph and it still sticks with the second paragraph (td), what I'd expect is the td not to stick together but to align the text above the text that I've made
<h3>Tabel HTML</h3>
<table>
<caption>Tabel Simpanan Peserta</caption>
<thead>
<tr>
<th>No</th>
<th>Nama Peserta</th>
<th>Alamat</th>
<th>Simpanan</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">Total</td>
<td>350.000</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>1.</td>
<td>Andi Suryono</td>
<td>Jl. Kemerdekaan No.17</td>
<td>50.000</td>
</tr>
<tr>
<td>2.</td>
<td colspan="3">Susi Handayani
Jl. Kebangsaan No.225
300.000</td>
</tr>
<tr>
<td>3.</td>
<td>Roy Pratama</td>
<td>Jl. Merdeka No.32</td>
<td>1.000.000</td>
</tr>
</tr>
<tr>
<td>4.</td>
<td>Tia Suryani</td>
<td>Jl. Jelajah No.111</td>
<td>1.555.000</td>
</tr>
</tbody>
</table>
I'm trying to make the table fused but not sticking together, I'm planning to give the word some space and how do I make the space for each column
You're talking about styling, yet you posted no style tag or CSS whatsoever. Post the general CSS you have, to reproduce the problem, or at least a screenshot of what the problem is.
Generally, spacing in tables comes with padding. Try something like:
<style>
td {
padding: 5px 10px;
}
</style>
This will put 5px top and bottom and 10px from both sides of every cell in the table.
tr {
margin-bottom: 100px;
}
<table>
<thead>
<tr>
<th>School Name</th>
<th>State</th>
<th>League</th>
<th>Division</th>
</tr>
</thead>
<tbody>
<tr>
<td>School1</td>
<td>State1</td>
<td>League1</td>
<td>Division1</td>
</tr>
</tbody>
</table>
I need a table like this with some basic information where I'll add a button that on click expands that row with some more details.
I couldn't find the right answer for that so I thought about changing css with some transition to the table on click where some specific row gets margin bottom from 0 to 100px maybe and then find the position of that tr and let say:
trPosition= 100px;
trHeight= 30px;
The bottom of the tr is at: 70px
And some new element appends with jquery at that position with a height of tr margin bottom.
But before all that I can't even use margin bottom between rows, why is that?
And is there some other solution for this?
Table elements do not have margin - you can use padding-bottom here. Another thing is you need to apply it to the td or th element.
See demo below:
tr > td {
padding-bottom: 100px;
}
<table>
<thead>
<tr>
<th>School Name</th>
<th>State</th>
<th>League</th>
<th>Division</th>
</tr>
</thead>
<tbody>
<tr>
<td>School1</td>
<td>State1</td>
<td>League1</td>
<td>Division1</td>
</tr>
<tr>
<td>School1</td>
<td>State1</td>
<td>League1</td>
<td>Division1</td>
</tr>
</tbody>
</table>
If you change display for block, you will see margin, but I'm not sure, if that's what you need. You should use different methods.
margin-bottom: 100px; display: block;
I have a simple HTML table generated with PHP from a database where in some cases one cell spans over more than one row. As a simple example:
<table>
<tr class ="highlightRow">
<td>Cell 1</td>
<td rowspan="2">Cell over more rows</td>
</tr>
<tr class ="highlightRow">
<td>Cell 2</td>
</tr>
</table>
I highlight the row the mouse is over with a simple CSS rule:
.highlightRow {
background-color: #FFF;
}
.highlightRow:hover {
background-color: #EEE;
}
I can not find any solution (that is CSS only) to highlight 'Cell over more rows' when the mouse hovers over 'Cell 2'.
I don't know if this will satisfy your need, but have a look at my solution
.highlightRow {
background-color: #FFF;
}
.highlightRow:hover{
background-color: #EEE;
}
table:hover .include{
background-color: #EEE;
}
<table>
<tr class ="highlightRow">
<td>Cell 1</td>
<td rowspan="2" class="include">Cell over more rows</td>
</tr>
<tr class ="highlightRow">
<td>Cell 2</td>
</tr>
</table>
Thr trick is to highlight .include cell every time the table has been hovered and add some rule to highlight tr everytime it's hovered.
I had meet same problems in my projects.We cannot do it.That is why most of all developers perefered jquery DOM traversing methods(parent(),child(),sibling(),next(),prev(),After(),etc..,)
Reference: Is there a CSS parent selector?
Conclusion is :there is no option in CSS for select the parent.we can with javascript help.
**we love coding..*
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.
I use Richfaces and have a rich:datatable with nested rich:tooltip-s.
You can imagine the generated HTML looks like this:
<table style="width: 400px; border: 3px solid #000; caption-side: bottom; border-collapse:collapse;">
<caption align="bottom">Table 1.1: A record of the fur shed annually by Jennifer's dog Shasta</caption>
<thead>
<tr>
<th>Month</th>
<th>Fur Shed (mm)</th>
</tr>
<thead>
<tbody style="background-color: #ff3;">
<tr>
<td>April</td>
<td>20</td>
</tr>
<tr>
<td>May</td>
<td>19</td>
</tr>
<tr>
<td>June</td>
<td>10</td>
</tr>
<tr>
<td>July</td>
<td>6</td>
</tr>
<tr>
<td>August</td>
<td>8</td>
</tr>
<tr>
<td>September</td>
<td>14</td>
</tr>
</tbody>
<tbody>
<tr>
<td style="display:none;">
<script type="text/javascript">
new RichFaces.ui.DataTable("form1:table1:0:j_idt227",{"ajaxEventOptions":{}} )
</script>
</td>
</tr>
The problem with this html is in the 2nd (generated from RF) tbody: td has style="display:none;" and in Google Chrome this causes the bottom border being not shown.
My question is: do you know if it is possible to find a workaround to fix this? Moving the display:none; at tr or tbody level would already be a solution.
Thanks!
You can add a footer to the table (<f:facet name="footer">) which will render under the hidden row but if you don't want to you can use this CSS:
table > tbody > tr:last-child {
border-bottom: 3px solid #000;
}
this will find the last row and add a border at the bottom, of course this will affect every table on your page so you should use some identifiers. Also note that the :last-child selector may not be supported by all browsers (it does work in Chrome).
Other alternative is to wrap the table in a div but you'd need to play a little with the CSS to make it look the way you want.