Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply bootstraps tr hover effect to a div

I am using Bootstrap for my css themes. The themes are compiled and saved into the database for use later on. This means that the pages only have access to the compiled css and not the less files.

Given that, how can I apply Bootstrap’s tr:hover effect to various divs? I need the color to be the same as what is defined for tr:hover and I can’t use less variables.

Bootstrap's style:

.table tbody tr:hover td,
.table tbody tr:hover th {
  background-color: #f5f5f5;
}

Is it possible to define a css element based on another one that is assigned to a tr? Is there a way to grab the style using jQuery?

Any suggestions?

like image 325
rboarman Avatar asked Sep 05 '12 01:09

rboarman


People also ask

Can you put a hover on a div?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.

Why Hover is not working on div?

You might have linked your stylesheet to your HTML file improperly. Some other CSS in the context of your project may be overriding the piece that you've given here. You might be running into browser compatibility issues with the :hover selector or something else in your code that is breaking the styling.

How do you hover an element?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.


1 Answers

Pure CSS Way:

Have a class for the div as .hoverDiv and the CSS for this:

.hoverDiv {background: #fff;}
.hoverDiv:hover {background: #f5f5f5;}

Fiddle: http://jsfiddle.net/bD5kS/

jQuery Way:

$(document).ready(function(){
    $(".hoverDiv").hover(function(){
        $(this).css("background", "#f5f5f5");
    }, function(){
        $(this).css("background", "#fff");
    });
});

Fiddle: http://jsfiddle.net/KQzwc/

To get the colour dynamically, use $(".table tbody tr:hover").css("background-color"):

$(document).ready(function(){
    $(".hoverDiv").hover(function(){
        $(this).css("background", $(".table tbody tr:hover").css("background-color"));
    }, function(){
        $(this).css("background", $(".table tbody tr").css("background-color"));
    });
});
like image 161
Praveen Kumar Purushothaman Avatar answered Oct 25 '22 21:10

Praveen Kumar Purushothaman