CSS进度条文本颜色用于对比填充和空白背景?
我想要 XHTML+CSS 进度条,在填充和空白背景区域之间具有对比色。
我的文本颜色有问题。 由于填充背景和空白背景对比度太高(这是一项要求),为了保持可读性,文本应该是双色的,以便与它们形成对比。 图片应该比文字更好地解释它:
深蓝色填充的进度条区域和白色空背景http://drdaeman.pp.ru/tmp/20090703/progress-bar-text-example.png 问题示例 http://drdaeman.pp.ru/tmp /20090703/progress-bar-text-problem.png
我当前的进度条实现很简单,但如上面的示例所示,在某些情况下文本可能难以阅读,这正是我想要解决的问题。
我当前(简化的)实现尝试(失败,因为如果没有定位 div.progress
,则 overflow:hidden
无法工作,由于内部 span
我无法定位该位置> 的 width
):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Progress bar test</title>
<style type="text/css">
div.progress_bar {
border: 1px #ccc solid; position: relative;
text-align: center; height: 32px;
}
div.progress_bar .progress {
height: 32px;
overflow: hidden; /* This does NOT work! */
}
div.progress_bar .progress div {
position: absolute; width: 100%; height: 32px;
z-index: 30; overflow: hidden;
background-color: #44a;
}
div.progress_bar span {
position: absolute; top: 0; left: 0; width: 100%;
z-index: 20;
color: #000;
}
div.progress_bar .progress span {
position: absolute; top: 0; left: 0; width: 100%;
z-index: 40;
color: #eee;
}
</style>
</head>
<body>
<!-- Can be of any (unknown) width. Think of "width: auto".
The 400px value is just to keep it small on a big monitor.
DON'T rely on it! -->
<div id="container" style="width: 400px;">
<div class="progress_bar">
<!-- div.progress is a dark filled area container -->
<div class="progress" style="width: 51%;">
<!-- Actually dark filled area -->
<div style="width: 51%;"></div>
<!-- Text (white).
Does not clip, even with overflow: hidden on parent! -->
<span>This is a test</span>
</div>
<!-- Text (black) -->
<span>This is a test</span>
</div>
</div>
</body>
</html>
上述内容的实时版本:http: //drdaeman.pp.ru/tmp/20090703/test2.html
以前的尝试: http://drdaeman.pp.ru/tmp/20090703/test.html< /a>
这些图像是 GIMP 编辑的原型,并不完全是此代码显示的内容。
添加:谢谢大家,特别是 Meep3D、Nosredna 和 Lachlan! 但是我仍然有一个问题 - 在我的情况下,进度条应该没有固定宽度并占据所有水平可用空间(width: auto;
或 width: 100%
是可以接受的) 。 但如果没有 width: 400px
规则,Lachlan 的代码就会崩溃。 如果可能的话,我仍然想避免使用 JavaScript。
I want to have XHTML+CSS progress bar with contrast colors between filled and empty background areas.
I have a problem with text color. Because filled and empty backgrounds are too contrast (this is a requirement), to remain readable the text should be double-colored to be contrast to both of them. The image should explain it better than words:
Progress bar with dark blue filled area and white empty background http://drdaeman.pp.ru/tmp/20090703/progress-bar-text-example.png
Example of the problem http://drdaeman.pp.ru/tmp/20090703/progress-bar-text-problem.png
My current progress bar implementation is trivial, but as example above shows, the text can be hard to read in some cases, which is exactly a problem I want to solve.
My current (simplified) implementation attempt (fails, because overflow: hidden
does not work without positioning div.progress
which I cannot position because of inner span
's width
):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Progress bar test</title>
<style type="text/css">
div.progress_bar {
border: 1px #ccc solid; position: relative;
text-align: center; height: 32px;
}
div.progress_bar .progress {
height: 32px;
overflow: hidden; /* This does NOT work! */
}
div.progress_bar .progress div {
position: absolute; width: 100%; height: 32px;
z-index: 30; overflow: hidden;
background-color: #44a;
}
div.progress_bar span {
position: absolute; top: 0; left: 0; width: 100%;
z-index: 20;
color: #000;
}
div.progress_bar .progress span {
position: absolute; top: 0; left: 0; width: 100%;
z-index: 40;
color: #eee;
}
</style>
</head>
<body>
<!-- Can be of any (unknown) width. Think of "width: auto".
The 400px value is just to keep it small on a big monitor.
DON'T rely on it! -->
<div id="container" style="width: 400px;">
<div class="progress_bar">
<!-- div.progress is a dark filled area container -->
<div class="progress" style="width: 51%;">
<!-- Actually dark filled area -->
<div style="width: 51%;"></div>
<!-- Text (white).
Does not clip, even with overflow: hidden on parent! -->
<span>This is a test</span>
</div>
<!-- Text (black) -->
<span>This is a test</span>
</div>
</div>
</body>
</html>
Live version of the above: http://drdaeman.pp.ru/tmp/20090703/test2.html
Previous attempt: http://drdaeman.pp.ru/tmp/20090703/test.html
The images are GIMP edited prototypes, and not exactly what this code displays.
Add: Thank you all, especially Meep3D, Nosredna and Lachlan! However I still have a problem — in my case progress bar should have no fixed width and take all horizontally available space (width: auto;
or width: 100%
are acceptable). But without width: 400px
rule Lachlan's code breaks. And I'd still like to avoid using JavaScript, if that's possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个答案使用
clip-path: inset(0 0 0 50%);
是很棒的。如此答案中所述,使用带有
background-clip
的背景线性渐变也是有趣的。This answer with the use of
clip-path: inset(0 0 0 50%);
is great.The use of a background linear gradient with a
background-clip
as described in this answer is also interesting.根据 Meep3D 的建议,复制 2 份文本。
将每个内容包裹在与容器宽度相同的 div 中。 “上部”div 被另一个 div 包裹,并以所需的百分比进行剪辑。
更新:删除了固定宽度。
“上部”div 的大小与其包装器的百分比成反比。
As per Meep3D's suggestion, take 2 copies of the text.
Wrap each in a div of the same width as the container. The "upper" div is wrapped with another div which clips at the desired percentage.
Update: removed the fixed widths.
The "upper" div is sized to the inverse percentage of its wrapper.
如何将进度条文本的第二个副本放入 div 内,并将 div 的溢出设置为隐藏,以便它随之显示?
--
更新:我也不是 javascript 专家,但我确信您可以找出对象的宽度,然后根据该宽度设置偏移量(如果宽度像您所说的那样灵活)。
What about putting a second copy of the progress bar text inside the div, and set the div's overflow to hidden, so it reveals with it?
--
Update: I am also not a javascript expert, but I am sure that you can find out the width of an object and then set the offset based upon that if the width is flexible as you say.
您可以:
You could:
您可以为“百分比”文本使用文本阴影。 唯一的缺点是它只能在最新的浏览器中运行。 只有 Firefox 3.5、Safari(所有版本)和 Chrome 2+ 支持它。
这是一个使用文本阴影的演示,其方式可以使您的进度可读。
http://www.w3.org/Style/Examples/007/ text-shadow#white
如果您愿意使用更多 JavaScript,您可以尝试这个 jQuery 插件:
http://kilianvalkhof.com/2008/javascript/text-shadow-in-ie-with-jquery/
文章说它仅适用于 IE,但是它可以在 Chrome 3(我正在使用的)、Firefox 3.5、Internet Explorer 和 Safari 中运行。 它可能适用于较旧的浏览器,但我还没有测试过。
You could use a text shadow for your "percentage" text. The only downside to this is that it would only work in the latest browsers. Only Firefox 3.5, Safari (all versions), and Chrome 2+ support it.
Here is a demo of using text-shadow in a way that would make your progress readable.
http://www.w3.org/Style/Examples/007/text-shadow#white
If you're willing to use more JavaScript, you could try this jQuery plugin:
http://kilianvalkhof.com/2008/javascript/text-shadow-in-ie-with-jquery/
The article says it works in IE only, however it works in Chrome 3 (what I'm using), Firefox 3.5, Internet Explorer, and Safari. It may work in older browsers but I haven't tested it.
Meep3D 有正确答案。 盒子的两个版本。 显示顶部的 n%。
更多选项:
使该区域变暗的数字
为白色数字或减轻
黑色数字的区域。
一个黑色的号码。 (这里的问题是红色的
与错误相关,因此您可以
玩其他三者组合
都是高对比度的颜色
互相对抗。)
Meep3D has the correct answer. Two versions of the box. Reveal n% of the top one.
More options:
number that either darkens the area
for a white number or lightens the
area for a black number.
a black number. (Problem here is red
is associated with error, so you can
play with other combinations of three
colors that are all high contrast
against each other.)
您需要 2 个样式不同的值。 并且固定宽度
https://codepen.io/kosachevlad/pen/dypEjBa
You need 2 values styled differently. And fixed width
https://codepen.io/kosachevlad/pen/dypEjBa