My employer has wanted me to take a table that we had on our site and make it more responsive. My solution was to make the table react as detailed in a previous question of mine (Responsive Table Display)
After I went through an implemented that design, I was informed they actually just want the 5 column table "squished" so that it retains all five columns but just has them fit within the viewport, specifically on mobile.
My HTML looks basically like this:
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td rowspan="2">1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
Being that I am completely self-taught and have been doing this for a few months now, what can I do to make the table compress its full width in mobile view?
If your table is contained within an element that is 100% of the screen's width you would set the table's width to 100%. If the table's container is not necessarily 100% of the screen's width you could set the table's width to 100vw. The vw unit represents a percentage of the total viewport width. It is not supported in IE8 or lower.
If their width is left unspecified, the columns will be automatically expanded to fit the table's width.
Using the media query from your previous question:
#media only screen and (max-width: 760px),(min-device-width: 768px) and (max-device-width: 1024px) {
table {
width: 100%; /* or 100vw depending on the rest of your HTML */
}
...
}
If you need to control the column widths, you can do that by either setting the th and tds to a fixed percentage width (that sums to 100), or by modifying the table-layout property. Setting your table to table-layout: fixed; will space all columns equally.
Related
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>
I am trying to make a table that allows scrolling both horizontally, but even though I specify the column width in the headers, it shrinks them down to the div size.
I want it to overflow to the right, and allow scrolling.
Here is a link to a jsfiddle test with something that acts like my current implementation, but here is a code snippet that runs the same code:
.mainframe {
overflow: hidden;
position: fixed;
background-color: blue;
height:200px;
width:500px;
}
.wrapper {
margin: 5%;
width: 90%;
height: 90%;
background-color: red;
}
.allow-scroll {
position: relative;
height: 100%;
width: 100%;
overflow-y: scroll;
overflow-x: scroll;
background-color: lime;
}
<div class="mainframe">
<div class="wrapper">
<div class="allow-scroll">
<table>
<tr>
<th style="width:200px">Long name item 1</th>
<th style="width:300px">Even longer name item 2</th>
<th style="width:400px">Super crazy long item 3 need more words</th>
<th style="width:200px">Short item 4</th>
<th style="width:300px">Somewhat long time 5</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</div>
</div>
</div>
Additionally, could someone explain why the "margin 5%" doesn't work at the bottom, and what I could do about that?
You could add a width to the table so that it had horizontal scroll.
table {
width:1400px;
}
A better option is to use the table-layoutproperty.
table {
width:100%;
table-layout:fixed;
}
The margin issue you are having is caused by the height on the .mainframe element.
Remove the height: 200px;
If you need the height on the .mainframe element and you know it will be a fixed height, you just need to decrease the height of the .wrapper element. You can use an exact pixel height. I used a percentage below.
.wrapper { height: 75%; } // This percentage looks about right.
It only happens on Chrome, and at the beginning, the table displays well. However,when I resize it into small and then change back to big screen, the table is messed up. This resizing issue make it not perfectSee the gif image and code below. Basically, I want to the table to display one element in a line when it comes to small screen. If it is a Chrome bug, is there any alternative solutions? Thanks.
#media screen and (max-width: 600px) {
.responsive-table-md tbody td {
display: block;
}
}
I am wondering if there is way to fix it. If it is Chrome bug, is there any alternative to do the table staff.
<table class="responsive-table-md">
<thead>
<tr>
<th>Test 1</th>
<th>Test 2</th>
<th>Test 3</th>
<th>Test 4</th>
<th>Test 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
</tr>
</tbody>
</table>
According to the article, the whole set of table elements should be reset, if you only reset the td, chrome behaves weird.
The biggest change is that we are going to force the table to not behave like a table by setting every table-related element to be block-level.
With the following, it does not behave strange:
#media screen and (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
}
Here is the complete change, I slightly modified the CSS from the article.
I am trying to create this layout with tables in HTML using rowspan and colspan but the size of a cell is not showing how I expected, the value of rowspan and colspan of the cell "X" are "2" but the tables are created like it where 2x1.
Here is my code
<html>
<head>
</head>
<body>
<table border="3" cellspacing="5" cellpadding="3">
<tbody>
<tr>
<td rowspan="2">1</td>
<td>2</td>
<td rowspan="2" colspan="2">X</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td rowspan="1" colspan="4">4</td>
</tr>
<tr>
<td colspan="3">5</td>
<td>6</td>
</tr>
</tbody>
</table>
</body>
This is working properly. It's easiest to see if you add a row that has all four cells occupying a single column:
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
http://jsfiddle.net/ExplosionPIlls/HfHBU/
You're facing a visual problem caused by the there is no third column cell to create any width. The entire third column is created by colspans on X, 4, and 5, so it will appear to have no (or very little) width, and the cells will not expand without width rules if they don't have to.
I have a table, constructed like this:
<table>
<tr> <td>1</td> <td>2</td> <td rowspan="4">3</td></tr>
<tr> <td>4</td> <td>5</td> </tr>
<tr> <td>6</td> <td>7</td> </tr>
<tr> <td>8</td> <td>9</td> </tr>
<tr height="100"><td colspan="2">10</td><td class="eleven">11</td> </tr>
</table>
Now the problem is within the last row. Whole row has a height set to 100px, so there is a plenty of room in TDs. In the very last TD I want to set an individual padding, so only the content "11" is padded from the top:
.eleven {
padding-top:15px;
}
Setting this causes the problem - the first TD in this row also gets padding-top:10px; Why and how to make only the 2nd one padded?
Why don't you wrap the content you want to be padded into a <div> (onto which you will be applying the padding style) and put that <div> into the <td>?
<td>
<div style="padding-top: 15px;">
Content
</div>
</td>
Ok, I found out what caused the problem. It was an entry in a little html5-css-reset snippet I use:
vertical-align:baseline;
assigned generally to most of common elements. Having that in mind, now everything works as supposed to.