CSS for TH hyperlink in asp:gridview - asp.net

I want to style the anchor links in my gridview header with css. My style for the classes th works but the style for th a does not apply to it. It is overwritten by the containing divs a style. Also if I do th a or th a:hover without a preceding class it does not effect the hyperlinks in my th. I have tested this in both IE and Firefox. This is the gridview portion of my css:
.gridview
{
border-color: #9BBE00;
border-width: thin;
border-style: solid;
width: 700px;
}
.gridview th
{
background-color: #F4A80A;
color: White;
font-weight: bold;
}
.gridview th a
{
font-weight: bold;
color:Red;
}
.gridview th a:hover
{
font-weight: bold;
color:Red;
}
.gridview td
{
text-align:center;
}

This is probably a specificity issue. CSS rules are weighted not only by their source and order, but according to a formula:
Inline? 1000 points
IDs in selector? 100 points for each
Classes and pseudo-classes? 10 points each
Specific elements? 1 point each
Therefore you might have something like this:
div#something a { color: blue; } /* 102 points */
overriding your style:
.gridview th a { color: red; } /* 12 points */
You can solve this by either making your style more specific:
div#something .gridview th a { color: red; } /* 123 points */
or using the hackier !important approach:
.gridview th a { color: red !important; } /* overrides more specific selectors */
To be technically correct, I should mention that this is not really straight addition of points if any position reaches 10. For example, if for some strange reason you had a selector with 12 classes, the specificity weight might be:
0 1 12 0
That is, don't carry the one. The above is less specific than:
0 2 0 0
Finally, I assume you realize your :hover style is the same as your plain link style.

It could have something to do with the way the GridView control renders table HTML. The header row is not contained in the thead element as expected.
Correctly using thead and th
<table>
<thead>
<tr>
<th>
Column One Header
</th>
<th>
Column Two Header
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Column One
</td>
<td>
Column Two
</td>
</tr>
</tbody>
</table>
GridView Header
The grid view control instead renders the header cells inside the table body using td instead of th.
<table>
<tbody>
<tr>
<td>
Column One Header
</td>
<td>
Column Two Header
</td>
</tr>
<tr>
<td>
Column One
</td>
<td>
Column Two
</td>
</tr>
</tbody>
</table>
You can add a CSS class to be appended to the GridViews header like so.
<style type="text/css">
.gridview td.gridviewheader
{
background-color: #F4A80A;
color: White;
font-weight: bold;
}
.gridview td.gridviewheader a
{
font-weight: bold;
color:Red;
}
.gridview td.gridviewheader a:hover
{
font-weight: bold;
color:Red;
}
</style>
....
<asp:GridView ID="gvExample" CssClass="gridview" runat="server">
<HeaderStyle CssClass="gridviewheader" />
<Columns>
</Columns>
</asp:GridView>
Good luck, hope it helps.

Related

CSS Specificity Order

I have two css classes in a codebase and I'm unsure why one is being chosen over the other, I thought the specifity of my second one was stronger. Is anyone able to explain?
The CSS class that it's choosing is -
td, th, table { border-collapse: collapse; border: 1px solid #999; }
And the CSS that I want it to use is -
table.cancellation { border: none; }
I thought as the second one had a class selector it would have a stronger specificity, why am I wrong?
You're overwriting the style for the table, but not the cells.
In the first table, everything has a red border.
In the second table, the table has a blue border, but since border-collapse:collapse is set, the red of the td & td appear above it.
In the third table, border-collapse is set to separate and you can see that the table does truly have a blue border.
In the last table, the styles for td and th are also overwritten - giving a borderless table.
th,
td,
table {
border-collapse: collapse;
border: 1px solid red;
margin-bottom:1rem;// just for looks
}
table.table {
border-color: blue
}
table.separate{
border-collapse:separate;
}
table.none,
table.none th,
table.none td{
border:none
}
<table>
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table separate">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table none">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
CSS reads from top to bottom
if the class
td, th, table { border-collapse: collapse; border: 1px solid #999; }
is written after
table.cancellation { border: none; }
Then it will take the properties of the one that comes at the very last in your code!
If this isn't the case then you can use "border: none !important" as it has the highest rank among everything else
In case both of these methods don't work then I suggest you show me the bigger picture as in type in your related HTML and complete CSS of the div

How to achieve this with CSS?

I wouldn't call myself a programmer at all but I do know some basic css. However, I have trouble creating a table that looks something like on this site: https://www.slotsia.com.
What I wish to achieve is that row numbering to the far left together with that corner graphic. I'm sure it's all done with css and the numbering is probably automatic.
I use wordpress and the plugin tablepress to create tables.
I did try to put the following code in the css which achieved the numbering, even if it starts wrong with 0 instead of 1.
.tablepress-id-4 {
counter-reset: rowNumber;
}
.tablepress-id-4 tr:not(:first-child) {
counter-increment: rowNumber;
}
.tablepress-id-4 tr td:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: .5em;
}
Also, the larger text doesn't look like normal fonts? I'm thinking it's font awesome or google fonts or something? How to use that?
Thank you
Here's a fiddle demonstrating to accomplish that index thing in the cell : https://jsfiddle.net/59wfkzgs/2/
HTML
<table>
<tr>
<td>
<span class="index">1</span>
ABC
</td>
<td>
ABC
</td>
</tr>
<tr>
<td>
<span class="index">2</span>
ABC
</td>
<td>DEF</td>
</tr>
<tr>
<td>
<span class="index">3</span>
ABC
</td>
<td>DEF</td>
</tr>
</table>
CSS
table, tr, td {
font-family:"Segoe UI";
padding:20px;
border:solid 1px black;
border-collapse:collapse;
}
td {
text-align:center;
position:relative;
min-width:150px;
}
.index {
position:absolute;
top:-2px;
left:-2px;
width:30px;
height:30px;
border:solid 1px black;
border-radius:0 0 100px 0;
background-color:rgba(0,0,0,.5);
}
You are able to do -almost- everything you want in a table's cells, so you can stylish it in any way you want to. Do not fear of doing so :).
Hope it helps.
Dylan

How to add margins between rows of gridview

I have a gridview and I'm trying to add empty space between each row using CSS.
This doesn't work:
#gridview tr{
margin-bottom: 10px;
}
Neither does this:
#gridview td{
margin-bottom: 10px;
}
Messing with "border-collapse" does nothing as well.
Anyone know how to add spacing?
Margin will not work in table elements you should use border or padding like the following snippet
table.grid
{
border-collapse: collapse;
}
table.grid tr
{
border: 0px solid white;
border-width: 20px 0;
}
<table class="grid ">
<tr>
<td>
Row 1
</td>
</tr>
<tr>
<td>
Row 2
</td>
</tr>
</table>
You can use it in GridView like this:
<asp:GridView ID="PetGrid" runat="server" CssClass="grid">
use padding:
#gridview td{
padding-bottom: 10px;
}
Use padding in th and td,
th, td {
padding: 15px;
}
Try this
table td {
border-bottom: solid 20px transparent;
}
<table>
<tr>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
</tr>
</table>
Which gridview are you using? try "inspect element" on the grid row and check from which .css class the style is getting inherited. try changing some parameters like padding/row height and observe if that works and make the change in the grid's library css or ovverride it with custom class.
#gridview tr{
{
height: 18px;text-align: left;font-weight: bold;color:white;padding: 5px 6px 2px;border-bottom: 2px solid #666666;white-space: nowrap;
}
In specifications margin are ignored for table cells: Link
Try to use padding
#gridview td{
padding-bottom: 10px;
}

