Reputation: 2094
I'm trying to do something resembling health bar and I currently stuggle with centering the text within it. I exhausted my options, it was either off center when the text changed length or text goes outside the borders of bar.
Here's how it looks with my current CSS
.bar{
position: relative;
background: #1DA598;
height: 96px;
width: 16px;
border: 1px solid #333;
margin-top: 72px;
margin-left: 24px;
}
.barFiller{
width: 100%;
height: 90%;
transition: width .2s ease-in;
}
.barText{
position: absolute;
bottom: 50%;
top: 50%;
width: 100%;
transform: rotate(-90deg);
color: "white"
}
barFiller and barText are children of bar
setting margins on auto and bot top left right on 0 didnt help either. Any other options? It should be centered and be flexible as progress bar can go from 2 to 6 digits.
Upvotes: 1
Views: 387
Reputation: 6230
You can combine rotate
and translate
for the css transform
property like this
.bar {
border : 1px solid black;
width : 20px;
height : 100px;
position : relative;
}
@keyframes health {
0% {
height : 0;
}
100% {
height : 100%;
}
}
.barFiller {
animation: health 3s linear infinite;
width : 100%;
background : red;
position : absolute;
bottom : 0;
}
.barText {
position : absolute;
left : 50%;
top : 50%;
transform : translate(-50%, -50%) rotate(-90deg);
color: rgb(0, 255, 255);
mix-blend-mode: difference;
}
<div class="bar">
<div class="barFiller"></div>
<div class="barText">Logan</div>
</div>
Upvotes: 1
Reputation: 816
try this:
.barText{
position: absolute;
line-height: 96px;
top: 0;
bottom: 0;
right: 0;
left: 0;
transform: rotate(-90deg);
color: "white"
}
Upvotes: 0
Reputation: 532
Since you have the exact pixels of the bar you might as well just use the correct pixel values instead of percentages.
.bar {
position: relative;
background: #1da598;
height: 96px;
width: 16px;
border: 1px solid #333;
margin-top: 72px;
margin-left: 24px;
}
.barFiller {
width: 100%;
height: 90%;
transition: width 0.2s ease-in;
background: red;
}
.barText {
position: absolute;
height: 15px;
top: 40px;
transform: rotate(-90deg);
color: white;
}
https://codepen.io/rgarcianuskin/pen/MzMVdo
Upvotes: 0