简体   繁体   English

覆盖没有类的div的背景颜色与nth-child?

[英]override background color from a div without class with nth-child?

I would like to change the background color from all the div under the div with class=hamburger-react .我想用class=hamburger-react更改 div 下所有 div 的背景颜色。

I don't have access to this piece of html, so I need to target the good div and !important override.我无权访问这段 html,所以我需要定位好的 div 和 !important 覆盖。

I tried with :我试过:

 div.close-overlay-btn:nth-child(2) { background: rgba(0, 100, 172, 0.411) !important; }
 <div class="close-overlay-btn"> <div class="hamburger-react" aria-expanded="true" role="button" tabindex="0" style="cursor: pointer; height: 48px; position: relative; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; user-select: none; width: 48px; outline: none; transform: rotate(-180deg);"> <div style="background: #dc3545;height: 3px;left: 8px;position: absolute;width: 32px;top: 13px;transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s;transform: rotate(-45deg) translate(-7.07px, 7.07px);"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 23px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; opacity: 0;"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 33px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; transform: rotate(45deg) translate(-7.07px, -7.07px);"></div> </div> </div>

But didnt work.但是没有用。

Any tips ?有小费吗 ? since these div have no class it's a little bit more complicated than expected由于这些 div 没有类,因此比预期的要复杂一些

Your reference in your selectors is invalid.您在选择器中的引用无效。 Try this:尝试这个:

.close-overlay-btn>.hamburger-react>div {}

There is no :nth-child(2) to .close-overlay-btn . .close-overlay-btn没有:nth-child(2)

.close-overlay-btn is the grandparent, .hamburger-react is the parent, and all of the other divs nested in .hamburger-react are the children to the parent. .close-overlay-btn是祖父母, .hamburger-react是父母,所有其他嵌套在.hamburger-react中的 div 都是父母的孩子。

 .close-overlay-btn>.hamburger-react>div { background: rgba(0, 100, 172, 0.411) !important; }
 <div class="close-overlay-btn"> <div class="hamburger-react" aria-expanded="true" role="button" tabindex="0" style="cursor: pointer; height: 48px; position: relative; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; user-select: none; width: 48px; outline: none; transform: rotate(-180deg);"> <div style="background: #dc3545;height: 3px;left: 8px;position: absolute;width: 32px;top: 13px;transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s;transform: rotate(-45deg) translate(-7.07px, 7.07px);"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 23px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; opacity: 0;"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 33px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; transform: rotate(45deg) translate(-7.07px, -7.07px);"></div> </div> </div>

If you really can't access/change the HTML , then the easy way is really using !important (which I do not advise in general) Solution 1如果您真的无法访问/更改HTML ,那么简单的方法就是使用!important (我一般不建议这样做)解决方案 1

Other - better - solution, is remove the inline styles with JavaScript - and implement the same CSS but without !important Solution 2其他更好的解决方案是使用 JavaScript 删除内联样式并实现相同的 CSS 但没有!important解决方案 2


You are using your selector the wrong way, try this:您以错误的方式使用选择器,试试这个:

.hamburger-react div

This will affect every div child of .hamburger-react这将影响.hamburger-react的每个 div 孩子

Solution 1解决方案 1

 .hamburger-react div { background: rgba(0, 100, 172, 0.411) !important; }
 <div class="close-overlay-btn"> <div class="hamburger-react" aria-expanded="true" role="button" tabindex="0" style="cursor: pointer; height: 48px; position: relative; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; user-select: none; width: 48px; outline: none; transform: rotate(-180deg);"> <div style="background: #dc3545;height: 3px;left: 8px;position: absolute;width: 32px;top: 13px;transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s;transform: rotate(-45deg) translate(-7.07px, 7.07px);"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 23px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; opacity: 0;"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 33px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; transform: rotate(45deg) translate(-7.07px, -7.07px);"></div> </div> </div>

Solution 2解决方案 2

 document.querySelectorAll('.hamburger-react div').forEach(el => el.style.removeProperty('background'))
 .hamburger-react div { background: rgba(0, 100, 172, 0.411); }
 <div class="close-overlay-btn"> <div class="hamburger-react" aria-expanded="true" role="button" tabindex="0" style="cursor: pointer; height: 48px; position: relative; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; user-select: none; width: 48px; outline: none; transform: rotate(-180deg);"> <div style="background: #dc3545;height: 3px;left: 8px;position: absolute;width: 32px;top: 13px;transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s;transform: rotate(-45deg) translate(-7.07px, 7.07px);"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 23px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; opacity: 0;"></div> <div style="background: rgb(255, 221, 2); height: 3px; left: 8px; position: absolute; width: 32px; top: 33px; transition: all 0.4s cubic-bezier(0, 0, 0, 1) 0s; transform: rotate(45deg) translate(-7.07px, -7.07px);"></div> </div> </div>

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

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