英文:
Adding text in this CSS/progress bar area
问题
我用JavaScript创建了一个简易的进度条。基本上是这样做的:
HTML:
<div class="progress-bar">
<div id="heat_power" class="heat_progress"></div>
</div>
<center>Heat: <label id='heat_text'>0</label>%</center>
<br>
<div class="progress-bar">
<div id="cool_power" class="cool_progress"></div>
</div>
<center>Cool: <label id='cool_text'>0</label>%</center>
CSS:
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
}
.progress {
height: 100%;
border-radius: 10px;
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
width: 0%;
}
我用SSE获取的数据更新了“progress”类的进度条。
我想把文字添加到进度条上,显示实际值。
不幸的是,我尝试过用“progress”类来做这个,但因为它实际上是一个动态变化的宽度,看起来不好看。
所以,我尝试将其添加到“progress-bar”类,虽然文字被添加了,但看不到“progress”类。(就像它被文本覆盖了一样)
但我更希望将文本放在进度条内部。
有什么建议吗?
英文:
I've created a rudimentary progress bar using Javascript. I've basically done this:
HTML:
<div class="progress-bar">
<div id="heat_power" class="heat_progress"></div>
</div>
<center>Heat: <label id='heat_text'>0</label>%</center>
<br>
<div class="progress-bar">
<div id="cool_power" class="cool_progress"></div>
</div>
<center>Cool: <label id='cool_text'>0</label>%</center>
CSS:
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
}
.progress {
height: 100%;
border-radius: 10px;
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
width: 0%;
}
I update the ".progress" bar with data that comes over an SSE.
I would like to add the text to the progress bar of the literal value.
Unfortunately, I tried doing this with the ".progress" class, but because it's actually a dynamically changing width it does not look good.
So, I tried to add it to the ".progress-bar" class and while the text was added I could not see the ".progress" class. (Like it was layered under the text)
I am currently putting the text underneath like so:
But I would much prefer to put the text inside the bar.
Any tips on how to do this?
答案1
得分: 1
只需使用position:absolute
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
position: relative;
}
.progress {
height: 100%;
border-radius: 10px;
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
width: 0%;
position: relative;
}
.progress-bar center {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
<div class="progress-bar">
<div id="heat_power" class="progress" style="width:30%"></div>
<center>Heat: <label id='heat_text'>30</label>%</center>
</div>
or
<div class="progress-bar">
<div id="heat_power" class="progress" style="width:30%">
<center>Heat: <label id='heat_text'>30</label>%</center>
</div>
</div>
英文:
Just use position:absolute
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
position:relative;
}
.progress {
height: 100%;
border-radius: 10px;
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
width: 0%;
position:relative;
}
.progress-bar center {
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}
<!-- language: lang-html -->
<div class="progress-bar">
<div id="heat_power" class="progress" style="width:30%"></div>
<center>Heat: <label id='heat_text'>30</label>%</center>
</div>
or
<div class="progress-bar">
<div id="heat_power" class="progress" style="width:30%"> <center>Heat: <label id='heat_text'>30</label>%</center></div>
</div>
<!-- end snippet -->
答案2
得分: 0
如果您可以更改HTML,可以在进度条(div)内部添加"label"。然后为进度条指定相对定位(position: relative),接着绝对定位(label)在其中。
我还做了一些随机更新,例如为进度条添加了一个通用的progress类,您可以应用通用CSS样式,然后使用CSS选择器来更改特定部分的样式。并且不再使用center,而是使用CSS中的text-align属性来居中文本。
<div class="progress-bar heat_progress">
<div class="label">Heat: <label>0</label></div>
<div class="progress"></div>
</div>
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
position: relative;
}
.progress {
height: 100%;
border-radius: 10px;
width: 0%;
}
.heat_progress .progress {
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
}
.progress-bar .label {
position: absolute;
width: 100%;
top: 2px;
text-align: center;
}
let heat_progress = document.querySelector(".heat_progress");
let percent = 0;
function updateProgress() {
if (percent < 100) {
percent += 10;
heat_progress.querySelector(".progress").style.width = percent + "%";
heat_progress.querySelector("label").textContent = percent + "%";
setTimeout(updateProgress, 1000);
}
}
setTimeout(updateProgress, 1000);
英文:
If you can change the HTML, you can add the "label" within the progress bar div. Then give the progress bar a position of relative. Then absolutely position the label within it.
I also made some random updates. Like I gave the progress bar a generic progress class that you can apply the general CSS to then use a CSS selector to just change the heat specific part. And got rid of center and used CSS text align center.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let heat_progress = document.querySelector(".heat_progress")
let percent = 0;
function updateProgress(){
if(percent < 100){
percent += 10;
heat_progress.querySelector(".progress").style.width = percent + "%"
heat_progress.querySelector("label").textContent = percent + "%"
setTimeout(updateProgress,1000)
}
}
setTimeout(updateProgress,1000)
<!-- language: lang-css -->
.progress-bar {
height: 20px;
background-color: #ddd;
border-radius: 10px;
position:relative
}
.progress {
height: 100%;
border-radius: 10px;
width: 0%;
}
.heat_progress .progress{
background: linear-gradient(to right, #ffbf00 0%, #ff007f 100%);
}
.progress-bar .label{
position:absolute;
width:100%;
top:2px;
text-align:center;
}
<!-- language: lang-html -->
<div class="progress-bar heat_progress">
<div class="label">Heat: <label>0</label></div>
<div class="progress"></div>
</div>
<!-- end snippet -->
答案3
得分: 0
You can use CSS variables:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论