:hover CSS is not working?

2019-07-12 13:46发布

I have created a CSS class as follows:

div.ycontent div.ppclisting { background-color:#cccccc; }
div.ycontent div.ppclisting:hover { background-color:#FFE5DF; }

I have applied the above style in one of my page, which is working quite fine in Firefox, IE 7.0+ and in other latest browsers. But When I am applying the same style in another page then its not working.

Can anyone tell me what could be the reason? why its not working in another page.

7条回答
聊天终结者
2楼-- · 2019-07-12 14:24

I got the answer why the :hover was not working on another page. Actually on second page the DOCTYPE tag was not added in the HTML page as below,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

Earlier it was simple <html> tag and due to which :hover on the div was not working. After adding doctype tag as shown above its working for me.

Thanks to all of you for help.

查看更多
闹够了就滚
3楼-- · 2019-07-12 14:25

If it's working in one place, but not another place, then either it's not being called correctly or it's not being applied correctly.

Use Firebug, and see what properties the element that should have the styles really have. Then check the classnames for typos. Usually, that'll solve the problem as described.

查看更多
forever°为你锁心
4楼-- · 2019-07-12 14:30

Also, in IE6, :hover only works with a elements. Keep that in mind.

查看更多
狗以群分
5楼-- · 2019-07-12 14:31

Just had a similar problem. Make sure you're stacking correctly! That is, the z-index.

查看更多
虎瘦雄心在
6楼-- · 2019-07-12 14:31

I am in agreeance with Brandon.

I will also add..

If you remove the:

div.ycontent 

section from your lines, such that it looks like this:

.ppclisting { 
background-color:#cccccc; 
}

.ppclisting:hover { 
background-color:#FFE5DF; 
}

You may find it will work on your other page.

Why? Because you have defined these styles as 'classes'. Classes intend to apply the same style numerous times.

By placing the "div.ycontent" before it, you are essentially 'restricting'/ not utilising the pull potential of CSS classes.

查看更多
Ridiculous、
7楼-- · 2019-07-12 14:36

I just had position:absolute on a parent break my :hover's, although it doesn't seem to be an issue as a grandparent. I'm new to all of this so I don't know if that's normal or not.

Amendment: This turned out to be a z-index issue for me. I needed to put -1 on the parent, 0 on any siblings with position specified and 1 on the divs I wanted hover activity for.

查看更多
登录 后发表回答