Is it possible to split css table into multiple columns? - css

I'm trying to make a responsive table, but I have no idea about how can I achieve this. After googling, could not find a solution.
So, I have this table in desktop view:
1 2 3
I B C D
II C D E
III D E F
IV E F G
And I'm trying to get this in responsive view:
1
I B
II C
III D
IV E
2
I C
II D
III E
IV F
etc..
Any kind of help would be appreciated

There are several available, responsive table solutions including this simple, CSS-only pattern:
data-label attribute :
First, we’ll add a data-label attribute to each data cell with a value that represents that column’s name. That will be used for labeling purposes in the responsive layout.
In smaller viewports the and elements will display as block-level and not as table rows and cells. And the ::before pseudo-class now serves as a label.
body {
font-family: "Open Sans", sans-serif;
line-height: 1.25;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 1.5em;
margin: .5em 0 .75em;
}
table tr {
background-color: #f8f8f8;
border: 1px solid #ddd;
padding: .35em;
}
table th,
table td {
padding: .625em;
text-align: center;
}
table th {
font-size: .85em;
letter-spacing: .1em;
text-transform: uppercase;
}
#media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: .625em;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: .8em;
text-align: right;
}
table td::before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
}
table td:last-child {
border-bottom: 0;
}
}
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="ID">1</td>
<td data-label="Name">Jake</td>
<td data-label="Age">23</td>
</tr>
<tr>
<td data-label="ID">2</td>
<td data-label="Name">Dave</td>
<td data-label="Age">45</td>
</tr>
</tbody>
</table>

Related

CSS classes overlapping in an unintended way

What I'm trying to achieve is that when hovering over the td, that the background changes to black and the text color to white, but I only get the black background. I believe this happens because there is an interference with the "blueColor" and "redColor" classes with the "hover:hover" class.
td {
text-align: center;
}
table {
width: 100%;
font-size: 35px;
font-family: Arial;
font-weight: bold;
}
.blackBorder {
border: 1px solid black;
}
p.redColor {
color: red;
}
p.blueColor {
color: blue;
}
.lightGreenBackground {
background-color: #b5f79e;
}
.hover:hover {
background-color: black;
color: white;
}
<table>
<tr>
<td class="lightGreenBackground blackBorder hover">
<p class="redColor">1</p>
<p class="blueColor">H</p>
</td>
</tr>
</table>
I have tried using the !important property without any success and I'm not allowed to use id's.
Because you already have a color defined on p and not td.hover means you will have to change the color of <p> when td.hover is in a hovered state.
You'll notice if you remove both colors from your CSS, the text will turn white as expected on hover.
You can target both p's and change the color using .hover:hover > p.
td {
text-align: center;
}
table {
width: 100%;
font-size: 35px;
font-family: Arial;
font-weight: bold;
}
.blackBorder {
border: 1px solid black;
}
p.redColor {
color: red;
}
p.blueColor {
color: blue;
}
.lightGreenBackground {
background-color: #b5f79e;
}
.hover:hover {
background-color: black;
}
.hover:hover > p {
color: #fff;
}
<table>
<tr>
<td class="lightGreenBackground blackBorder hover">
<p class="redColor">1</p>
<p class="blueColor">H</p>
</td>
</tr>
</table>

CSS for Table Header with auto-sized trailing underline

Using only HTML and CSS,
I wish to have a table like the following:
Header1 Header2
-------------- --------------------
L1Col1 content L1Col2 wider content
L2Col1 datum L2Col2 datum2
Where the underline automatically sizes to the
table column width.
Help!
Try this:
CSS:
.sample-table {
width: 50%;
text-align: left;
border-spacing: 25px 0;
}
.sample-table th {
border-bottom: 1px dashed #000;
}
.sample-table td,
.sample-table th {
padding: 3px;
}
HTML:
<table class="sample-table">
<tr>
<th>Header1</th>
<th>Header2</th>
</tr>
<tr>
<td>L1Col1 content</td>
<td>L1Col2 wider content</td>
</tr>
<tr>
<td>L2Col1 datum</td>
<td>L2Col2 datum2</td>
</tr>
</table>
Azu,
Thanks for your answer.
The "border-spacing" usage was completely enlightening to me.
I modified the CSS to the following:
.sample-table {
text-align: left;
border-spacing: 25px 0;
}
.sample-table th {
border-bottom: 2px solid #000;
}
.sample-table td,
.sample-table th {
padding: 3px 0px;
}
This gives me exactly what I wanted.

