Search code examples
phpcsshtmlpositioning

how do i make numbers expand left and right over a centrally positioned CSS element?


I have been wracking my head trying many different methods trying to achieve an effect for a health element.

What I want to happen is have a central median push a value left, and push another value right.

I have tried floats, which kind of work, but does not give me the effect I am looking for.

Positioning absolutely causes them to overlap the central median when exceeding a certain length.

Here's what ALMOST gets it right.

.travel-spacing {
    margin-bottom: 20px;
}

.stat-wrapper {
    height: 170px;
    width: 150px;
}

.stat-label-align {
    float: left;
    width: 150px;
    margin: 0 auto;
    text-align: center;
}

.stat-align {
    float: left;
    width: 150px;
    margin: 0 auto;
    text-align: center;
}

.val-1 {
    float: left;
    position: relative;
    margin-left: 35px;

}

.center-divide-h {
    position: absolute;
    width: 150px;
    margin: 45px auto auto -150px;
    text-align: center;
}

.center-divide-m {
    position: absolute;
    width: 150px;
    margin: 60px auto auto -150px;
    text-align: center;
}

.center-divide-s {
    position: absolute;
    width: 150px;
    margin: 75px auto auto -150px;
    text-align: center;
}

.val-2 {
    float: right;
    margin-right: 35px;
}

This is the PHP:

<div class="stat-wrapper">
<?php echo "<div class ='stat-label-align'>Level:</div> <span class=\"stat-align\">" . $playerLevel . "</span>"; ?><br />
<?php echo "<div class ='stat-label-align'>Exp:</div> <span class=\"stat-align\">" . $playerExp . "</span>"; ?><br />
<?php echo "<div class ='stat-label-align'>Health:</div> <span class=\"val-1\">" . $maxHealth . " </span><span class='center-divide-h'>/</span><span class='val-2'> " . $maxHealth . "</span>"; ?><br />
<?php echo "<div class ='stat-label-align'>Mana:</div> <span class=\"val-1\">" . $maxMana . " </span><span class='center-divide-m'>/</span><span class='val-2'> " . $maxMana . "</span>"; ?><br />
<?php echo "<div class ='stat-label-align'>Stamina:</div> <span class=\"val-1\">" . $maxStamina . "</span> <span class='center-divide-s'>/</span><span class='val-2'> " . $maxStamina . "</span>"; ?><br />
<?php echo "<div class ='stat-label-align'>Coins:</div> <span class=\"stat-align\">" . $money . "</span>"; ?><br />
</div>
<div class="travel-spacing"></div>

How I want it:

 5 / 5000
40 / 5000 

It is not as clean as I normally do it, but was trying everything I could think of. Thanks for any insight!


Solution

  • I think I understand what you're after. Like this?

    If so, take a look at the updated CSS:

    .val-1 {
        display:inline-block;
        text-align:right;
        width:65px;
    }
    .center-divide {
        display:inline-block;
        width: 20px;
        text-align: center;
    }
    .val-2 {
        display:inline-block;
        width:65px;
    }
    

    By using display:inline-block I can apply a fixed width to <span> elements, and by using suitable numbers and alignments I can produce the effect you want.