Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hover with a nth-child

i was wondering if it is possible to use a hover with a nth-child like so

#gallery a img:hover {
    display: block;
    height:300px;
    width:450px;
    position:absolute;
    z-index:99;
    margin-left:-112.5px;
    margin-top:-75px;
    -webkit-box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);
    -moz-box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);
    box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);

}

From this up here to some thing like this down here, only its not working

 #gallery a img:hover:nth-child(1n+4) {
        display: block;
        height:300px;
        width:450px;
        position:absolute;
        z-index:99;
        margin-left:-112.5px;
        margin-top:-75px;
        -webkit-box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);
        -moz-box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);
        box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.5);

    }
like image 328
Ivo Avatar asked Jan 07 '13 20:01

Ivo


People also ask

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.

What is hover in CSS with example?

The :hover CSS pseudo-class matches when the user interacts with an element with a pointing device, but does not necessarily activate it. It is generally triggered when the user hovers over an element with the cursor (mouse pointer).

What is nth child even?

tr:nth-child(even) or tr:nth-child(2n) Represents the even rows of an HTML table: 2, 4, 6, etc. :nth-child(7) Represents the seventh element.


1 Answers

#gallery a:hover:nth-child(1n+4)

Will work correctly but style the A tags instead of the IMG inside.

When you have markup like...

<a href="#"><img src=""/></a>
<a href="#"><img src=""/></a>
<a href="#"><img src=""/></a>
<a href="#"><img src=""/></a>
<a href="#"><img src=""/></a>

You cannot select the inner IMG and then try to apply an nth-child on it because there is only 1 IMG inside of the A tag.

Refer to the JSFIDDLE I created http://jsfiddle.net/fXS93/2/

Any change in how the IMG markup is wrapped will reset the CSS matching and NTH-CHILD calculation. This applies even if you are matching on a CLASS that all of the IMG share.

This is true for the latest FF, Chrome, and IE9.

like image 154
Louis Ricci Avatar answered Oct 04 '22 01:10

Louis Ricci