简体   繁体   English

根据div的宽度大小应用不同的颜色?

[英]Apply different color depending of width size of a div?

I am trying to apply a different color to a div depending its width size. 我试图根据其宽度大小将不同的颜色应用于div。

My html is this 我的HTML是这个

<div class="container">
        <div class="progress"></div>
      </div>

And my CSS is this: 我的CSS是这样的:

div._progress{
    background-color: black;
    width: 100%;
    height: 20px;
    padding: 1px;
  }
  div.progress{
    background-color: green;
    border-radius: 25%;
    width: 50%;
    height: 100%;
    -webkit-transition:width 350ms ease-in-out, height 350ms ease-in-out;
    -moz-transition:width 350ms ease-in-out, height 350ms ease-in-out;
    -o-transition:width 350ms ease-in-out, height 350ms ease-in-out;
    transition:width 350ms ease-in-out, height 350ms ease-in-out;
  }

From my javascript I change the width size with document.getElementById...... 从我的JavaScript,我用document.getElementById ......更改宽度大小。

How can I change the color of the progress for example when the inside bar is the 90% of the container width to red? 例如,当内部栏是容器宽度的90%变为红色时,如何更改进度的颜色?

Something like this 像这样

在此处输入图片说明

Thanks 谢谢

you can use linear gradient property. 您可以使用线性渐变属性。 not necessary to do in js. 没必要在js中做。

 #grad1 { height: 20px; background: linear-gradient(to left, #FF5733 30%, #36FF33 30%, #36FF33 100%); } 
 <div id="grad1"></div> 

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

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