Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text in bootstrap 3 progress bar?

I'm trying to display a progress bar, using Bootstrap 3, with the following code:

   <div class="progress">
        <div class="progress-bar" style="width: 60%;">
        </div>
        <span>60%</span>
    </div>

Screenshot of output:

screenshot

However, this causes the text '60%' to be displayed towards the right, rather than in the center of the progress bar. How can I center this text, so it appears in the center?

like image 650
Ali Avatar asked Jan 29 '14 10:01

Ali


3 Answers

I would put a class on the span tag, and put the tag before the progress-bar class. Then set the span to position:absolute and give progress text-align:center:

HTML:

<div class="progress">
    <span class="progress-value">60%</span>
    <div class="progress-bar"></div>
</div>

CSS:

.progress {
    text-align:center;
}
.progress-value {
    position:absolute;
    right:0;
    left:0;
}

See demo: http://jsfiddle.net/bozdoz/fSLdG/2/

like image 147
bozdoz Avatar answered Nov 09 '22 23:11

bozdoz


Adding to @bozdoz answer:

Absolutely positioning the progress percentage indicator will do the trick:

HTML

<div class="progress">
    <div class="progress-bar" style="width: 60%;">
    </div>
    <span>60%</span>
</div>

CSS

.progress {
    position:relative;
}
.progress span {
    position:absolute;
    left:0;
    width:100%;
    text-align:center;
    z-index:2;
    color:white;
}

Fiddle: http://jsfiddle.net/Varinder/ejgp5/

like image 34
Varinder Avatar answered Nov 10 '22 00:11

Varinder


Twitter's bootstrap .span classes are floated to the left. Try adding float:none to the span it might work!

.progress span{
   margin: 0px auto;
   float:none;
}

UPDATE: This works for sure: HTML

 <div class="progress">
  <div class="bar" style="width: 60%;"></div>
  <span>60%</span>
 </div>

CSS:

 .progress {
    position: relative;
 }

 .bar {
    z-index: 1;
    position: absolute;
  }

 .progress span {
    position: absolute;
    top: 0;
    z-index: 2;
    text-align: center;
    width: 100%;
    color: black;
 } 
like image 1
Abhay Desai Avatar answered Nov 10 '22 00:11

Abhay Desai