简体   繁体   English

继承颜色,然后使其更深/更亮

[英]Inherit colour and then make it darker/brighter

I have two div's one is .father and the other one is .child - as follows: 我有两个div,一个是.father,另一个是.child-如下:

<div class="father">
  <p></p>
</div>

<div class="child">
  <p></p>
</div>

As you can see, the child is not inside the father (sorry if this seems doggy, the only example blinked to me). 如您所见,孩子不在父亲之内(对不起,如果这看起来像狗狗,那唯一的例子对我眨眼了)。 What I am trying to do; 我正在尝试做的事; is when I give the .father a background colour lets say "background-color: #000". 是当我给.father一个背景色时说“ background-color:#000”。 I want the child to inherit the colour black BUT make it brighter/darker... 我希望孩子继承黑色但让它更明亮/更暗...

I have tried to do the following: 我尝试执行以下操作:

.child {
  background-color: -20%;
}

I don't know if that is a real way, I guess it's stupid - but I need to share what I did. 我不知道那是不是一种真实的方法,我想那是愚蠢的-但我需要分享自己的所作所为。 I have also tried to do it using CSS transparency but that will be applied to the whole div... what if the div has text inside it? 我也尝试过使用CSS透明性来做到这一点,但是它将应用于整个div ...如果div内有文本怎么办?

So for example, I wrap the div .child with another div and give that div a black background, then apply transparency to the div .child - but this will apply transparency to the text as well! 因此,例如,我将div .child与另一个div包裹在一起,并为该div提供黑色背景,然后将透明度应用于div .child-但这也将对文本应用透明度!

If I have added a around the how can I make that span inherit the colour of the div inside it? 如果在周围添加,如何使该范围继承其内部div的颜色? and inherit its size as well. 并继承其大小。

I'm afraid you cannot do that in pure CSS. 恐怕您不能在纯CSS中做到这一点。 Unless of course you work with rgba colors to make it transparent. 除非您当然要使用rgba颜色使其透明。

There is however a function in SASS (and LESS) that allows to darken/lighten colors. 但是,SASS(和LESS)中有一个功能可以使颜色变暗/变亮。 Take a look at these function's reference here . 这里查看这些函数的参考。 More information about SASS/SCSS: http://sass-lang.com/ 有关SASS / SCSS的更多信息: http ://sass-lang.com/

You can set for the child background the same color that for the father, and set a semitransparent pseudo element over it. 您可以为子背景设置与父背景相同的颜色,并在其上设置半透明的伪元素。

If the background of the pseudo element is semi-transparent white, the background-color gets lighter; 如果伪元素的背景是半透明的白色,则背景色变浅;反之,则变浅。 if the pseudo element is semitransparent black, the child gets darker. 如果伪元素是半透明的黑色,则子级将变暗。

.father, .child { 
    background-color: red;
}

.child {
    position: relative;
}

.child:after {
    content: "";
    position: absolute;
    left: 0px;
    top: 0px;
    right: 0px;
    bottom: 0px;
    background-color: rgba(0,0,0,0.2);
}

.child:hover:after {
    background-color: rgba(255,255,255,0.3);
}

In this example, the child gets darker red, but turns to lighter red when hovered (and works the same if the base color is whatever you want 在此示例中,该子项的颜色变深,但在悬停时变为红色(如果基础色是您想要的颜色,则其作用相同

fiddle 小提琴

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

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