CSS-selector for children [duplicate] - html

This question already has answers here:
How do I apply a style to all children of an element
(2 answers)
Closed 27 days ago.
Lets say I have 2 tables. The 2nd one got an id "mytab".
<table>
<tr>
<td>abc</td>
</tr>
</table>
<table id="mytab">
<tr>
<td>xyz</td>
</tr>
</table>
Now I want a CSS for just the 2nd one but for all child-elements too (table has children th, tr, td). What would the CSS-selector look like??
I tired these 2 selectors, but none did the trick.
This would address all tables - not just the 2nd one.
table, th, tr, td
{
border: 1px solid black;
border-collapse: collapse;
}
This would address the correct table but not the children beause they do not have the id set.
table#mytab, th#mytab, tr#mytab, td#mytab
{
border: 1px solid black;
border-collapse: collapse;
}

you need to target the parent then use * to select all childs, so like parent *
table#mytab , table#mytab *
{
border: 1px solid black;
border-collapse: collapse;
}
<table>
<tr>
<td>abc</td>
</tr>
</table>
<table id="mytab">
<tr>
<td>xyz</td>
</tr>
<tr>
<td>asd</td>
</tr>
<tr>
<td>zxc</td>
</tr>
</table>

Related

provide the code for colspan and rowspan?

expecting the code to for that image that i have attached
In order to create such a table, you'd need to implement the rowspan and colspan attributes. colspan represents the number of columns a cell should span (to the right), and rowspan represents the number of rows a cell should span (downward). For instance, if I had a <td colspan="2"> tag, the td element would take up the space of two columns. Here's an example of that behavior:
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<table>
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td colspan="2">This element occupies the space of two columns.</td>
</tr>
</table>
Utilizing rowspan and colspan, I was able to create code that generates a look-alike of the table you attached (CSS code solely for table visibility):
table,
td {
border: 1px solid black;
border-collapse: collapse;
}
<table>
<tr>
<td rowspan="2"></td>
<td colspan="2">Average</td>
<td rowspan="2">Red Eyes</td>
</tr>
<tr>
<td>Height</td>
<td>Weight</td>
</tr>
<tr>
<td>Males</td>
<td>1.9</td>
<td>0.003</td>
<td>40%</td>
</tr>
<tr>
<td>Females</td>
<td>1.7</td>
<td>0.002</td>
<td>43%</td>
</tr>
</table>

html table : is it possible to style inexistant cells or must I add them to the code?

context :
I work on a little csv-like script editor,
and I use an html table to display the data
As you can see there are missing cells.
That was to be expected, since I only insert the cells containing data
(and the previous cells of the row),
and the border style is only applied on cell elements for now.
I'd like All the cells to be displayed,
and I wondered if it was possible to draw the table lines
without cells, or if the only solution is to insert the missing cells ?
minimum reproducible example :
<table>
<tr>
<td>A1</td>
</tr>
<tr>
<td></td>
<td>B2</td>
</tr>
<tr>
<td></td>
<td></td>
<td>C3</td>
</tr>
</table>
<style>
table {
border-collapse: collapse;
border: 2px solid #555;
}
td {
border: 1px solid #888;
}
</style>
As we know elements who do now exist will not shown you have to add as needed to your table with no data in it. and you can set your code like below.
table {
border-collapse: collapse;
border: 2px solid #555;
empty-cells: show;
}
td {
border: 1px solid #888;
}
<table>
<tr>
<td>A1</td>
</tr>
<tr>
<td></td>
<td>B2</td>
</tr>
<tr>
<td></td>
<td></td>
<td>C3</td>
</tr>
</table>

Styling table borders with CSS

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>

How to style a table with only partly border spacing?

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>

HTML Table colspan rowspan

I have 6 columns & I want to use colspan for 3&4 and 5&6 column.
And I don't want to show 1st & 2nd column for that particular row.
How to hide 1&2 column in that row?
Desired output..
Remove the border from the cells you wish to "hide". They are still there, but visually absent.
Have a fiddle!
Experimental fiddle for a table { border: xxx; } fix.
In this example I am targeting the cell with tbody tr:first-child td:first-child. This could obviously also be targeted with a class.
HTML
<table>
<tbody>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
CSS
table {
border-collapse: collapse;
width: 500px;
}
td {
border: solid 1px #000;
padding: 10px;
}
tbody tr:first-child td:first-child {
border: none;
}
Use display:none; in your CSS File on the 1st & 2nd Column.
You can select them with:
td:nth-of-type(1),
td:nth-of-type(2) {
display:none;
}
Use visibility:hidden on the first two columns of the row.
Try to use visibility: hidden; empty-cells: hide; for that cells.
Here is a fiddle
HTML
<table border="1">
<thead>
<tr>
<td colspan="2" style="border: 0;">a1</td>
<td colspan="2">a3</td>
<td colspan="2">a5</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>5555</td>
</tr>
</tbody>
</table>
CSS
table {
border: none;
border-collapse: collapse;
}