How to add Focus to a table row <tr>? - css

I have a table generated with AngularJS.
Here's my HTML:
<table class="my_table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Celphone</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>{{contact.Name}}</td>
<td>{{contact.Address}}</td>
<td>{{contact.Celphone}}</td>
</tr>
</tbody>
</table>
I want each row to change color when "hover" and a different color when "clicked", so I tried this with CSS:
<style>
.my_table tbody tr:hover {
background-color: #7fccee; /*--- this is a blue color when hover ---*/
}
.my_table tbody tr:focus {
background-color: #f3f3f3; /*--- this is a gray color when clicked---*/
}
</style>
The hover works perfectly, but the Focus is not working! (The weird thing is that in the browser console, if I select the row and "force element state :focus" then it applies the focus to the row)
I also tried using a SCRIPT with jquery:
$(document).ready(function() {
$('.my_table tbody tr').click(function() {
$(this).addClass('active'); //I'm adding the color gray with css to '.active'
});
});
But this won't work too, what am I doing wrong?

The :focus pseudo class only works with form elements such as <input>, <button> etc. You can add it to a non-form element like <tr> by adding tabindex, e.g.:
<tr tabindex="0">
Then apply the CSS as normal.
.my_table tbody tr:hover {
background-color: blue;
}
.my_table tbody tr:focus {
background-color: red;
outline: 0; /*remove outline*/
}
<table class="my_table" border>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Celphone</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts" tabindex="0">
<td>{{contact.Name}}</td>
<td>{{contact.Address}}</td>
<td>{{contact.Celphone}}</td>
</tr>
</tbody>
</table>

I would use the directive:
app.directive('ngFocusClass', function () {
return ({
restrict: 'A',
link: function(scope, element) {
element.focus(function () {
element.addClass('focus');
});
element.blur(function () {
element.removeClass('focus');
});
}
});
});

Related

What is the correct way to apply classes dynamically to odd/ even rows of Table TR element using Tailwind CSS

Trying to alternate row colors in my tailwindcss styled page, the code below has no effect on my <tr>s:
<style>
tr:nth-child(even) {
class="bg-gray-50";
}
tr:nth-child(od) {
class="bg-white";
}
</style>
What am I missing out please?
As stefan.at.wpf mentioned you should extend your tailwind config like so:
// tailwind.config.js
module.exports = {
// ...
variants: {
extend: {
backgroundColor: ['even'],
}
},
}
Then you can use even in your classes in like so:
<tr class="even:bg-grey">
The Tailwindish way would be to use even:someclassor odd:someclass. Needs to be enabled first, see details here
It seems to me that you are mixing CSS styles and HTML classes. You have to go for one or the other. Assuming that .bg-gray-50 corresponds to #ccc, you could apply the styles as follows:
<style>
tr:nth-child(even) {
background-color: #ccc;
}
tr:nth-child(od) {
background-color: #fff;
}
</style>
Add even and odd prefixes to your class list
<tbody>
<tr class="even:bg-gray-50 odd:bg-white"></tr>
</tbody>
See First, last, odd, and even from their documentation
You are trying to set kind of css classes into css tags.
Plus I advice you to set border-collapse: collpase; to your table, this way, you won't have the separation between your cells.
You must set css directly like below demo:
table{
border-collapse: collapse;
}
tr:nth-child(even) {
/*class="bg-gray-50";*/
background: gray;
}
tr:nth-child(od) {
/* class="bg-white";*/
background: white;
}
<table>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>
You can achieve this using #apply and then listing the tailwind classes you want applied to the element in question.
tr:nth-child(even) {
#apply bg-blue-50;
}

Changes color of 1st three rows only Data Tables

I am using datatables to sow data and i want to make different colors for top 3 rows 1st should be green 2nd blue and third row color should be red
My table structure is as
<script>
$(document).ready(function() {
$(document).ready(function() {
$('#project').DataTable({
"columnDefs": [
{ "orderable": false, "targets": [0,6,7] }
]
});
});
});
</script>
<table class="table table-striped table-bordered table-hover table-checkable order-column dataTable no-footer" id="project" class="color">
<thead>
<tr role="row">
<th style="display:none"></th>
<th>Sr</th>
<th>Reg #</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach($results as $index => $result)
<tr>
<td style="display:none"></td>
<td>{{ $index+1 }}</td>
<td class="sorting_1"> {{ $result->reg_no }} </td>
<td>{{ $result->first_name }}</td>
</tr>
#endforeach
</tbody>
</table>
I tried to give style to first row as
.color td {
background-color : green;
}
But not working please help to solve it
Thanks
All modern browsers support CSS nth-child selectors. Backgroundcolours can be applied to the whole rows as such:
tbody>tr:nth-child(1) { /* etc */
background-color: green;
}
Not all properties can be applied to rows. Sometimes you will need to apply them to the cells inside:
tbody>tr:nth-child(1) td { /* or th */
background-color: green;
}
If you are desperate, you can apply classes, but that is not the best possible solution. Classes should only be used to distinguish between elements which don’t follow a pattern.
You can use the nth-child selectors to achieve what you are trying to do. You can also learn about it here https://css-tricks.com/almanac/selectors/n/nth-child/
Try something like this:
tr:nth-child(1) {
background-color: green;
}
tr:nth-child(2) {
background-color: blue;
}
tr:nth-child(3) {
background-color: red;
}
tr:nth-child(1):hover, tr:nth-child(2):hover, tr:nth-child(3):hover {
/*
Give whatever effect you want
*/
}

