Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping text over two divs centered

Tags:

html

css

I'm trying to modified a code to do something a bit different but I can't get it to work. This is the code:

<div id="progress-bar" class="all-rounded">
<div id="progress-bar-percentage" class="all-rounded" style="width: 88%">88/100</div>
</div>

This renders:

enter image description here

Using this CSS:

.all-rounded {
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

.spacer {
    display: block;
}

#progress-bar {
    width: 100px;
    background: #cccccc;
}

#progress-bar-percentage {
    background: #3063A5;
    padding: 5px 0px;
    color: #FFF;
    text-align: center;
}

As you can see the text 88/100 is printed inside the inner div progress-bar-precentage so is centered relative to this div, the problem with this is if progress-bar-precentage width is very small the text will be out of place, my idea is to center it relative to the outter div progress-bar so it will always be in place regardless of the inner div, but i have no idea how to put the text on top and center, any ideas?

like image 829
DomingoSL Avatar asked Nov 08 '12 21:11

DomingoSL


People also ask

How do I overlay two divs?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

How do you prevent div elements from overlapping?

You need to take out the min-width from your CSS. When the page is sized smaller the min-width stops it from shrinking any further. Thus causing the overlap. Save this answer.

Can two divs overlap?

To overlap the div2, follow the given instructions: Set the value of position property, width, and height of the div2 same as the “div1”. Set the value of the z-index as “2” to place it in front of the first div. Set a different background color for the div2 as “rgb(0, 204, 255)”.


1 Answers

Try this - DEMO

HTML

<div id="progress-bar" class="all-rounded">
    <div id="progress-bar-percentage" class="all-rounded" style="width: 68%"><span>68/100</span></div>
</div>

CSS

#progress-bar {
    width: 100px;
    background: #cccccc;
    position: relative;
}

#progress-bar-percentage {
    background: #3063A5;
    padding: 5px 0px;
    color: #FFF;
    text-align: center;
    height: 20px;
}

#progress-bar-percentage span {
    display: inline-block;
    position: absolute;
    width: 100%;
    left: 0;
}
like image 120
Zoltan Toth Avatar answered Sep 22 '22 18:09

Zoltan Toth