简体   繁体   English

如何在悬停时更改整个div的背景颜色?

[英]How to change background colour of entire div on hover?

I'm making a website and want to have a link inside a div that changes colour when hovered over, however I've come across a problem. 我正在制作一个网站,并希望在div中有一个链接,该链接在悬停时会更改颜色,但是我遇到了问题。 Pre hover, I want a border around the div the same colour as the text in the link inside. 悬停前,我希望div周围的边框与内部链接中的文本具有相同的颜色。 The background colour should be white. 背景颜色应为白色。 Then, upon hover, I want the background colour to change to the colour of the text and border, and the text to become white. 然后,在悬停时,我希望背景色更改为文本和边框的颜色,并且文本变为白色。 Because I have padding inbetween the link and div border, it doesn't quite work as intended. 因为我在链接和div边框之间有填充,所以它并没有按预期工作。 Here is the source html/css: 这是源html / css:

HTML: HTML:

<div id="home">
    <a href="index.html">HOME</a>
</div>

CSS: CSS:

#home {
    border: 4px solid #00a651;
    font-size: 30px;
    padding: 10px 20px 10px 20px;
    margin: 20px 100px 20px 100px;
    display: inline-block;
}
#home a {
    background: #ffffff;
    color: #00a651; 
    text-align: center;
}
#home a:hover {
    background: #00a651;
    color: #ffffff;
}

When anywhere within the div is hovered over other than the link, nothing happens, and when you hover over the link the padding remains white. 当div中除链接以外的任何其他地方都悬停时,什么也没有发生,当您悬停在链接上时,填充保持白色。 What do I need to do to make it so the colour change happens when anywhere on the div is hovered over, and the whole divs colour changes? 我需要做些什么才能使div上的任何地方悬停在整个div上的颜色发生变化时发生颜色变化? Thanks, Brandon :) 谢谢,布兰登:)

You need to add the hover event to the div and the anchor: 您需要将悬停事件添加到div和锚点中:

 #home { border: 4px solid #00a651; font-size: 30px; padding: 10px 20px 10px 20px; margin: 20px 100px 20px 100px; display: inline-block; } #home a { background: #ffffff; color: #00a651; text-align: center; } #home:hover { background: #00a651; color: #ffffff; } #home:hover a { background: #00a651; color: #ffffff; } 
 <div id="home"> <a href="index.html">HOME</a> </div> 

 #home { font-size: 30px; margin: 20px 100px 20px 100px; display: inline-block; } #home a {background: #ffffff; border: 4px solid #00a651; padding: 10px 20px 10px 20px; color: #00a651; text-align: center; } #home a:hover {background: #00a651; color: #ffffff; } 
 <div id="home"> <a href="index.html">HOME</a> </div> 

I recommend using the jQuery library , if you know how to use it: 如果您知道如何使用jQuery库 ,我建议您使用它:

Link your jQuery download to your main html file or use a CDN. 将您的jQuery下载链接到您的主要html文件或使用CDN。

In your JavaScript file add a hover event on your link and use the css effect to to change the color of the div once you hover over the link. 在JavaScript文件中,在链接上添加一个悬停事件,并将鼠标悬停在链接上后,即可使用css效果更改div的颜色。 Then add another css effect so the link changes color as the div does. 然后添加另一个css效果,以便链接像div一样更改颜色。 Like this: 像这样:

 $(document).ready(function(){
   $('a').hover(function(){
     $('#home').css("background-color","#00a651");
     $('a').css("color", "chooseacolor");
  });
});

I am not entirely sure what you are asking of, so this is the best answer I can give! 我不确定您要问什么,所以这是我能给的最好的答案!

I don't think using :hover on DIV elements is a good idea. 我不认为在DIV元素上使用:hover是个好主意。 As I know, this practice is not fully supported on all browsers of all versions. 据我所知,所有版本的所有浏览器均未完全支持这种做法。 Why not use some JS? 为什么不使用一些JS?

But, anyway, if you need all area (including padding) in the DIV become clickable - you should rather make it "display:block" in CSS. 但是,无论如何,如果您需要DIV中的所有区域(包括填充)都可以单击-您应该在CSS中将其设置为“ display:block”。

 $(document).ready(function() { $('#home').hover(function() { $(this).css('background-color', '#00a651').find('a').css({ backgroundColor: '#00a651', color: '#FFFFFF'}); }, function() { $(this).css('background-color', '#FFFFFF').find('a').css({ backgroundColor: '#ffffff', color: '#00a651'}); }); }); 
 #home { border: 4px solid #00a651; font-size: 30px; padding: 10px 20px 10px 20px; margin: 20px 100px 20px 100px; display: inline-block; } #home a {background: #ffffff; color: #00a651; text-align: center; } /* There is no need to use :hover anymore if using a script */ 
 <!-- this goes to the HEAD section if not arleady there --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <div id="home"> <a href="index.html">HOME</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM