I have a responsive table on my web page. This table contains three columns and a lot of rows. However, some rows contain only one column with colspan 3.
I need to have a table with two additional columns for large screens only so I need to modify colspan to 5.
Small and medium screens
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
Large screens - what I have
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
Large screens - what I want to have
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
It is not possible to modify colspan in CSS and it is not possible to have @media Queries from CSS in JavaScript. I don't want to have break points definiton in two places in my code. Additionaly, in JavaScript there is a problem with correct recognization of screen width because of scroll bars and different browser implementation of this (it may not corespond with the CSS break points, read more here).
Is there any possibility how to achieve this with CSS break points only? I need to have my design mobile first.
I have a responsive table on my web page. This table contains three columns and a lot of rows. However, some rows contain only one column with colspan 3.
I need to have a table with two additional columns for large screens only so I need to modify colspan to 5.
Small and medium screens
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
| xxxxx | xxxxx | xxxxx |
+---------+---------+---------+
Large screens - what I have
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
Large screens - what I want to have
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
| xxxxx | xxxxx | xxxxx | xxxxx | xxxxx |
+---------+---------+---------+---------+---------+
It is not possible to modify colspan in CSS and it is not possible to have @media Queries from CSS in JavaScript. I don't want to have break points definiton in two places in my code. Additionaly, in JavaScript there is a problem with correct recognization of screen width because of scroll bars and different browser implementation of this (it may not corespond with the CSS break points, read more here).
Is there any possibility how to achieve this with CSS break points only? I need to have my design mobile first.
Share Improve this question asked Feb 15, 2015 at 16:07 Knut HolmKnut Holm 4,1624 gold badges35 silver badges55 bronze badges2 Answers
Reset to default 7If you move borders from your table
to your td
s, you can use a constant 5 for your colspan. It won't create extra columns.
Changes to CSS:
table {
border-spacing: 0;
border-collapse: collapse;
}
table td {
border: 1px solid white;
}
Snippet 1
table {
border-spacing: 0;
border-collapse: collapse;
}
table td {
border: 1px solid white;
width: 80px;
height: 30px;
background-color: #99CCFF;
padding: 10px;
font-family: Calibri, sans-serif, Arial;
font-size: 20px;
text-align: center;
color: white;
}
table td[colspan] {
background: #5CAD5C;
}
table td[colspan]::before {
content:"3 columns, colspan=5";
}
table td.large-only {
display: none;
}
@media screen and (min-width: 1025px) {
table td.large-only {
display: table-cell;
}
table td[colspan]::before {
content:"5 columns, colspan=5";
}
}
<table>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td colspan="5"></td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
</table>
Snippet 2
Since you want to maintain the colspan
= 3 or 5, you can do that with a media query:
Updated HTML:
<td colspan="3">colspan=3</td>
<td colspan="5">colspan=5</td>
Updated CSS:
table td.large-only, table td[colspan="5"] {
display: none;
}
@media screen and (min-width: 1025px) {
table td.large-only, table td[colspan="3"] {
display: none;
}
table td.large-only, table td[colspan="5"] {
display: table-cell;
}
}
table {
border-spacing: 0;
border-collapse: collapse;
}
table td {
border: 1px solid white;
width: 80px;
height: 30px;
background-color: #99CCFF;
padding: 10px;
font-family: Calibri, sans-serif, Arial;
font-size: 20px;
text-align: center;
color: white;
}
table td[colspan] {
background: #5CAD5C;
}
table td.large-only, table td[colspan="5"] {
display: none;
}
@media screen and (min-width: 1025px) {
table td.large-only {
display: table-cell;
}
table td[colspan="3"] {
display: none;
}
table td[colspan="5"] {
display: table-cell;
}
}
<table>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3">colspan=3</td>
<td colspan="5">colspan=5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
</table>
It is possible to do that with JavaScript. What you only need is to have some class which adds some specific CSS to your element for your media query. Then you can detect changes of CSS by JavaScript and change the colspan. (inspiration has been taken here)
'use strict';
$(document).ready(function() {
// run test on initial page load
checkSize();
// run test on resize of the window
$(window).resize(checkSize);
});
// detect media query by css
function checkSize(){
if ($('td.large-only').css('display') === 'table-cell' ) {
$('td[colspan]').attr('colspan', '5');
$('td[colspan]').text('Colspan: 5');
} else {
$('td[colspan]').attr('colspan', '3');
$('td[colspan]').text('Colspan: 3');
}
}
table td {
width: 80px;
height: 30px;
background-color: #99CCFF;
padding: 10px;
font-family: Calibri, sans-serif, Arial;
font-size: 20px;
text-align: center;
color: white;
}
table td[colspan] {
background: #5CAD5C;
}
table td.large-only {
display: none;
}
@media screen and (min-width: 1025px) {
table td.large-only {
display: table-cell;
}
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
<tr>
<td class="large-only">1</td>
<td>2</td>
<td>3</td>
<td class="large-only">4</td>
<td>5</td>
</tr>
</table>
CodePen link