How to control the width of - tr:nth-child(odd) - html

I am looking at using tr:nth-child(odd) to create a style for my datasheets. the code is below:
Now, my only real question is how do I make it so that I adjust the width? I want it to fill a space that is 200px by 400px
The depth isn't an issue since each row will dictate the depth, but the length does matter. The code I am using now only center aligns it, and I can't seem to find a way to stretch the fields. I know I am missing something, any help would be greatly appreciated.
tr:nth-child(odd) {
background: #4D4D4D}
<div class="black">
<table class="test2">
<tr>
<td>Text</td>
<td>Text2</td>
<td>Text3</td>
<td>Text4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>13</td>
</tr>
</table>
</div>

CSS Tricks Explanation on TR widths
The only options would be to:
Increase the columns that the row takes up. This could be very fidgety
OR
Do something similar to this example here: Semantic replacement for a table
As someone who prefers semantic markup, and enjoyable websites... I would choose option 2.

HTML
body {
font: normal medium/1.4 sans-serif;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 0.25rem;
text-align: left;
border: 1px solid #ccc;
}
tbody tr:nth-child(odd) {
background: #4D4D4D;
}
<div class="black">
<table class="test2">
<thead>
<tr>
<th>Text</th>
<th>Text2</th>
<th>Text3</th>
<th>Text4</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</tbody>
</table>
</div>

Related

Rowspan at the end of the table is working differently

I've created a simple table using html and a bit of bootstrap, but the last rowspan doesn't work as I thought it will, here's code:
I wanted 4 red-marked cells to be one, so I've replaced first <td>group1</td> with <td rowspan="4">group1</td> and removed remaining 3 <td>group1</td> but it has messed up whole table.
Also it is placed it <div class="col-lg-7 mb-4"> div, but I've also tried without any div - the effect was the same. I'm not sure what is causing that problem, considering that the rest rowspans is working just fine.
/* I don't think CSS is needed, but just in case: */
table.table-bordered {
border: 1px solid #2f8dff!important;
box-shadow: 0px 0px 100px 0px #2f8dff;
margin-top: 20px;
text-transform: uppercase;
font-size: 12px;
color: white;
}
table.table-bordered>thead>tr>th {
border: 1px solid #2f8dff!important;
}
table.table-bordered>tbody>tr>td {
border: 1px solid #2f8dff!important;
}
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>mon</th>
<th>tue</th>
<th>wed</th>
<th>thu</th>
<th>fri</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">17:00-18:00</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td rowspan="2">18:00-19:00</td>
<td>1</td>
<td rowspan="2">group3</td>
<td>3</td>
<td>4</td>
<td rowspan="2">group3</td>
</tr>
<tr>
<td rowspan="2">group2</td>
<td>8</td>
<td rowspan="2">group2</td>
</tr>
<tr>
<td rowspan="2">19:00-20:00</td>
<td rowspan="4">group1</td>
<td rowspan="2">group1</td>
<td>group1</td>
</tr>
<tr>
<td rowspan="3">group1</td>
<td rowspan="3">group1</td>
<td>group1</td>
</tr>
<tr>
<td rowspan="2">20:00-21:00</td>
<td>3</td>
<td>group1</td>
</tr>
<tr>
<td>7</td>
<td>group1</td>
</tr>
</tbody>
</table>
What shall I do?
Thanks in advance!
I don't really know what you mean by "messed up whole table". After adding the row-span="4" and removing the three following td tags, the table looked just fine for me:
The only thing I can see is the changing height of that table cell. This can be prevented by adding this CSS:
tr {
height: 1rem;
}
It makes every row equal height and produces following result:

CSS class styles affecting HTML elements outside of class definition

Style properties defined under one class affects the properties of another element with a different class definition.
Specifically the styles for the th and td from the Movement table are overwriting the ones defined in the Characteristics table. Inspecting the styles within Chrome it clearly shows that the Movement styles are trumping the Characteristic settings.
A working example of the code is below. It can run at this link from w3chools.com.
I've tried numerous variations and have been unable to correct it. Checked here and other sites for solutions but to no avail.
I'd really like to know what I'm doing wrong. Thanks.
.grid-container-0 {
display: grid;
grid-gap: 1px;
grid-template-columns: auto auto 5px auto;
width: 200mm;
border: solid black thin;
padding: 0px;
background-color: white;
}
.grid-container-2 {
display: grid;
grid-gap: 1px;
border: none;
padding: 2px;
background-color: white;
}
.grid-container-5 {
display: grid;
grid-gap: 1px;
border: none;
padding: 1px;
background-color: white;
}
.title {
padding: 2px;
text-align: left;
font-size: 12pt;
font-weight: bold;
color: white;
background-color: black;
}
div.characteristics {
grid-row: 2;
grid-column: 1 / span 2;
}
.characteristics th,
td {
padding: 2px;
text-align: center;
font-size: 10pt;
}
.characteristics th:last-of-type,
td:last-of-type {
text-align: left;
}
.characteristics tr:last-of-type {
font-weight: bold;
}
div.movement {
grid-row: 4;
grid-column: 2;
}
.movement th,
td {
margin: 0px;
padding: 0px 2px;
text-align: left;
font-size: 8pt;
}
<body>
<div class="grid-container-0">
<div class="grid-item characteristics">
<div class="grid-container-2">
<div class="grid-item characteristics-1 title">CHARACTERISTICS</div>
<div class="grid-item characteristics-2">
<table>
<tr>
<th>Val</th>
<th>Char</th>
<th>Points</th>
<th>Roll</th>
<th>Notes</th>
</tr>
<tr>
<td>10</td>
<td>STR</td>
<td>0</td>
<td>11-</td>
<td>HTH Damage 2d6 END [1]</td>
</tr>
<tr>
<td>10</td>
<td>DEX</td>
<td>0</td>
<td>11-</td>
<td></td>
</tr>
<tr>
<td>10</td>
<td>CON</td>
<td>0</td>
<td>11-</td>
<td></td>
</tr>
<tr>
<td>10</td>
<td>INT</td>
<td>0</td>
<td>11-</td>
<td>PER Roll 11-</td>
</tr>
<tr>
<td>10</td>
<td>EGO</td>
<td>0</td>
<td>11-</td>
<td></td>
</tr>
<tr>
<td>10</td>
<td>PRE</td>
<td>0</td>
<td>11-</td>
<td>PRE Attack: 2d6</td>
</tr>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td>3</td>
<td>OCV</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>DCV</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>OMCV</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>DMCV</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>SPD</td>
<td>0</td>
<td></td>
<td>Phases: 6, 12</td>
</tr>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td>2</td>
<td>PD</td>
<td>0</td>
<td></td>
<td>2 PD (0 rPD)</td>
</tr>
<tr>
<td>2</td>
<td>ED</td>
<td>0</td>
<td></td>
<td>2 ED (0 rED)</td>
</tr>
<tr>
<td>4</td>
<td>REC</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>20</td>
<td>END</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>10</td>
<td>BODY</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>20</td>
<td>STUN</td>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="3"> </td>
<td>0</td>
<td>Total Characteristic Points</td>
</tr>
</table>
</div>
</div>
</div>
<div class="grid-item movement">
<div class="grid-container-5">
<div class="grid-item movement-1 title">MOVEMENT</div>
<div class="grid-item movement-2">
<table>
<tr>
<th>Type</th>
<th>Total</th>
</tr>
<tr>
<td>Run</td>
<td>12m[24m NC]</td>
</tr>
<tr>
<td>Swim</td>
<td>4m[8m NC]</td>
</tr>
<tr>
<td>H. Leap</td>
<td>4m</td>
</tr>
<tr>
<td>V. Leap</td>
<td>2m</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</body>
When you write .characteristics th, td in your CSS, you're apparently expecting that to apply to .characteristics th and .characteristics td elements.
However, that isn't what it means: it actually applies to .characteristics th elements and all td elements. So that's why you're seeing styles intended for one class in places they shouldn't be.
Here's an example:
.red i, b {color:red}
.blue i, b {color:blue}
<div class="red">1 <i>2</i> <b>3</b></div>
<div class="blue">4 <i>5</i> <b>6</b></div>
You'd expect 2 and 3 to be red, and 5 and 6 to be blue, but that doesn't actually happen: both the color:red and color:blue rules apply to all b elements, so they'll end up the same color as each other (in this case, blue).
To fix this, .characteristics th, td has to be written as .characteristics th, .characteristics td (and likewise with other places where you've used commas).

CSS only scrollable table body [duplicate]

This question already has answers here:
Fixed Table Header using CSS
(3 answers)
Closed 3 years ago.
I want to use CSS to scroll only the table body. I found a few examples but they don't work well if the table rows length is different.
I started with this example:
https://codepen.io/pdg/pen/MayBJK
HTML code:
<div class="container">
<h1>CSS only scrollable table body</h1>
<p>by PDG</p>
<table class="table-scroll small-first-col">
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
</tr>
</thead>
<tbody class="body-half-screen">
<tr>
<td>1</td>
<td>First row</td>
<td>3 dsad sad sad sad sad sadsadsad sadasd asdsa</td>
<td>4 dasd sad asdsad sad sadsa dasdsad</td>
</tr>
<tr>
<td>1 dasd asd sadsadsad sadsad</td>
<td>2dsadsadsa dsad sadasd sad sad sa</td>
<td>A very long cell content comes here, just to test it all!!!</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2 dsad asd asd sad sad sad asdsadsad</td>
<td>3</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>There is an empty cell above!</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr><tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td> Last row</td>
</tr>
</tbody>
</table>
<h3>Notes</h3>
<p>The table uses flex properties to arrange the cell sizes, however, this tends to make them the same as "fixed width". To fix that, you need to create additional classes that target the <strong>td:nth-child()</strong> and <strong>th:nth-child()</strong> with a specific <strong>flex-basis</strong> property.</p>
<p>The <strong>height</strong> of the body also must be fixed and cannot be percentages (in the example above I use "vh")</p>
</div>
CSS code:
.container{
padding: 1rem;
margin: 1rem;
}
.table-scroll{
/*width:100%; */
display: block;
empty-cells: show;
/* Decoration */
border-spacing: 0;
border: 1px solid;
}
.table-scroll thead{
background-color: #f1f1f1;
position:relative;
display: block;
width:100%;
overflow-y: scroll;
}
.table-scroll tbody{
/* Position */
display: block; position:relative;
width:100%; overflow-y:scroll;
/* Decoration */
border-top: 1px solid rgba(0,0,0,0.2);
}
.table-scroll tr{
width: 100%;
display:flex;
}
.table-scroll td,.table-scroll th{
flex-basis:100%;
flex-grow:2;
display: block;
padding: 1rem;
text-align:left;
}
/* Other options */
.table-scroll.small-first-col td:first-child,
.table-scroll.small-first-col th:first-child{
flex-basis:20%;
flex-grow:1;
}
.table-scroll tbody tr:nth-child(2n){
background-color: rgba(130,130,170,0.1);
}
.body-half-screen{
max-height: 50vh;
}
.small-col{flex-basis:10%;}
It seemed to be a good idea and in their demo everything looks fine, but on a more complex example you can see that the columns are not vertical aligned very well. See my example below:
https://jsfiddle.net/pnfu321g/
I also tried a different example: http://jsfiddle.net/hashem/CrSpu/555/
But this also doesn't work well in my case, on complex tables with rows that have different widths.
Please suggest a css solution (no javascript) that will make a table with rows of any size to look good.
UPDATE
The reason why other solutions didn't worked for me was because I was using an older version of Mozilla Firefox - 57. And that's why almost evrything that I tested didn't worked. I marked the questions as duplicate.
You can use position:sticky on the th elements within the table head.
Basic example (CSS):
table.sticky-headers { width:100%; }
table.sticky-headers thead tr th { text-align:left; position:sticky; top:0; background:#fff; }
Basic markup (Add more rows to get scrollbars):
<table class="sticky-headers">
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
</tbody>
</table>
Working example here: http://jsfiddle.net/yp1xb7dj/
If you want fixed cell width and the text in cells with more content to break and fall on two-three lines, you can try specifying the cell width and the height will be fixed automatically:
table.sticky-headers thead tr th {
width: 20%;
text-align:left;
position:sticky;
top:0;
background:#fff;
}
The result:

Centering a column in a table

I am trying to center just certain columns in a table but I am having issues. I know in the past you would just simply apply inline styles to each TD but there has to be a better way.
Here is a simple example:
.centerText{
text-align:center;
}
<table border="1">
<col>
<col class="centerText">
<thead>
<tr>
<th>heading1</th>
<th>heading2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
With that class I am trying to center the text inside. I know applying css to the col will work for changing background color for the column and text color and such, but I am not sure how I would use it to center a column. I am assuming because I need to center the contents of the td and this is probably just centering the TD element itself; which is already 100 percent.
I understand I can just say apply the css to the 5th TD in this TR but that seems fragile.
Also, bonus points if you can show me how to change the width of a column this way. I used the width attribute for col but that is deprecated in html 5 (even though it is still currently supported.
Done, your class wasn't used anywhere
tr td:nth-child(2) {
text-align:center;
}
<table border="1">
<thead>
<tr>
<th>heading1</th>
<th>heading2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td >2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
I removed:
<col>
<col class="centerText">
and
.centerText{
text-align:center;
}
Because col doesn't mean anything and you didn't close the tags.
CSS
table {
border-collapse: collapse;
width: 100%;
}
td {
text-align: center
}
If you want to align your all td content to the center
add the centerText class to your table
<table class="centerText" border="1">
It's not completely clear what you want, but if you want to center the contents of a certain column, you can just use this CSS rule:
td:nth-child(2) {
text-align:center;
}
In this example it applies to the second column, but you could define that for any column. It works since the td are always children of a tr, so you can use the nth-child selector.
td:nth-child(2) {
text-align: center;
}
<table border="1">
<col>
<col class="centerText">
<thead>
<tr>
<th>heading1</th>
<th>heading2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>

when changing image-size td-size won't change

I have made a table with three columns, two with text and one with a picture. The table has 10 rows, so I gave the the cell with the picture a rowspan of 10. I am able to change the width of the picture, but for some odd reason the table-cell won't change it's width. The cell still have the width, the picture originally had.
I have tried setting the image to block, but that doesn't help with the td-width. I could give the td a class with a max-width, but I am looking for another solution.
example
HTML
<table>
<thead>
<tr><th colspan="2">Attribute</th></tr>
</thead>
<tbody>
<tr>
<td>Stufe</td>
<td>9</td>
<td rowspan="10"><img src="#"></img></td>
</tr>
<tr>
<td>Vitalität</td>
<td>12</td>
</tr>
<tr>
<td>Zauberei</td>
<td>10</td>
</tr>
<tr>
<td>Kondition</td>
<td>11</td>
</tr>
<tr>
<td>Belastung</td>
<td>15</td>
</tr>
<tr>
<td>Stärke</td>
<td>13</td>
</tr>
<tr>
<td>Geschicklichkeit</td>
<td>12</td>
</tr>
<tr>
<td>Intelligenz</td>
<td>9</td>
</tr>
<tr>
<td>Glaube</td>
<td>9</td>
</tr>
<tr>
<td>Glück</td>
<td>7</td>
</tr>
</tbody>
</table>
CSS (nothing special)
table {
font-size:90%;
margin:1.5em auto;
padding:0 1em;
border-collapse:collapse;
text-align:center;}
article table thead {
background:#91414B;}
article table th, main article table td {
padding:0.25em 0.5em;}
table img {
display:block;
width:50%;}
table a {
text-decoration:underline;
color:#FFFFFF;}
table a:hover {
text-decoration:none;
color:#C6C6C6;}