简体   繁体   English

HTML和CSS,父选择器?

[英]HTML & CSS, parent selector?

So my problem is that I want to change my <div> background-color and my <h4> to red , either I mouse in a <div> or <h4> . 所以我的问题是我想将<div>背景色和<h4>更改为red ,或者将鼠标放在<div><h4>

在此处输入图片说明

This is my html (without unnecessary classes). 这是我的html(没有不必要的类)。

<div>
    <a href="#"><div><span class="icon-bubbles3"></span></div></a>
    <a href="#"><h4>News</h4></a>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

So I've been trying to apply h4:hover {color: red} then I wanted to target also also my <div> while hovering <h4> , but there is no something like "parent selector" to target my div. 因此,我一直在尝试应用h4:hover {color: red}然后我也想在悬停<h4>时也定位我的<div> <h4> ,但是没有像“父选择器”这样的目标定位我的div。 So how can i do this? 那我该怎么办呢? (I don't want to wrap either <div> and <h4> with <a> tag). (我不想用<a>标签包装<div><h4> )。

Thanks! 谢谢!

First of all you need a class on your outer-most div in order to be able to directly reference this without also referencing the "inner div": 首先,您需要在最外面的div上创建一个类,以便能够直接引用该类,而不必同时引用“ inner div”:

<div class="mydiv">
    <a href="#"><div><span class="icon-bubbles3"></span></div></a>
    <a href="#"><h4>News</h4></a>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

Then you can call: 然后,您可以致电:

.mydiv:hover div {
   background-color:red;
}
.mydiv:hover h4 {
   color:red;
}

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

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