What's the best way to have a table with sticky header? - html

What I'm currently doing is 2 tables, one for headers and one for data, in combination with table-layout:fixed and fixed widths for each column.
I don't like that solution since I have to keep adjusting widths and they don't look as good a the auto adjusted ones you get when the table layout isn't fixed.
I also considered a JS solution where the data table is laid out, then JS picks up each column width and applies it to the header table. The advantage of this is fluid td widths dictated by content. The problem is table contents can change dynamically, and the JS script needs to know that so it can recompute the widths... ew.
Is there a better way of doing that?

better to use some plugins like jQuery Plugin for Fix Header Table
or if you want the pure CSS and want to know how it works. you can try the below fiddle https://jsfiddle.net/dPixie/byB9d/3/light/
its all about showing the content if div which is inside the TH
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
padding: 9px 25px;
top: 0;
margin-left: -25px;
line-height: normal;
border-left: 1px solid #800;
}
th:first-child div{
border: none;
}

Related

Adjust width of container to child only using multiples of a number

I'm trying to use only CSS to adapt a container width to its child element content (the content is dynamic and load from the backend) but then adjusting this width to always a multiple of 8px. Is that possible only using CSS? (No JavaScript)
Thanks!
Edit: I'm trying to use the padding as a way to increase the width of the element and keeping the width:fit-content tag. The thing is, how can I get the current width of the element in CSS to create a formula that increases the padding to a multiple of 8? Can you come up with another solution? Thanks again. Example:
HTML
<div class="banner">This is a banner!</div>
CSS
.banner {
width: fit-content;
padding: calc() /* Not sure what to do here */
border: solid black 1px;
padding: 6px;
text-align: left;
box-sizing: border-box;
}
George if you have a codepen URL to attach it would be really helpful, and a photo of the expected result.
.container {
border: 2px solid #ccc;
padding: 10px;
width: 20em;
}
.item {
width: -moz-fit-content;
width: fit-content;
background-color: #8ca0ff;
padding: 5px;
margin-bottom: 1em;
}
this would work with reference to its parent container https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content

How do I resize a table and its cells as screen size shrinks?

https://clementmihailescu.github.io/Pathfinding-Visualizer/#
I'm using a similar table layout like the one linked for a project I'm currently doing but I can't seem to figure out how to not have the cells wrap onto a new row when the screen size shrinks. How is it done in this Pathfinding Visualizer project? What CSS property should I be applying to my table/tr/td? When I shrink my screen size, my table's cells wrap onto the next row making it so that every other row has a different number of columns. I'd like for the table width and, consequently, the table cells to just shrink in width and have the same columns in every row even for smaller screen sizes.
table {
border-collapse: collapse;
margin-left: 8px;
margin-right: 5px;
position: fixed;
bottom: 20px;
}
.grid-row {
white-space: nowrap;
}
.square {
background: #fff;
border: 1px solid #999;
padding: 0;
height: 25px;
width: 25px;
margin-right: -1px;
margin-top: -1px;
border: 1px solid rgb(175, 216, 248)
}
You haven't actually provided a link to your table or it's code.
But, if you're actually using a table structure, your white-space: nowrap; call should be applied to your td elements and not your tr elements.
Again, without a link to your table it's difficult to answer. But perhaps this will point you in the right direction?

What's causing these image sizes to be wrong?

