在CSS/进度条区域添加文本

huangapple go评论56阅读模式
英文:

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”类。(就像它被文本覆盖了一样)

我目前是这样将文本放在下面:
在CSS/进度条区域添加文本

但我更希望将文本放在进度条内部。
有什么建议吗?

英文:

I've created a rudimentary progress bar using Javascript. I've basically done this:
HTML:

    &lt;div class=&quot;progress-bar&quot;&gt;
        &lt;div id=&quot;heat_power&quot; class=&quot;heat_progress&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;center&gt;Heat: &lt;label id=&#39;heat_text&#39;&gt;0&lt;/label&gt;%&lt;/center&gt;
    &lt;br&gt;
    &lt;div class=&quot;progress-bar&quot;&gt;
        &lt;div id=&quot;cool_power&quot; class=&quot;cool_progress&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;center&gt;Cool: &lt;label id=&#39;cool_text&#39;&gt;0&lt;/label&gt;%&lt;/center&gt;

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:
在CSS/进度条区域添加文本

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 -->

&lt;div class=&quot;progress-bar&quot;&gt;
        &lt;div id=&quot;heat_power&quot; class=&quot;progress&quot; style=&quot;width:30%&quot;&gt;&lt;/div&gt;
        &lt;center&gt;Heat: &lt;label id=&#39;heat_text&#39;&gt;30&lt;/label&gt;%&lt;/center&gt;
    &lt;/div&gt;
    
    or 
    
    &lt;div class=&quot;progress-bar&quot;&gt;
        &lt;div id=&quot;heat_power&quot; class=&quot;progress&quot; style=&quot;width:30%&quot;&gt; &lt;center&gt;Heat: &lt;label id=&#39;heat_text&#39;&gt;30&lt;/label&gt;%&lt;/center&gt;&lt;/div&gt;
       
    &lt;/div&gt;

<!-- 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(&quot;.heat_progress&quot;)
let percent = 0;

function updateProgress(){
    if(percent &lt; 100){
        percent += 10;
        
        heat_progress.querySelector(&quot;.progress&quot;).style.width = percent + &quot;%&quot;
        heat_progress.querySelector(&quot;label&quot;).textContent = percent + &quot;%&quot;
        
        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 -->

&lt;div class=&quot;progress-bar heat_progress&quot;&gt;
  &lt;div class=&quot;label&quot;&gt;Heat: &lt;label&gt;0&lt;/label&gt;&lt;/div&gt;
  &lt;div class=&quot;progress&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

You can use CSS variables:

https://codesandbox.io/s/beautiful-bouman-5mo3v2

英文:

Also you can use css variable:

https://codesandbox.io/s/beautiful-bouman-5mo3v2

huangapple
  • 本文由 发表于 2023年5月25日 03:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定