Table row background-color not filling container in Edge/Chrome

I have two tables, side by side, surrounded by a shared border. The last row in the first table contains a list with dynamically generated data (usually one word each). The second cell in the second table contains a list with dynamically generated data (usually 31 words).
In
Firefox, the table row color (light blue) for the first table extends down to the shared border.
Whereas, in Edge/Chrome, the table row color (light blue) for the first table does not extend down to the shared border.
Is this a difference in how Firefox styles tables? Or am I missing a property in my code? I used normalize.css, if that makes a difference.
<style>
/* general styles */
#generate {
margin-top: 15%;
height: 100%;
text-transform: capitalize;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
}
/* button styles */
#button-generate {
background-color: #76cef2;
font-size: 8rem;
text-align: center;
text-shadow: 5px 5px 8px #93aef9;
border: 6px solid #58efec;
border-radius: 10px;
padding: 20px 100px 20px 100px;
}
#button-generate:active {
background-color: #a29dfc;
text-shadow: 5px 5px 8px #76cef2;
}
/* table styles */
.table {
margin: 5%;
font-size: 1.8rem;
text-align: left;
text-shadow: 1px 1px 2px #b18dff;
border: 4px solid #000000;
border-collapse: collapse;
border-spacing: 0;
border-radius: 10px;
display: flex;
flex-direction: row;
table-layout: auto;
width: auto;
}
.table tr:nth-child(even) {
background-color: #93aef9;
}
.table tr:nth-child(odd) {
background-color: #85bef6;
}
.table td {
border: 4px solid #a29dfc;
padding: 2%;
}
.table td:focus {
font-weight: bold;
}
#purple-background-bold {
background-color: #93aef9;
font-weight: bold;
}
/* list styles */
.list {
list-style-type: none;
padding: 0;
line-height: 1.6;
}
.list li:focus {
font-weight: bold;
}
#two-column-list {
columns: 3;
-webkit-columns: 3;
-moz-columns: 3;
}
#bold {
font-weight: bold;
}
</style>
<div class="table">
<table v-show="isGenerated">
<tr>
<td id="bold">Cat1:</td>
<td>{{ stuff1 }}</td>
</tr>
<tr>
<td id="bold">Cat2:</td>
<td >{{ stuff2 }}</td>
</tr>
<tr>
<td id="bold">Cat3:</td>
<td>{{ stuff3 }}</td>
<ul class="list">
<li>Cat4: {{ stuff4 }}</li>
<li>Cat5: {{ stuff5 }}</li>
<li>Cat6: {{ stuff6 }}</li>
</ul>
</td>
</tr>
</table>
<table v-show="isGenerated">
<tr>
<td id="purple-background-bold">Generated List</td>
<td>
<ul class="list" id="two-column-list">
<li v-for="(item, index) in listItems" :key="index">{{ index + 1 }}. {{ item }}</li>
</ul>
</td>
</tr>
</table>
</div>

