I am a beginner in CSS and HTML. This is what I am trying to achieve, a progress bar with text label on top of the progress. The position of the texts are fixed, the name is always on the leftmost part while the unit is always on the rightmost. I need multiple duplicates of this progress bar.
I was able to achieve the correct layout with only one copy.
.container {
width: 100vw;
height: 100vh;
background-color: #000000;
}
.stats-display-container {
position: absolute;
z-index: 1;
right: 0;
margin-top: 50px;
margin-right: 50px;
background-color: #86fffb1f;
}
.stats-progress-container {
display: block;
margin: 12px;
width: 200px;
height: 20px;
background-color: #00586199;
}
.stats-progress {
width: 40%;
height: 100%;
background-color: #03d5ff;
}
.stats-label {
position: absolute;
z-index: 1;
font-size: 12px;
top: 14px;
left: 16px;
color: #606060;
background-color: transparent;
}
.stats-unit {
position: absolute;
z-index: 1;
font-size: 12px;
top: 14px;
right: 16px;
color: white;
background-color: transparent;
}
<div class="container" id="view_container">
<div class="stats-display-container">
<div class="stats-progress-container">
<div class="stats-progress"></div>
<div class="stats-label">Temperature</div>
<div class="stats-unit">80°C</div>
</div>
</div>
</div>
However, If I duplicate a copy of the group of divs, the result became this.
.container {
width: 100vw;
height: 100vh;
background-color: #000000;
}
.stats-display-container {
position: absolute;
z-index: 1;
right: 0;
margin-top: 50px;
margin-right: 50px;
background-color: #86fffb1f;
}
.stats-progress-container {
display: block;
margin: 12px;
width: 200px;
height: 20px;
background-color: #00586199;
}
.stats-progress {
width: 40%;
height: 100%;
background-color: #03d5ff;
}
.stats-label {
position: absolute;
z-index: 1;
font-size: 12px;
top: 14px;
left: 16px;
color: #606060;
background-color: transparent;
}
.stats-unit {
position: absolute;
z-index: 1;
font-size: 12px;
top: 14px;
right: 16px;
color: white;
background-color: transparent;
}
<div class="container" id="view_container">
<div class="stats-display-container">
<div class="stats-progress-container">
<div class="stats-progress"></div>
<div class="stats-label">Temperature</div>
<div class="stats-unit">80°C</div>
</div>
<div class="stats-progress-container">
<div class="stats-progress"></div>
<div class="stats-label">Humidity</div>
<div class="stats-unit">50</div>
</div>
</div>
</div>
So, I have to change the top
value of the style of the texts to around 48px to get the correct result. But isn't a good solution because the number of progress bar that I am going to display is dynamic.
I don't understand why the x and y position of the second copy of the texts the same with the first copy? I mean they belong to different parent div. I thought absolute positioning will be with respect to its parent div only. What is the correct way to achieve this?