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>
Related
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:
I have a working table with a scrolling tbody by using css with the following within it.
tbody{
overflow:auto;
}
thread > tr, tbody{
display:block;
}
<table>
<thread>
<tr>
<td>Item</td>
<td>SerialNm</td>
</tr>
</thread>
<tbody style="height:50px">
<tr>
<td>1</td>
<td>12345</td>
</tr>
<tr>
<td>2</td>
<td>12345</td>
</tr>
<tr>
<td>3</td>
<td>12345</td>
</tr>
</tbody>
</table
I am happy with this.
However on other more complex tables I am having to use tbody for specific formatting to get the required affect.
The issue I have is that doing the CSS above applies to all my tables everywhere, and I only want it for the tbody within this specific table.
I know that this means I need to make a class, however I have tried this;
.tbody-scroll, tbody{
overflow:auto;
}
thread > tr, tbody-scroll{
display:block;
}
and obviously assigned the tbody in the table to have a class="tbody-scroll", and it does not behave in the same way, it shows the entire table instead of preventing the overflow and going to a scrollbar.
How do I create another instance of tbody with all these settings in to allow scrolling without affecting the 'default' tbody settings?
Just to show you the suggested solution, applied to your situation:
#specificTable tbody{
overflow:auto;
}
#specificTable thread > tr, tbody{
display:block;
}
<p> Specific table example</p>
<table id="specificTable">
<thread>
<tr>
<td>Item</td>
<td>SerialNm</td>
</tr>
</thread>
<tbody style="height:50px">
<tr>
<td>1</td>
<td>12345</td>
</tr>
<tr>
<td>2</td>
<td>12345</td>
</tr>
<tr>
<td>3</td>
<td>12345</td>
</tr>
</tbody>
</table>
<p> Other table example</p>
<table id="otherTable">
<thread>
<tr>
<td>Item</td>
<td>SerialNm</td>
</tr>
</thread>
<tbody style="height:50px">
<tr>
<td>1</td>
<td>12345</td>
</tr>
<tr>
<td>2</td>
<td>12345</td>
</tr>
<tr>
<td>3</td>
<td>12345</td>
</tr>
</tbody>
</table>
I am HTML/CSS beginner attempting to create calendar view very similar to Microsoft Outlook's design as shown below:
So far, I gotten the basic outline of the calendar with the following HTML/CSS:
<html>
<head>
<title>August, 2016</title>
</head>
<body>
<center><h1>August, 2016</h1></center>
<style>
table{
table-layout: fixed;
}
</style>
<table border="1" width="1250" height="800">
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thur</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<table>
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
Claim Benefits<br>
Pick up groceries<br>
Iron the shirts<br>
+5 more...
</td>
</tr>
</table>
</td>
<td>2</td>
</tr>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tr>
<td>3</td>
<td>4</td>
<td>
<table>
<tr>
<td>5</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
Claim Benefits<br>
Pick up groceries<br>
Iron the shirts<br>
+5 more...
</td>
</tr>
</table>
</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tr>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tr>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tr>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
</body>
</html>
Which produces:
I want to accomplish some of the same things Outlook does:
The day of the month number should be on the top left corner of the cell. I attempted to create this with a table inside the cell but it doesn't quite feel or look right. I am directly attempting to recreate Outlook's view and the way they setup the day number and reminders below it.
Each of the cells should maintain the same size while containing up to 4 hyperlinks. In the above code, the cells of the row containing hyperlinks become bigger than the cells of the other rows. I want to fix the cell size such that all cells are the same size whether they contain up to 4 hyperlinks or not.
Thanks for any helpful code or suggestions.
An easy way to position/style the calendar numbers separately from the content is to make them td pseudo-elements. You could set the content to something like attr(data-day) to have it dynamically display a number set in the HTML.
To make sure the cells have the same height, just specify a height in the CSS. Since you only want "up to 4 hyperlinks", you can just check what the height is for a cell with 4 links and then use that.
By the way, it looks like there are a lot of errors with the tr tags in your code, and I would recommend representing the listed elements inside an individual day as a list rather than as nested tables (the calendar is tabular data, but the contents of the day are a to-do list).
Here's a snippet that includes the recommended changes:
table {
table-layout: fixed;
width: 1250px;
height: 800px;
}
table td {
height: 100px;
position: relative;
}
table td:before {
content: attr(data-day);
display: block;
position: absolute;
top: 0;
left: 0;
background: yellow;
}
<center>
<h1>August, 2016</h1>
</center>
<table border="1">
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thur</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td data-day="1">
<ul>
<li>Claim Benefits</li>
<li>Pick up groceries</li>
<li>Iron the shirts</li>
<li>+5 more...</li>
</ul>
</td>
<td data-day="2"></td>
</tr>
<tr>
<td data-day="3"></td>
<td data-day="4"></td>
<td data-day="5"></td>
<td data-day="6"></td>
<td data-day="7"></td>
<td data-day="8"></td>
<td data-day="9"></td>
</tr>
<tr>
<td data-day="10"></td>
<td data-day="11"></td>
<td data-day="12">
<ul>
<li>Claim Benefits</li>
<li>Pick up groceries</li>
<li>Iron the shirts</li>
<li>+5 more...</li>
</ul>
</td>
<td data-day="13"></td>
<td data-day="14"></td>
<td data-day="15"></td>
<td data-day="16"></td>
</tr>
<tr>
<td data-day="17"></td>
<td data-day="18"></td>
<td data-day="19"></td>
<td data-day="20"></td>
<td data-day="21"></td>
<td data-day="22"></td>
<td data-day="23"></td>
</tr>
<tr>
<td data-day="24"></td>
<td data-day="25"></td>
<td data-day="26"></td>
<td data-day="27"></td>
<td data-day="28"></td>
<td data-day="29"></td>
<td data-day="30"></td>
</tr>
<tr>
<td data-day="31"></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
I think You can add a flot:left; style to this month number, and maybe margin,padding:0;
I suggest to use min-height with the same value as height of all cells
First, you have not added any styling so you are leaving that up to the browser's defaults. Also, the center tag is deprecated. Use this instead:
h1 {text-align:center;}
For the table, you can add this to make the border appear like Outlooks' borders:
table, th, td {border-collapse: collapse;}
To style the table data remove the padding and margin. That should move the first td in the first tr to the top left corner.
Lastly, add a specific height to the row of each week. I would assign that tr a class of week and style with whatever height you want:
tr.week {height:200px;}
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;}
I want to set style for <td> tag, I am designing table in a page. I want to give different styles for upper and lower cells, is that possible to set <style> which only do work on down side <td> tags.
Please Help.
Yes, you either give a class to lower <td> tags and then inside <style> you can set their style. or you can use this css selector without giving your <td> elements a class.
Here is a representation of what I just said:
<table id="table1">
<tr>
<td>1</td>
<td class="lowertd">2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td class="lowertd">2</td>
<td>6</td>
</tr>
</table>
<style>
#table1 tr td:last-child {
background-color: green;
}
.lowertd {
background-color: silver;
}
#table1 tr td:nth-child(1) {
background-color: blue;
}
</style>
For your requirement you need to create a css class and reference it by
<td class="someclass" ........... >
although you want to add this class, only for elements for which you need styling.
and in CSS under <style> , define css attributes. Something like below
<style>
.someclass{
property: value;
.
.
.
}
</style>
Hope this solves your problem.
I got the answer...
Just set the particular rows in any tag like <thead>, <tbody> or <tfoot>
<table id="table1">
<thead>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>6</td>
</tr>
</tbody>
</table>
then set style
<style>
table thead td
{
border:none;
}
</style>
You can style the first 10 <td> tags differently by using the nth-of-type selector and then setting a style for those elements:
td:nth-child(-n+10) { text-decoration: underline; }
jsfiddle