Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS border issues when using :hover on the tr

Tags:

css

I'm trying to highlight the row the mouse is over in a table of data. I'm trying to do this with a border-top and border-bottom. To help the readability i also have a light transparent png on alternate rows.

It seems that when I turn on and off the borders (works in IE8+ and FF) the rows jump around a little. I think I can fix it by having a a non-hover transparent border, rather than none at all. Is this x-browser compatible now days?

In Chrome, the highlighted row's border does not go away when you move the mouse off the row, why?

http://justinzaun.com/Tree/people/

Update: I've fixed the border issue in chrome where they wouldn't go away. I moved the border to the TDs rather than the TR. The rows are still jumping around though.

Thanks!

like image 769
Justin808 Avatar asked Jul 26 '11 18:07

Justin808


People also ask

How do I apply a border to the element on a mouse hover without affecting the layout in CSS?

Answer: Use the negative CSS margin If you apply the border around an element on mouse hover it will move the surrounding elements from its original position, this is the default behavior.

Can you put a border on a tr?

Not directly: adding a border to a tr isn't allowed.

How do you put a border on a tr tag?

Borders can be added to rows of table by adding border to <td> and <th> elements [This is basically a CSS trick to achieve (hack!) that as borders cannot be added to <tr> and <tbody> elements of table]. Add following styles to your CSS to get borders around rows or headers or table cells.

How do I hide TR border?

Use the CSS property border on the <td> s following the <tr> s you do not want to have the border. In my example I made a class noBorder that I gave to one <tr> . Then I use a simple selector tr.


1 Answers

put an transparent border on your normal state elements.

When the :hover is applied the size of the border changes the size the element takes up.

eg:

.myelement
{
        border:4px solid transparent;
}

.myelement:hover
{
    border: 4px solid green;
}

http://jsfiddle.net/mPmRA/

EDIT:- more specifically to your table (ugh: tables ... collapse border makes the above not work properly)

http://jsfiddle.net/mPmRA/1/

put the transperant border on the tr

tr
{
    border-top:4px solid transparent;
    border-bottom:4px solid transparent;
}

And for the hover do something like:

tr:hover td
{
    border-top:4px solid green;
    border-bottom:4px solid green;
}

The td borders will then appear ABOVE the row's border.

like image 184
James Khoury Avatar answered Sep 19 '22 00:09

James Khoury