I have HTML table in my webPage. I have applied css to that table.
now i want to change css only for first Row that is Table header.
i dont have th in table header, because the table is autogenerated from gridView
how can i achieve this ?
tr:first-child
Apply styles to that.
Consider using CSS pseudo class :first-child
Read this for details: http://www.w3schools.com/css/css_pseudo_classes.asp
If by "gridView" you're refering to the ASP.NET GridView control, you can use the ShowHeader property to have a th generated.
This will then allow you to hook into the th and will be more semantcially correct.
myGridControl.ShowHeader = true;
or
<asp:GridView ShowHeader="true" [...]>
Use the CSS pseudo class :first-child, for example:
table tbody tr:first-child
{
background-color: red;
}
http://jsfiddle.net/KvHsb/
Related
I've been using 'border = 1' in my HTML, which looks fine, but I realise it would be better to use CSS, so I created a basic border class, like so...
.basicborder table, .basicborder th, .basicborder td {
border: 1px solid black;
}
The borders appear around the th and td's but not around the outside of the table itself. Have I done something wrong?
CSS looks fine to me, but you can better use:
table.basicborder, th.basicborder, td.basicborder{border: 1px solid black;}
So, the selector starts with the least specific selector (the HTML element, instead of the class).
But it should already work fine, if you have linked your HTML properly. Do your table, th and td elements have a class="basicborder" attribute each?
edit:
If i comprehend correctly, this would be the best solution.
You make a basic style for all 's with just table,td,th{ etc...
Then you add to the ones with a different style a class, lets stay differentborder.Now you make a CSS saying the following: table.differentborder, .differentborder td, .differentborder th{ your style }
This selects your tables with the class, and all td's an th's where a parent has the class differentborder.
For more fun with CSS selectors you can look on the W3Schools CSS Selector Reference
I wish to use the default bootstrap style ".table" for HTML table that I am creating for my webpage as per :
Bootstrap Link
However, I do not wish to add the horizontal separators, plus I also want the columns to be a little closer than they appear currently. Is there any CSS styling that I should use for this?
Basically, I wish to create an HTML table that has uniform column width, height and have some space between 2 rows for easy readability.
Any guidance would be appreciated, thank you in advance.
You have to add some css in your style :-
Remove horizontal separators :-
table td {
border-top: none !important;
}
and add table-condensed class for columns to be a little closer.
It may help you.
In order to override the table class, you need to create another class named borderless.
html:
<table class='table borderless'>
CSS :
.borderless tbody tr td, .borderless tbody tr th, .borderless thead tr th {
border: none;
}
I would suggest you to go for Bootstrap Grid system
Fiddle example: http://www.bootply.com/vZRIDaqaRm
I have a table, let's say with 3 rows,
I want the first two to be aligned vertically to the top, and second one to be vertically aligned to the middle.
If I try to define:
td {vertical-align:top}
tr.middle {vertical-align:middle}
and in the html, assign class="middle" to the relevant <tr>,
it doesn't work, as the td definition would persist.
only way i've managed to do is is to define
td.top {vertical-align:top}
td.middle {vertical-align:middle}
and then assign the relevant class to every single td, which is too much code.
any way to do this with minimum code?
Simply do this, no need to edit your HTML or add classes.
tr:nth-child(1) td {vertical-align:top;}
tr:nth-child(2) td {vertical-align:middle;}
tr:nth-child(3) td {vertical-align:bottom;}
Demo http://jsfiddle.net/v68ao3pv/1
If you need IE8 support:
tr:first-child td {vertical-align:top;}
tr:first-child + tr td {vertical-align:middle;}
tr:first-child + tr + tr td {vertical-align:bottom;}
Demo http://jsfiddle.net/v68ao3pv/2/show
Yes, there is a way. You can use Descendant selectors. That means, you assign the class “top” to the first tr and the class “middle” to the second one and write in CSS:
.top td{vertical-align:top}
.middle td{vertical-align: middle}
// ↑
// THIS space is important.
So it selects all table cells within an element with the top/middle class. This is something very practical which you can use quite often.
More about CSS selectors on the website of the W3 Consortium.
Using your current CSS, make the .middle rule selector more specific:
td {vertical-align:top}
tr.middle td {vertical-align:middle}
In this case, all td will align to the top by default and the more specific selector will force all the cells in the .middle rows to align to the middle.
Doesn't get much more minimum than this Fiddle: http://jsfiddle.net/sqhxqdck/
Unless I'm misunderstanding your question, all you really need is:
tr.top {
vertical-align: top;
}
Due to the cascading nature of cascading style sheets.
I have the first row of a table styled as follows:
.Table1 tr:first-child td { ... }
I also want a hover style on part of this row, and I've tried these 2 as the next rule, but neither of them works:
.Table1 tr:first-child:hover td { ... }
.Table1 tr:first-child td:hover { ... }
They do both partially work - some td cells in the row have no content, and have a class of emptyColumn, and these cells do have the hover styling applied to them (but only if the styling is !important). However, the cells which do have text content are not styled.
From my (very) limited understanding of specificity, if the first rule actually works, then surely the second rule should also work, since it's more specific? Is there something else that could stop it working?
However, what I actually need is slightly different from this. I only want the hover to work on certain td cells in the tr, and I've given these cells a class of colHeaderButton:
td.setAttribute('class', 'colHeaderButton');
I can't find any way to apply the hover style using the class name - I've tried various combinations such as
.Table1.colHeaderButton tr:first-child td:hover { ... }
but I can't get any of these to work. Thanks.
The td is a member of the class, not the table.
.Table1 tr:first-child td.colHeaderButton:hover
I rewrite some css for a big site. There's lot of content and the content often differs.
I'm somehow stuck on css tables.
My new tables should have rounded borders. Thankfully, I can use CSS only (it can graceful degrade down to IE7), so I will use CSS only.
Tables onsite differs. Some has table header, some doesn't.
As we all know, border-radius doesn't work on table itself. It has to be a table cell argument.
Is there any possibility to write the CSS, so a browser would recognize if the table has thead (and then set border-radius for "table thead tr:first-child th:first-child"), and if not border-radius would be set for "table tbody tr:first-child td:first-child")?
Quick simple shot I tried doesn't work - "table tr:first-child td:first-child" - as I should assume if I would think about it - it sets border-radius for both "thead th" and "tbody td".
If you have any possible solution I'd be grateful. CSS3 only solutions are fine enough for me.
You could use jQuery to count the th tags and set a class on the table if it found any...
var thCount = $("#theTable tr th").length;
if (thCount > 0){
$("#theTable").addClass("roundCorners");
//adds a class to the table so you can make round corners
}
This example assumes you already have some form of ID/Class on the table, just need to change as necessary and use your CSS.