:nth-child definition & when it meets tr and caption

with the code below I found I'm confused with the definition of :nth-child
The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent.
tr:nth-child(2n) {
background-color: gray;
}
table {
margin-left: 20px;
margin-right: 20px;
border-spacing: 0px;
border: thin solid black;
caption-side: bottom;
border-collapse: collapse;
}
td,
th {
border: thin dotted gray;
padding: 5px;
}
caption {
font-style: italic;
padding-top: 8px;
}
<table>
<caption>content</caption>
<tr>
<th>table head</th>
</tr>
<tr>
<td>111111</td>
</tr>
<tr>
<td>222222</td>
</tr>
<tr>
<td>333333</td>
</tr>
<tr>
<td>444444</td>
</tr>
<tr>
<td>555555</td>
</tr>
<tr>
<td>666666</td>
</tr>
</table>
and the 111111, 333333, 555555 become gray. nothing changed after I delete the caption tag, but 222222, 4444444, 666666 become gray after I removed the tr tag of table title. Isn't :nth-child suppose to count every element of its parent?
The problem here is that your HTML is invalid. tr elements must be wrapped within either a thead, tbody or tfoot element, and most browsers will automatically fix this for you by sticking them in a tbody.
Your HTML on these browsers will actually end up looking like this:
<table>
<caption>...</caption>
<tbody>
<tr>...</tr>
...
</tbody>
</table>
And thus, deleting the <caption> element will have no impact on the positioning of your tr elements.
If you inspect your <table> element, this is what you'll see:

tableCss style with Nested tables

I am trying to set the first column of the outer table (tbStudentPreference) with special styles...
But the problem is that it applies not only to the outer table column but also to the table inside the outer table.
I want to apply my style only to the outer container table. Please help.
<style>
#tbStudentPreference td:first-child {
font-weight: bold;
vertical-align: top;
width: 100px;
}
#tbStudentPreference {
vertical-align: top;
padding: 3px;
}
</style>
<table id='tbStudentPreference'>
<tr>
<td>xxxxx
</td>
<td>.....
</td>
</tr>
<tr>
<td>xxxxx
</td>
<td>.....
</td>
</tr>
<tr>
<td colspan='2'>
<table>
<tr>
<td>Inside Table
</td>
<td>.....
</td>
</tr>
<tr>
<td>Inside Table
</td>
<td>.....
</td>
</tr>
</table>
</td>
</tr>
</table>
I am trying to set the first column of the outer table..
You need to negate the inner table.
Also, the browser automatically adds a tbody for you, so it is not enough to use a child combinator on tr directly. You need to override that with a child-combinator on tbody. You then negate the inner table by using the presence of colspan attribute.
So, you select td which is a first-child among those which are not having a colspan attribute, direct descendant of tr which itself is a direct descendant of tbody which itself is direct descendant of your table. Like this:
#tbStudentPreference > tbody > tr > td:not([colspan]):first-child {...
The negation pseudo-class: https://developer.mozilla.org/en-US/docs/Web/CSS/:not
Snippet:
#tbStudentPreference { border: 1px solid gray; border-collapse: collapse; }
#tbStudentPreference td { border: 1px solid gray; }
#tbStudentPreference > tr > td:not([colspan]):first-child {
font-weight: bold; color: red;
}
#tbStudentPreference > tbody > tr > td:not([colspan]):first-child {
font-weight: bold; color: red;
}
<table id='tbStudentPreference'>
<tr><td>xxxxx</td><td>.....</td></tr>
<tr><td>xxxxx</td><td>.....</td></tr>
<tr><td colspan='2'>
<table>
<tr><td>Inside Table</td><td>.....</td></tr>
<tr><td>Inside Table</td><td>.....</td></tr>
</table>
</td></tr>
</table>

Resources