所以我想做的就是根据其高度更改div元素的颜色。
例如: 如果div的高度<= 20%,我希望它是绿色,如果它的20%以上,它应该是蓝色。
我想仅使用CSS实现此操作(如果可能的话)
答案 0 :(得分:10)
这是一个带有渐变背景的技巧,您可以依靠它来调整背景大小并重复。想法是使大小为负值(无着色)或为正值,而重复进行,您将获得完整的着色。
这里是一个示例,其中我定义了3个范围:从0到100像素(橙色),从100像素到200像素(蓝色),大于200像素(红色)。
我正在手动设置高度,但是可以根据内容自动设置高度:
.box {
min-height:50px;
margin:10px;
border:1px solid;
background:
linear-gradient(red,red) left/100% calc(100% - 200px),
linear-gradient(blue,blue) left/100% calc(100% - 100px),
orange;
}
<div class="box"></div>
<div class="box" style="height:120px"></div>
<div class="box" style="height:220px"></div>
同样可以使用宽度(调整屏幕尺寸以进行测试):
.box {
min-height:50px;
margin:10px;
border:1px solid;
background:
linear-gradient(red,red) left/calc(100% - 800px) 100%,
linear-gradient(blue,blue) left/calc(100% - 600px) 100%,
orange;
}
<div class="box"></div>
我们也可以将相同的技巧扩展到文本着色:
.box {
min-height:50px;
margin:10px;
font-size:20px;
border:1px solid #000;
background:
linear-gradient(red,red) left/100% calc(100% - 200px),
linear-gradient(blue,blue) left/100% calc(100% - 100px),
orange;
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
color: transparent;
}
<div class="box"> I am an orange text</div>
<div class="box" style="height:120px"> I am a blue text</div>
<div class="box" style="height:220px"> I am a red text</div>
也有边界:
.box {
min-height:50px;
margin:10px;
border:5px solid transparent;
background:
/*Background coloration (color padding-box)*/
linear-gradient(red,red) padding-box,
linear-gradient(blue,blue) padding-box,
linear-gradient(orange,orange) padding-box,
/*Border coloration (color the border-box)*/
linear-gradient(purple,purple) border-box,
linear-gradient(green,green) border-box,
linear-gradient(#000,#000) border-box;
background-size:
100% calc(100% - 200px),
100% calc(100% - 100px),
100% 100%;
}
<div class="box"></div>
<div class="box" style="height:120px"></div>
<div class="box" style="height:220px"></div>
最后,我们可以同时具有边框,文本和背景(仅适用于chrome)
.box {
min-height:50px;
margin:10px;
font-size:25px;
border:5px solid transparent;
background:
/*Text coloration*/
linear-gradient(yellow,yellow) ,
linear-gradient(grey,grey) ,
linear-gradient(#fff,#fff),
/*Background coloration*/
linear-gradient(red,red),
linear-gradient(blue,blue),
linear-gradient(orange,orange),
/*Border coloration*/
linear-gradient(purple,purple),
linear-gradient(green,green),
linear-gradient(#000,#000);
background-size:
100% calc(100% - 200px),
100% calc(100% - 100px),
100% 100%;
-webkit-background-clip:
text,text,text,
padding-box,padding-box,padding-box,
border-box,border-box,border-box;
background-clip:
text,text,text,
padding-box,padding-box,padding-box,
border-box,border-box,border-box;
-webkit-text-fill-color: transparent;
color: transparent;
}
<div class="box">some text here</div>
<div class="box" style="height:120px">some text here</div>
<div class="box" style="height:220px">some text here</div>
对于以上所有内容,我们都可以根据高度和宽度进行着色。
这是一个简化的交互式演示:
.box {
padding:10px;
display:inline-block;
margin:10px;
font-size:20px;
resize:both;
overflow:auto;
border:1px solid;
background:
linear-gradient(green,green),
linear-gradient(red,red),
linear-gradient(blue,blue),
linear-gradient(orange,orange),
yellow;
background-size:
calc(100% - 400px) calc(100% - 300px),
calc(100% - 400px) calc(100% - 200px),
calc(100% - 200px) calc(100% - 100px),
calc(100% - 100px) calc(100% - 50px);
}
<div class="box">resize me</div>