Using CSS, apply padding to all td elements, except those with input or select children [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 5 years ago.
The title says it all:
Using only CSS, How can I apply some padding to all td elements, except those with children elements of input or select types, which I'd like to have another padding for?
Note that I'd like as generic solution as possible (e.g., no hardcoded inline style), so the first thing that came in mind was to try and do this:
td:not(:has(input, select)), but it's not possible.
For example, in the following table (here's also a fiddle), I'd like all td elements to have padding-left and padding-right of 20px, but those with input or select elements to be with padding of 2px:
table {
width: 100%;
border-collapse: collapse;
}
td {
text-align: center;
vertical-align: middle;
white-space: nowrap;
border: 1px solid #ddd;
font-size: 16px;
font-family: arial;
padding-right: 20px;
padding-left: 20px;
}
input, select {
text-align: center;
vertical-align: middle;
font-size: 16px;
}
.fit {
white-space: nowrap;
width: 1%;
}
<table id="myTable" class="fit">
<tr>
<td>some</td>
<td>nice</td>
<td>text</td>
</tr>
<tr>
<td>
<input value="some"/>
</td>
<td>nice</td>
<td>
<select>
<option>option</option>
</select></td>
</tr>
</table>
You could apply a negative margin to input and select.
Subtract a total of 36px to give you 2px each side
table {
width: 100%;
border-collapse: collapse;
}
td {
text-align: center;
vertical-align: middle;
white-space: nowrap;
border: 1px solid #ddd;
font-size: 16px;
font-family: arial;
padding-right: 20px;
padding-left: 20px;
}
input,
select {
text-align: center;
vertical-align: middle;
font-size: 16px;
margin-left: -18px;
margin-right: -18px;
}
.fit {
white-space: nowrap;
width: 1%;
}
<table id="myTable" class="fit">
<tr>
<td>some</td>
<td>nice</td>
<td>text</td>
</tr>
<tr>
<td>
<input value="some" />
</td>
<td>nice</td>
<td>
<select>
<option>option</option>
</select></td>
</tr>
</table>

Fitting width and height of html page contents to screen

I have created a table with rows in html and defined it's width and height parameters in css using pixel(px).
I'm using a 15.6" display pc to run it and everything goes well, but when i open the page in smaller screen pc, the table exceeds the screen margin.
Which unit should I use to define the width & height parameters so that, it will always fit the display of any media screen that opens the page?
I always use Width: 1-100vw and height: 1-100vh; on everything (<p>,<div>,font-size etc.)
You will also need to add in min-height: 100vh; and min-width: 100vw; to the html,body css. else it won't work.
This WILL ensure that it works properly. I used to use %, but in the end it didn't work out with scaling to the re-size ^^
See this for more info: https://css-tricks.com/viewport-sized-typography/
You could set the width of the body to 100%. Another suggestion is to use
1vw = 1% of viewport width
1vh = 1% of viewport height
1vmin = 1vw or 1vh, whichever is smaller
1vmax = 1vw or 1vh, whichever is larger
can read about it here
Further read into responsive web design might help you in the future.
Ex:
#import "https://fonts.googleapis.com/css?family=Montserrat:300,400,700";
.rwd-table {
margin: 1em 0;
min-width: 300px;
}
.rwd-table tr {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
.rwd-table th {
display: none;
}
.rwd-table td {
display: block;
}
.rwd-table td:first-child {
padding-top: .5em;
}
.rwd-table td:last-child {
padding-bottom: .5em;
}
.rwd-table td:before {
content: attr(data-th) ": ";
font-weight: bold;
width: 6.5em;
display: inline-block;
}
#media (min-width: 480px) {
.rwd-table td:before {
display: none;
}
}
.rwd-table th, .rwd-table td {
text-align: left;
}
#media (min-width: 480px) {
.rwd-table th, .rwd-table td {
display: table-cell;
padding: .25em .5em;
}
.rwd-table th:first-child, .rwd-table td:first-child {
padding-left: 0;
}
.rwd-table th:last-child, .rwd-table td:last-child {
padding-right: 0;
}
}
body {
padding: 0 2em;
font-family: Montserrat, sans-serif;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
color: #444;
background: #eee;
}
h1 {
font-weight: normal;
letter-spacing: -1px;
color: #34495E;
}
.rwd-table {
background: #34495E;
color: #fff;
border-radius: .4em;
overflow: hidden;
}
.rwd-table tr {
border-color: #46637f;
}
.rwd-table th, .rwd-table td {
margin: .5em 1em;
}
#media (min-width: 480px) {
.rwd-table th, .rwd-table td {
padding: 1em !important;
}
}
.rwd-table th, .rwd-table td:before {
color: #dd5;
}
<h1>RWD List to Table</h1>
<table class="rwd-table">
<tr>
<th>Movie Title</th>
<th>Genre</th>
<th>Year</th>
<th>Gross</th>
</tr>
<tr>
<td data-th="Movie Title">Star Wars</td>
<td data-th="Genre">Adventure, Sci-fi</td>
<td data-th="Year">1977</td>
<td data-th="Gross">$460,935,665</td>
</tr>
<tr>
<td data-th="Movie Title">Howard The Duck</td>
<td data-th="Genre">"Comedy"</td>
<td data-th="Year">1986</td>
<td data-th="Gross">$16,295,774</td>
</tr>
<tr>
<td data-th="Movie Title">American Graffiti</td>
<td data-th="Genre">Comedy, Drama</td>
<td data-th="Year">1973</td>
<td data-th="Gross">$115,000,000</td>
</tr>
</table>
<p>← Drag window (in editor or full page view) to see the effect. →</p>
Ref: LINK

Resources