AngularJS bootstrap class

I have the following angularjs ng-repeat ,column status have three values (Phoned,Active,Waiting)
<tr ng-repeat="person in persons">
<td>{{person.id}}</td>
<td>{{person.status}}</td>
</tr>
I need to apply bootstrap class depending on the value of status,like the following image
You need to use ng-class directive
Markup
<tr ng-repeat="person in persons">
<td>{{person.id}}</td>
<td ng-class="{'phoned':person.status === 'Phoned',
'active':person.status === 'Active',
'waiting':person.status === 'Waiting'}">
{{person.status}}
</td>
</tr>
CSS
.phoned{
background-color: blue;
}
.active{
background-color: green;
}
.waiting{
background-color: orange;
}

CSS - Style the title from the image tag

I'm trying to style the title from the image tag.
I have search other question but can´t it put working in my project.
But I can´t make any changes.
Someone can give me hand with this pls?
my code:
table.tablesorter tbody tr td a:hover img[title]:after
{
content: attr(title);
padding: 4px 8px;
color: #FFF;
background-color:black;
}
<table class="tablesorter" style="width:98% !important">
<thead>
<tr>
<th>
.....
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
..........
</td>
<td>
<a href="#Url.Action("Edit","Account", new { id=item.UserId })">
<img src="~/Content/images/icon_edit.png" title="Edit"/>
</a>
</td>
</tr>
</tbody>
<tfooter>
</tfooter>
</table>
It should be content: attr(title);, not content: attr(data-title); - you don't have a data attribute.
Also, it seems ::before and ::after pseudo-elements are not defined for img - you may have to use something else:
CSS Content attribute for IMG tag
Working Example (when the image is missing): http://jsfiddle.net/DMAFm/
Another example, with the title on the <a> tag: http://jsfiddle.net/DMAFm/1/

I want a different color only for first row of a html table

I used a table. I applied CSS ID table-4.
Following the html code:
<table border='0' width='100%' id='table-4'>
<tr><td>Date</td><td>Headline</td></tr>
<tr><td>29 DEC</td><td>Dead</td></tr>
<tr><td>30 DEC</td><td>Hit</td></tr>
<tr><td>02 JAN</td><td>Leg</td></tr>
</table>
Here is the style.css:
#table-4 { background-color: #F2F2F2;}
So the whole table's background color is #F2F2F2, but I want a different color for the first row where Date and Headline goes, so how could I modify my CSS for this thing?
You can use :first-child for this. Write like this:
#table-4 tr:first-child{
background:red;
}
Check this http://jsfiddle.net/cbK8J/
#JonathandeM.'s comment is correct. Pop <thead> and <tbody> tags in there, and change the <td> tags in the <thead> row to <th> tags (because HTML says what things are, and they're headings):
<table border='0' width='100%' id='table-4'>
<thead>
<tr><th scope="col">Date</th><th scope="col">Headline</th></tr>
</thead>
<tbody>
<tr><td>29 DEC</td><td>Dead</td></tr>
<tr><td>30 DEC</td><td>Hit</td></tr>
<tr><td>02 JAN</td><td>Leg</td></tr>
</tbody>
</table>
Then the CSS to make the heading row have a different background colour is:
#table-4 thead tr {
background-color: green;
}
you can do this:
HTML:
<td class="whatever">Date</td><td class="whatever">Headline</td>
css:
.whatever { color: #a9a9a9 }
<tr> will do the row, <td> will do the cells.
You have to add id to first tr tag:
<table border='0' width='100%' id='table-4'>
<tr id ="r1" ><td>Date</td><td>Headline</td></tr>
<tr><td>29 DEC</td><td>Dead</td></tr>
<tr><td>30 DEC</td><td>Hit</td></tr>
<tr><td>02 JAN</td><td>Leg</td></tr>
</table>
and then add another css line:
#table-4 #r1 { background-color: blue;}
it will give you blue color
Or you can simply modify only the Html file, no change in css
<table border='0' width='100%' id='table-4'>
<tr><td bgcolor='red' >Date</td><td bgcolor='red'>Headline</td></tr>
<tr><td>29 DEC</td><td>Dead</td></tr>
<tr><td>30 DEC</td><td>Hit</td></tr>
<tr><td>02 JAN</td><td>Leg</td></tr>
</table>

Resources