Dynamic column width in table with advanced css selectors - css

I once found a small piece of CSS code here that let me set the width of a column in a table based on the amount of columns in the table. Sadly my search-fu is not able to give me an answer. So I decided to ask directly.
I remember the CSS code went something like this:
table tr td:nth-of-type(1) ~ table tr td:nth-of-type(3) {
width: 33%;
};
Where the above code would make every column in a table with 3 columns be 33% at width.
table tr td:nth-of-type(1) ~ table tr td:nth-of-type(4) {
width: 25%;
};
And with this every column in a table with 4 columns be 25% width.

Related

Google Visualization Table from spreadsheet: hide header row of table

I have a Google sheet for the data source. Column B contains the information I want to show in the table. B1 is the column label ("name") and the rest of column B is the data of interest (B2 is "Anna," B2 is "Bernard," etc.)
As described in https://developers.google.com/chart/interactive/docs/spreadsheets, I can successfully query the data using an URL like https://docs.google.com/spreadsheet/ABC123/gviz/tq=select...
...where the select statement is the encoded version of "SELECT B ORDER BY B."
My table div tag populates but it shows the header row "name." How can I conceal this and make it start with "Anna"?
Do I change the select statement in the front end or is there a table option I can configure to hide the first row?
I figured it out: in the table options, add the parameters:
allowHtml: true,
cssClassNames: { tableCell: 'nameClass', headerCell: 'noHeader' }
and in the CSS, add:
.noHeader { background-color: #FFF; border-collapse: collapse; border:none; font-size: 0px; }
(Following your example) a simpler way is to use display: none (See https://stackoverflow.com/a/5113253/11794965)
So in your CSS, add:
.noHeader {
display: none;
}

How to apply last th in table display none?

I have a problem with table th, I can not set display none to the last th.
Here is my code:
#content-area-job-details #site-content-job-details .entry-content table.job-table tr th:last-of-type{
display:none;
}
when I use this code it set display to none for all th. I want only last th display none.
You can see my problem at:
http://westecmedia.com/?page_id=974
Help me please
This doesn’t work that way. :last-child or :last-of-type are always relative to the parent container. So in case of a table, that’s the tr element. If you match all tr elements in the table, and then get the last th for each, then you are matching every last th in each of those rows. So in your case, essentially all ths.
You would need to have a way to select that one tr which you are interested in, but other than maybe :nth-last-child(2), there is not really a good way to get that one. You should add an actual class to it.
Note that just hiding the th will not give you the desired result though. Table cells are always table cells, and unless you make them take more than a single cell, they will only ever occupy a single cell. So in your case, if you hide or remove that one th, the following td will not fill the whole row. It will only fit that very small cell where the th was previously located. You would have to add colspan="2" to the td in the markup to fix that.
You should use javascript to do what you need...
(function(window) {
'use strict';
function hideLastTh() {
var lastElement;
try {
lastElement = window
.document
.querySelector('.job_info')
.parentElement
.parentElement
.querySelector('th[scope="row"]')
;
lastElement
.classList
.add('hidden')
;
} catch(e) {
console.error('hideLastTh:ERROR', e);
}
}
return window.document.addEventListener('DOMContentLoaded', hideLastTh);
})(window);
I can see that your site uses jQuery. If you can add jQuery code, just add these two lines:
$("th:last").hide();
$("th:last").siblings("td").attr("colspan","2");

CSS: Keep DT and DD together within columns

I have a definition list split in multiple columns:
dl { -webkit-column-count: 2; }
Unfortunately, I sometimes have a case where a DT occurs at the very bottom of one column and its following DD at the top of the next, which looks really ugly. Since I can't wrap the elements I want to keep together in a container, my only option appears to be something like:
dt { -webkit-column-break-after: avoid; }
but that doesn't seem to work. Any ideas?
Here's a fiddle that demonstrates the problem.

How to CSS select all cells with colspan set?

I would like to select all cells in the table header which have the colspan attribute set.
Of course I could do something like:
table thead th[colspan="1"],
table thead th[colspan="2"],
table thead th[colspan="3"] {
}
but I'm looking for something like this:
table thead th[colspan*=""] {
}
which does not seem to work.
Question:
How to select all cells with colspan set in a table?
Thanks!
Just use
table thead th[colspan]
According to the CSS 2.1 spec,
[att]
Match when the element sets the "att" attribute, whatever the value of the attribute.

Column width on webgrid lost after postback

I have a very simple webgrid with 3 columns:
View
columns: grid.Columns(
grid.Column("Applicant Name",
format: (item) => #Html.ActionLink((string)item.Name, "EditApplicant", "Applicant", new { id = item.ApplicantId },
null), style: "AppName")
, grid.Column("Role", "Role", style: "Role")
, grid.Column("ApplicantSkills", "Skills", style: "AppSkills")
I want to set my columns to a fixed width. I have tried using percentage widths here and exact widths like 500px, 100px etc, and they all work initially, but are lost after postback.
css:
.AppSkills {
width: 70%;
}
.AppName {
width: 20%;
}
.Role {
width: 10%;
}
My grid is a results grid, which is populated from a number of filters, so every time the user selects different filters and clicks search the results are changed and the grid re-populated. What im finding is the grid column width style is being lost. Here is what my screen looks like. Initially it looks fine, but after selecting different filters and hitting search, the grid column widths are lost.
I have tried posting my form as a GET and a POST to see if it was the Get that was losing the formatting. But both yield the same results.
#using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
Is there anything obvious I'm doing wrong here? Or is there any way I can ensure a fixed width on my grid columns so they don't move about?
You're missing spaces after the commas, so it's probably treating one of the entries as a big unbreakable word. If you can't change the underlying data, assuming it isn't the code doing the concatenation, you might be able to use the css word-break property to sort it.
Make sure that after postback that the grids still have the classes applied to them.
Also, please post the HTML for us to view so we can diagnose the problem since we are not to sure if they are styled divs or a table.
If it is a table, try running the following CSS. Basically, it will target the columns individually without class names, just in case the classes are lost during postback.
td:nth-child(3) {
width: 70%; /* targeting the skills column */
}
td:nth-child(1) {
width: 20%; /* targeting the name column */
}
td:nth-child(2) {
width: 10%; /* targeting the role column */
}

Resources