Highlight Table Row on Hover Using Twitter Bootstrap

Introduction

 
Twitter Bootstrap provides built-in CSS that makes it very quick and easy to add clean and functional interface elements to your page. I am using Bootstrap and have created a table in which I need to highlight a table row on hover. In this article, we use some Twitter Bootstrap CSS to highlight a table row on hover. A Twitter Bootstrap table can be styled and well designed. You can style your table by just adding some classes to your table and it’ll look nice. In the previous article, I created a table using Bootstrap.
 
 
Using Twitter Bootstrap, you may create a static navbar. - See more at http://www.w3resource.com/twitter-bootstrap/navbar-tutorial.php#sthash.8Za94Odc.dpuf
 

What Bootstrap is

 
Twitter Bootstrap was created by two guys at Twitter who wanted to speed up (bootstrap) their workload and code. The Bootstrap framework is used to develop front-facing web applications and sites. When we work on the project there are many things that are required in nearly every project. For example a Grid, Tables, Forms, Buttons, and so on. These are the common requirements of any project. With these, you can get a web project up and running quickly and easily. The Bootstrap framework provides you all those components. The entire framework is module-based, you can customize it with your own bit of CSS. It also provides JavaScript plugins for things like tooltips, popovers, modals, and more.
 
To read more see Bootstrap.
 
Include with HTML
 
Now to include them in the project. So let’s imagine we have a blank HTML file that goes something like the following:
 
HTML file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title></title>  
  6.     </head>  
  7.   
  8.     <body>  
  9.     </body>  
  10.   
  11. </html> 
Now we need to add a reference to the bootstrap CSS file and JavaScrit file with the HTML file as in the following:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title></title>  
  6.         <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
  7.         <link href="Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />  
  8.         <script src="Bootstrap/js/bootstrap.min.js" type="text/javascript"></script>  
  9.     </head>  
  10.   
  11.     <body>  
  12.     </body>  
  13.   
  14. </html> 
Note: Also don’t forget to include jQuery if you’ll be using Bootstraps JS plugins.
 
Using bootstrap CSS class="table-striped table-hover"  
 
Just another good looking table. I added the "table-hover" class because it gives a nice hovering effect. Now open the bootstrap.css file and find the "table-hover" class. The HTML file looks as in the following:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title></title>  
  6.         <link href="Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />  
  7.         <style type="text/css">  
  8.         body {  
  9.             margin: 50px;  
  10.         }  
  11.         </style>  
  12.     </head>  
  13.   
  14.     <body>  
  15.         <table class="table table-striped table-hover">  
  16.             <thead>  
  17.                 <tr>  
  18.                     <th>id</th>  
  19.                     <th>Name</th>  
  20.                     <th>Address</th>  
  21.                 </tr>  
  22.             </thead>  
  23.             <tbody>  
  24.                 <tr>  
  25.                     <td>1</td>  
  26.                     <td>Rohatash</td>  
  27.                     <td>Delhi</td>  
  28.                 </tr>  
  29.                 <tr>  
  30.                     <td>2</td>  
  31.                     <td>Smith</td>  
  32.                     <td>capetown</td>  
  33.                 </tr>  
  34.                 <tr>  
  35.                     <td>3</td>  
  36.                     <td>Rahul</td>  
  37.                     <td>Bombay</td>  
  38.                 </tr>  
  39.             </tbody>  
  40.         </table>  
  41.     </body>  
  42.   
  43. </html> 
The HTML will render with Bootstrap CSS as in the following:
 
Hover effect
 
It only changes color on rows that are white. Now you want to change the color on row hover other than white. To do that use table-hover and override the color. The following CSS defines that:
  1. <style type="text/css">  
  2.     .table-hover tbody tr:hover td  
  3.     {  
  4.         background-color: Blue;  
  5.         }       
  6. </style> 
The HTML file looks as in the following :
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title></title>  
  6.         <link href="Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />  
  7.         <style type="text/css">  
  8.         .table-hover tbody tr:hover td {  
  9.             background-color: Blue;  
  10.         }  
  11.   
  12.         body {  
  13.             margin: 50px;  
  14.         }  
  15.         </style>  
  16.     </head>  
  17.   
  18.     <body>  
  19.         <table class="table table-striped table-hover">  
  20.             <thead>  
  21.                 <tr>  
  22.                     <th> id</th>  
  23.                     <th> Name</th>  
  24.                     <th>Address</th>  
  25.                 </tr>  
  26.             </thead>  
  27.             <tbody>  
  28.                 <tr>  
  29.                     <td>1</td>  
  30.                     <td>Rohatash</td>  
  31.                     <td>Delhi</td>  
  32.                 </tr>  
  33.                 <tr>  
  34.                     <td>2</td>  
  35.                     <td>Smith</td>  
  36.                     <td>capetown</td>  
  37.                 </tr>  
  38.                 <tr>  
  39.                     <td>3</td>  
  40.                     <td>Rahul</td>  
  41.                     <td>Bombay</td>  
  42.                 </tr>  
  43.             </tbody>  
  44.         </table>  
  45.     </body>  
  46.   
  47. </html> 
The HTML will render with Bootstrap as in the following:
 
 
It only highlights the background color of rows. Now you want to change the color on row hover with the foreground color. To do that use table-hover and override the color.
 
The following CSS defines that:
  1. .table-hover tbody tr:hover td  
  2. {  
  3.     background-color: Blue;  
  4.     colorred;  
  5.    

The HTML file looks as in the following :
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title></title>  
  6.         <link href="Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />  
  7.         <style type="text/css">  
  8.         .table-hover tbody tr:hover td {  
  9.             background-color: Blue;  
  10.             color: red;  
  11.         }  
  12.   
  13.         body {  
  14.             margin: 50px;  
  15.         }  
  16.         </style>  
  17.     </head>  
  18.   
  19.     <body>  
  20.         <table class="table table-striped table-hover">  
  21.             <thead>  
  22.                 <tr>  
  23.                     <th> id</th>  
  24.                     <th> Name</th>  
  25.                     <th>Address</th>  
  26.                 </tr>  
  27.             </thead>  
  28.             <tbody>  
  29.                 <tr>  
  30.                     <td>1</td>  
  31.                     <td>Rohatash</td>  
  32.                     <td>Delhi</td>  
  33.                 </tr>  
  34.                 <tr>  
  35.                     <td>2</td>  
  36.                     <td>Smith</td>  
  37.                     <td>capetown</td>  
  38.                 </tr>  
  39.                 <tr>  
  40.                     <td>3</td>  
  41.                     <td>Rahul</td>  
  42.                     <td>Bombay</td>  
  43.                 </tr>  
  44.             </tbody>  
  45.         </table>  
  46.     </body>  
  47.   
  48. </html> 
The HTML will render with Bootstrap as in the following:
 
Highlight table row on Hover


Similar Articles