简体   繁体   English

CSS Background-Size不适用于静态颜色

[英]CSS Background-Size doesn't work with static color

I am very bad in CSS and I need to accomplish something complicated for me. 我在CSS中表现很差,我需要完成一些复杂的事情。

I got a table with data and I have a column where I have something like Qty Assigned / Qty Requested . 我有一个包含数据的表,并且有一个列,其中有类似“已Qty Assigned / Qty Requested I calculate the % assigned so 5 assigned of 10 will be 50%. 我计算分配的百分比,因此10中的5个分配将为50%。 Now, I want to put a background-color on the TD who match the %. 现在,我想在与%相匹配的TD上放置背景色。 So 50% will be half of the TD in let's say red. 因此50%将是红色的TD的一半。

I have tried to use background-color: red; background-size: <?php echo $BgPct; ?>%; 我尝试使用background-color: red; background-size: <?php echo $BgPct; ?>%; background-color: red; background-size: <?php echo $BgPct; ?>%; but it doesn't work. 但这不起作用。

Anyone have solutions ? 有人有解决方案吗? I can use whatever. 我可以使用任何东西。 I have jQuery also. 我也有jQuery。 I don't care if it's CSS1/2/3, but it need to be accurate so 66% isn't a background picture of 75%. 我不在乎它是否是CSS1 / 2/3,但它必须准确,因此66%的背景图片不是75%。

You can use linear-gradient for this 您可以为此使用linear-gradient

For example, if you need 50% of red 例如,如果您需要50%的红色

td {
  background: linear-gradient(to right, red 50%, transparent 0);
}

Demo: 演示:

 table { border-collapse: collapse; } td { border: 1px solid gray; padding: 10px; } .half { background: linear-gradient(to right, red 50%, transparent 0); } .one-third { background: linear-gradient(to right, red 33.33%, transparent 0); } .three-quaters { background: linear-gradient(to right, red 75%, transparent 0); } 
 <table> <tr> <td class="half">50%</td> </tr> <tr> <td class="one-third">33%</td> </tr> <tr> <td class="three-quaters">75%</td> </tr> </table> 

Zero 0 second value is used intentionally to avoid duplicating first value. 0第二值被有意用来避免重复第一个值。 This works because next value in gradient can't be less than previous. 之所以可行,是因为渐变中的下一个值不能小于上一个。

You can place a div inside another div. 您可以在另一个div中放置一个div。 Make both divs have the same height. 使两个div具有相同的高度。 The inner div will have a background color (red). 内部div将具有背景色(红色)。 Then you can set the width of the div some % and thats how much it will be filled in. 然后,您可以将div的宽度设置为%,也就是要填充的宽度。

This is how twitter bootstrap implements its progress bars. 这就是twitter引导程序实现其进度栏的方式。 If you want a vertically filling bar I think you can set the height to a % and make the widths the same. 如果您想要垂直填充的条,我认为您可以将高度设置为%,并使宽度相同。

Here is the twitter bootstrap example: 这是twitter引导程序示例:

http://cssdeck.com/labs/twitter-bootstrap-progress-bars http://cssdeck.com/labs/twitter-bootstrap-progress-bars

Click the details tab to see the source code. 单击详细信息选项卡以查看源代码。

A basic implementation example: 一个基本的实现示例:

<div style="width:50%">
    <div style="width: 50%; background-color:red; text-align:center">Hello</div>
</div>

As suggested in comments, I have had good success with a single pixel background-image that is stretched to the appropriate percentage background-size of the table cell. 正如评论中所建议的那样,我将单个像素的background-image拉伸到表格单元格的适当background-size百分比方面取得了成功。 This works in all modern browsers (IE9+). 这适用于所有现代浏览器(IE9 +)。

This has the advantage (over using an additional div element to contain the "colored bar") of being independent of any other styling you might want to apply to the table cell, such as text-align , etc. It will naturally appear behind the cells content and flexes to fill the table cell if using percentages. 这样做的好处是(与使用其他div元素包含“彩色条”相比),它独立于您可能要应用于表格单元格的其他任何样式,例如text-align等。它自然会显示在单元格的内容,如果使用百分比,则弯曲以填充表格单元格。

CSS: CSS:

td.bargraph {
    background:transparent url(/img/dot-green.gif) 0 center no-repeat;
    text-align: center;
}

HTML: HTML:

<td class="bargraph" style="background-size:<?=$perc?>% 80%"><?=$perc?>%</td>

Where $perc is your calculated percentage. 其中$perc是您计算出的百分比。

Interesting question. 有趣的问题。 I would put a div , colored red, inside each of the td s you want to effect. 我会在要执行的每个td放入一个div (红色)。 Then use jQuery to get the number (string) inside the td and make it into a variable, which you can then use to set the width of the div . 然后使用jQuery获取td内的数字(字符串)并将其变成变量,然后可以使用该变量来设置div的宽度。

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

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