TeAmEr
TeAmEr

Reputation: 4773

Center text in div having a div inside of it

I have this code:

<div style="border:1px solid #afafaf;background-color:#efefef;width:100px;height:14px;">
    <div style="text-align:center;width:50px;height:14px;background-color:green;">
        50%
    </div>
</div>

How can i put the 50% in the middle if the 1st DIV , the 2nd div might have width of 0 to 100 px (progress bar)

Upvotes: 0

Views: 1060

Answers (2)

thirtydot
thirtydot

Reputation: 228162

Live Demo

Whatever width your progress bar has, it won't affect the text div.

HTML:

<div style="border:1px solid #afafaf;background-color:#efefef;width:100px;height:14px;position:relative">
    <div style="text-align:center;width:50px;height:14px;background-color:green;"></div>
    <div id="text">50%</div>
</div>

CSS:

#text {
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
    text-align: center
}

Upvotes: 5

Jan Zyka
Jan Zyka

Reputation: 17898

For example have a third div with margin-left:auto;margin-right:auto which centers this div into the middle

Upvotes: 0

Related Questions