Lighten parent's (unknown) background-color in child

前端 未结 3 1310
终归单人心
终归单人心 2020-12-04 03:15

Is there a way in less or (css3 in general) to access parent\'s background-color without knowing it?

Something like:

.child-class {
             


        
3条回答
  •  隐瞒了意图╮
    2020-12-04 04:01

    EDIT: Have a look at ScottS's answer for a clever solution that is probably what you need.

    That cannot be done in pure css. The reason is that these types of references could lead to 'circular' situations. Or situations where the DOM has to be looped over multiple times to determine the final value. Both these situations are something css always avoids.

    Now, if your main goal is to define a 'pallette' of sorts and have all your colors work together, you should look into CSS expansion tools like "SASS" or "LESS". They could let you define colors and change them and do other useful comparison things. Have a look at those tools if you want such flexibility. They cannot however, do direct comparison like you ask, since they just reduce to css in the end.

    This can easily be done using JavaScript, and even more easily using jQuery. I wont clutter this answer with JavaScript/jQuery code when it probably isn't what you need.

提交回复
热议问题