繁体   English   中英

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

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

我想用class=hamburger-react更改 div 下所有 div 的背景颜色。

我无权访问这段 html,所以我需要定位好的 div 和 !important 覆盖。

我试过:

 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>

但是没有用。

有小费吗 ? 由于这些 div 没有类,因此比预期的要复杂一些

您在选择器中的引用无效。 尝试这个:

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

.close-overlay-btn没有:nth-child(2)

.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>

如果您真的无法访问/更改HTML ,那么简单的方法就是使用!important (我一般不建议这样做)解决方案 1

其他更好的解决方案是使用 JavaScript 删除内联样式并实现相同的 CSS 但没有!important解决方案 2


您以错误的方式使用选择器,试试这个:

.hamburger-react div

这将影响.hamburger-react的每个 div 孩子

解决方案 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>

解决方案 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