简体   繁体   English

CSS形状和文本悬停更改颜色

[英]CSS shape and text hover change color

I have looked around and cannot find an answer for this, maybe I am just making my code too convoluted. 我环顾四周,找不到答案,也许我只是使我的代码太复杂了。

I have a div that is making up my menu links. 我有一个div构成菜单链接。 The wrapper div will be a link that contains a div for my CCS Circle and a span for my link text: 包装div将是一个链接,其中包含我的CCS Circle的div和链接文本的范围:

<div class="menu-item">
    <a href="#">
        <div class="menu-circle"></div>
            <span class="menu-text">Home</span>
        </a>
</div>

I am trying to make it so that when I hover ANYWHERE over the wrapper div it changes the color of the shape AND link. 我试图做到这一点,以便将鼠标悬停在包装div上的任何地方时,它会更改形状和链接的颜色。 So far I have only been able to manage individual hovers on the shape OR the link. 到目前为止,我只能管理形状或链接上的单个悬停。

my CSS is: 我的CSS是:

.menu-circle {
    width: 60px;
    height: 60px;
    background: black;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    margin:auto;
}

.menu-text{
    font-family: 'Oswald', sans-serif;
    font-weight:700;
    letter-spacing: .07em;
}

.menu-circle:hover{
    background:#e7e7e7;
}

.menu-text:hover{
    color:#e7e7e7;
}

.menu-item{
    text-align:center;
}

you can see the example here 你可以在这里看到例子

If you want to change the colors when you hover ANYWHERE on the wrapper div then you must call the hover state on the wrapper dive. 如果要在包装div上任何地方悬停时更改颜色,则必须在包装下潜中调用悬停状态。 Like this: 像这样:

mean-item:hover .menu-circle {change colors here}

This however would only change the circle when hovering anywhere in the wrapper div (the wrapper being menu-item) So you would need a second hover state for menu-text 但是,这仅会在将包裹器div中的任何地方悬停时改变圆圈(包裹器是菜单项),因此菜单文本需要第二种悬停状态

mean-item:hover .menu-text {change colors here}

See here for working example: 请参见此处的工作示例:

http://jsfiddle.net/s6jkja5r/ http://jsfiddle.net/s6jkja5r/

Add this code to your style 将此代码添加到您的样式中

menu-circle:hover ~ .menu-text{
    color:#e7e7e7;
}

DEMO DEMO

Try this in your css: 在您的CSS中尝试以下操作:

.menu-item:hover div, .menu-item:hover span {
    background:#e7e7e7;
    color:#e7e7e7;
}

It was a minor CSS change. 这是一个小的CSS更改。

.menu-item:hover .menu-circle {
    background-color: #f4f;
}

.menu-item:hover .menu-text {
    color: #f4f;
}

This fiddle should do exactly what you're trying to achieve: http://jsfiddle.net/9qg3vjm3/10/ 这个小提琴应该完全按照您要实现的目标进行操作: http : //jsfiddle.net/9qg3vjm3/10/

Check this fiddle 检查这个小提琴

This changed css will change the background color of the circle div and the text color only while hovering on the circle div.. 更改后的CSS仅在将鼠标悬停在div上时才会更改div的背景色和文本颜色。

CSS CSS

.menu-item:hover .menu-circle{
    background:#e7e7e7;
}

.menu-item:hover .menu-text{
    color:#e7e7e7;
}

HTML HTML

<div class="menu-item">
    <a href="#">
       <div class="menu-circle"></div>
       <span class="menu-text">Home</span>
    </a>
</div>

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

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