CSS:设置颜色为父母的背景色
从下面的图片:CSS:设置颜色为父母的背景色
我将如何使文本和图标的白色盒子里面是彩色的大框的背景一样吗?
这里是标记的外观
<div class="class-item blue-bg">
<!--Heading stuff-->
<div class="class-item-actions">
<span class="pick-up-icon">
<i class="fa fa-female fa-lg"></i>
<i class="fa fa-child"></i>
</span>
<span class="full-day-icon blue">AM/PM</span>
<span class="unapproved-projects-icon blue">
2 <i class="fa fa-th-large fa-lg fa-fw class-item-action"></i>
</span>
<i class="fa fa-unlock fa-lg></i>
</div>
</div>
所以基本上,我怎么能做出这么莫名其妙,我不需要向blue
类添加到full-day-icon
和unapproved-projects-icon
,而是它继承class-item
蓝色的background-color
?
编辑:下面是相关CSS
.class-item {
padding: 10px;
width: 90%;
color: white;
margin-bottom: 5px;
cursor: pointer;
opacity: 0.85;
}
.full-day-icon {
font-weight: bold;
background: white;
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
.unapproved-projects-icon {
background-color: white;
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
如果它的好供您使用CSS Variables,这里是一个整洁和简单的方法来做到这一点:
.class-item {
/** Specify the BG color in one place. **/
--class-item-bg: #0076A5;
padding: 10px;
width: 90%;
background-color: var(--class-item-bg);
color: white;
margin-bottom: 5px;
cursor: pointer;
opacity: 0.85;
}
.full-day-icon {
font-weight: bold;
background: white;
color: var(--class-item-bg);
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
.unapproved-projects-icon {
background-color: white;
color: var(--class-item-bg);
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
您可以检查this fiddle怎么看然后您可以使用JavaScript来更改属性,以便它影响使用它的所有CSS规则。
我喜欢这个。我有多个'class-item'应该是不同的颜色,但我想我可以看到一种方法来做到这一点。我认为这与我所寻找的最接近。谢谢! – Robert
不客气@Robert并确保您的生产环境支持此功能。 :) –
在CSS不能继承不同的属性。因此,子div无法继承它的父项的background-color
,因为它的color
属性。
.parent {
background-color: red;
}
.child {
color: inherit-background-color /* Not a way to do this without a preprocessor */
}
为什么不直接应用样式?
.full-day-icon {
color: #0076A5;
}
.class-item {
padding: 10px;
width: 90%;
background-color: #0076A5;
color: white;
margin-bottom: 5px;
cursor: pointer;
opacity: 0.85;
}
.full-day-icon {
font-weight: bold;
background: white;
color: #0076A5;
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
.unapproved-projects-icon {
background-color: white;
color: #0076A5;
padding: 5px;
border-radius: 3px;
font-size: 0.8em;
position: relative;
top: -1px;
}
<div class="class-item blue-bg">
<!--Heading stuff-->
<div class="class-item-actions">
<span class="pick-up-icon">
<i class="fa fa-female fa-lg"></i>
<i class="fa fa-child"></i>
</span>
<span class="full-day-icon blue">AM/PM</span>
<span class="unapproved-projects-icon blue">
2 <i class="fa fa-th-large fa-lg fa-fw class-item-action"></i>
</span>
<i class="fa fa-unlock fa-lg></i>
</div>
</div>
我不想直接应用样式,因为颜色是动态的,#0076A5只是默认设置。 无论如何,谢谢你的答案。 我环顾四周,希望有一些有'currentColor'或'transparency'的魔法,我可以做什么或什么。 谢谢! – Robert
目前还无法使用“父”选择器。所以你可能需要通过CSS将这两个类设置为相同的颜色。 –
你能提供你的CSS以及? – Mindless
@Mindless我添加了相关的CSS – Robert