简体   繁体   English

CSS 元素伪不覆盖子元素样式

[英]CSS element pseudo not overwrite child element style

So I want to style an anchor element what contains other html elements.所以我想为包含其他 html 元素的锚元素设置样式。 The base code is:基本代码是:
HTML: HTML:

  <div class="container" id="sub-menu">
    <div class="row" data-bind="foreach: subMenu">
      <a href="#">
        <div class="col-md-3 col-sm-6">
            <h3><span>title</span></h3>
            <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim</div>
        </div>
      </a>
    </div>
  </div>

The bootstrap classes do not bother you :)引导类不会打扰你:)
And here is the CSS (generated from less):这是CSS(从less生成):

#sub-menu {
  margin-top: 10px;
}
#sub-menu a span {
  color: black;
}
#sub-menu a div {
  color: #6b6b6b;
}
#sub-menu a:hover {
  color: red !important;
  text-decoration: none;
}

The goal was when the user hover over the anchor the text color should be red.目标是当用户将鼠标悬停在锚点上时,文本颜色应为红色。 With this solution the red color not applied but the text decoration works well.使用此解决方案,未应用红色,但文本装饰效果很好。 Here is a fiddle edition这是一个小提琴版
If I set the color on the a level than it works good ( fiddle ) or not set the color at all ( fiddle ), but I need to set different color for the title and the body part.如果我在 a 级别上设置颜色比它效果好(小提琴)或根本不设置颜色(小提琴),但我需要为标题和正文部分设置不同的颜色。
I also tried to set the hover pseudo for each element ( fiddle demo ) but it's ugly that the decoration works all of them but the color only applied if I hover over the exact element.我还尝试为每个元素设置悬停伪(小提琴演示),但装饰对所有元素都起作用很丑陋,但颜色仅在我将鼠标悬停在确切元素上时才应用。 I want to achieve the second version, but with custom color like the first version.我想实现第二个版本,但使用像第一个版本一样的自定义颜色。
Thanks for any help.谢谢你的帮助。

EDIT: I forget to explain that an icon also be in the h3 tag so that is why the title in a span tag.编辑:我忘记解释一个图标也在 h3 标签中,这就是为什么在 span 标签中的标题。

if you're trying to color all the elements contained in the anchor element this should work如果您尝试为锚元素中包含的所有元素着色,这应该可行

#sub-menu a:hover *{
    color: red;
}

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

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