I cannot work out why some of the images on this page are wrongly sized (2 of them appear smaller than the others).
https://www.violinschool.org/video-testing/
I have re-cropped them all to the same size (355x200, ratio 16:9) so there must be something else causing it.
Am trying to check the html and CSS (it's a wordpress site using Toolset Types) to see what might be wrong, but to no avail.
Try adding this line to your CSS file and see if it helps:
table.wpv-loop.js-wpv-loop td {
width: 25%;
}
As <td> in table are not fixed width they get the width according to the content inside it untill the width is not defined in css.
You can do it with 2 solutions.
First is Add table-layout:fixed in table.
table{
border-bottom: 1px solid #ededed;
border-collapse: collapse;
border-spacing: 0;
font-size: 14px;
line-height: 2;
margin: 0 0 20px;
width: 100%;
table-layout: fixed;
}
Adding table-layout:fixed will restrict the table to show each cell with same width.
and second Use width in <td>
As you are using exact 4 <td> in one row so you can give width manually width:25%.
td {
border-top: 1px solid #ededed;
padding: 6px 10px 6px 0;
width: 25%;
}
Problem is not in images but in table. Now each table-cell is taking dynamic width according to its content. If one table-cell has more content it will be wider than others.
Add table-layout: fixed property on table then all table-cell will take equal width and your problem will be fixed.
table {
border-bottom: 1px solid #ededed;
border-collapse: collapse;
table-layout: fixed;
border-spacing: 0;
font-size: 14px;
line-height: 2;
margin: 0 0 20px;
width: 100%;
}

Fixed table header scroll both horizontal and vertical CSS ONLY

Firstly, is it possible to achieve this using only CSS?
I have built a table that can scroll both horizontally and vertically however, I want to have the header encapsulated within it's container and not appear outside of the wrapper. So that when you scroll horizontally the corresponding header is in line with the content of it's designated column.
Using different variations of position: absolute and position: static give me some intended results but not the complete solution.
You'll note the width applied to the section element to give the effect of scrolling horizontally within the enclosed region.
Here is a JSFIDDLE example of what I have so far and CSS below to reference
https://jsfiddle.net/ko6qco1r/
html, body{
margin:0;
padding:0;
height:100%;
}
section {
position: relative;
border: 1px solid #000;
padding-top: 37px;
background: grey;
width: 300px;
}
.container {
overflow-y: auto;
overflow-x: scroll;
height: 200px;
}
table {
border-spacing: 0;
width:100%;
}
td + td {
border-left:1px solid #eee;
}
td, th {
border-bottom:1px solid #eee;
background: white;
color: #000;
padding: 10px 25px;
}
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
padding: 9px 25px;
top: 0;
margin-left: -25px;
line-height: normal;
border-left: 1px solid black;
}
th:first-child div{
border: none;
}
This is another one of those interesting challenges (like vertical centering) brought to us by the inaction of the W3C. Also like vertical centering, you can't put a fixed header on a table with a horizontal scrollbar using position: fixed; on the thead element. But you do have a couple of options.
Option 1 - Horizontal Scrolling (http://codepen.io/staypuftman/pen/JXbpvZ)
The key here is to reestablish your table layout as table-layout: fixed; CSS-tricks has a good piece on this approach and then put a min-width value on your container when you want the scrollbars to appear. This makes the table scrollable left to right and maintains the integrity of the column headers on smaller devices. But the header is not fixed.
Option 2 - Fixed Header (https://jsfiddle.net/dPixie/byB9d/3/light/)
Your code looked like a rip-off of this guy's work, so I thought I'd repost here to give him some credit. This approach creates a series of <div> elements that mirror the contents of the <th> elements and uses some VERY creative positioning techniques to get it all to work.
There is a much better run-down on Salzar design that shows some examples and explains in detail all the complicated maneuvers to get this right.

How to adjust table size to correspond to the media screen size?

I'm having difficulties adjusting my tables to fit media screen sizes. The layout as it is now adjusts its content via media queries. In effect, there is no need for a scroll bar.
Here's some code...
(1) The HTML:
<div id="results">
</div>
Ya, it's really simple. This tag actually resides in a few other divs that hold content. This particular div holds content that is sent from the server when a request is made. It responds properly when plain text is the content, but tables cause an issue.
Here's my CSS code...
(1) The CSS for #results
div#results {
width: 90%;
margin: 0 auto;
margin-top: 30px;
}
(2) The CSS for tables:
table {
font-family: 'PT Sans', Arial, sans-serif;
color: #666;
font-size: 12px;
text-shadow: 1px 1px 0px #fff;
background: #eaebec;
margin: 20px;
border: #ccc 1px solid;
width: 80%;
margin: 0 auto;
}
table th {
padding: 21px 25px 22px 25px;
border-top: 1px solid #fafafa;
border-bottom: 1px solid #e0e0e0;
background: #ededed;
}
table th:first-child {
text-align: left;
padding-left:20px;
}
table tr {
text-align: center;
padding-left: 20px;
}
table td:first-child {
text-align: left;
padding-left: 20px;
border-left: 0px;
}
table td {
padding:8px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #fafafa;
}
table tr:hover td {
background: #f2f2f2;
}
The table remains the same size regardless of media screen size. The CSS probably makes that obvious. However, what can be done in this case? If I "squish" the table, the content will be illegible. For instance, if I want to pull up a table on a smartphone, how can I modify the table so that it is usable? Right now I have 5 columns. Is there a way to possible start a new row, say after two or three columns, in order that the content doesn't have to be compressed?
That's the basic idea; I hope that the crux of what I am saying is clear. Any input is appreciated.
There are loads of ways to handle tables on smaller screens. What a lot of sites do is put them in a div and assign the div a width of 100% and overflow:auto; Then they set a minimum width for the table itself so that the integrity of the data is maintained without bastardising the table.
All the user does is scroll across when they reach a table that is too big for their screen, but because its contained in a 100% width div with overflow, it won't break the flow of the page.
That's just one way of doing it, and is the easiest to markup.
Hope that helps.