Oam Psy
Oam Psy

Reputation: 8663

CSS issue: vertically aligning label DIV

I'm having issues aligning a <label> that is placed inside a div.

Here's my HTML:

<div class="one-whole index-border">
   <div class="score red score--primary">
      <label>20</label>
   </div>
</div>

Here's my CSS:

.one-whole {
100%;
}
.index-border {
border-bottom: 2px solid #D2D2D2;
}
.score {
border: none;
display: inline-block;
/*  margin: 0; */
line-height: 1;
width: 120px;
height: 100px;
text-align: center;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
-ms-border-radius: 6px;
-o-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
-moz-box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
color: white;
margin-bottom: 15px;
vertical-align: middle;
}
.red {
background: #CC0000;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #FF3400), color-stop(100%, #CC0000));
background-image: -webkit-linear-gradient(#FF3400, #CC0000);
background-image: -moz-linear-gradient(#FF3400, #CC0000);
background-image: -o-linear-gradient(#FF3400, #CC0000);
background-image: linear-gradient(#FF3400, #CC0000);
}
.score--primary {
border: 1px solid #CC0000;
font-size: 30px;
font-weight: bold;
}

I thought using vertical-align: middle would work, but no luck.

Here is a fiddle: http://jsfiddle.net/oampz/aH86E/

If there is any way I could refactor my code, it would help.

Thanks

Upvotes: 2

Views: 139

Answers (2)

Kolby
Kolby

Reputation: 2865

If you don't want to use table-cell you can always just set a line-height to the parent. I honestly prefer table-cell in cases where I know the container could change in height.

It will allow the content to always be centered, where with this method you would have to change line-height to match the height of your container.

.score{
    line-height: 100px;
}

Demo

Upvotes: 4

Mr. Alien
Mr. Alien

Reputation: 157314

You need display: table-cell; instead of using display: inline-block;

.score {
    /* Other properties */
    display: table-cell;
}

Demo


As far as refactoring of the code goes, you can safely remove proprietary properties for box-shadow, box-radius as well as gradient codes, now it depends on you that till what level do you want to support the legacy browsers.

Refactored CSS

.one-whole {
    100%;
}

.index-border {
    border-bottom: 2px solid #D2D2D2;
}

.score {
    border: none;
    display: table-cell;
    /*  margin: 0; */
    line-height: 1;
    width: 120px;
    height: 100px;
    text-align: center;
    border-radius: 6px;
    box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
    color: white;
    margin-bottom: 15px;
    vertical-align: middle;
}

.red {
    background-image: linear-gradient(#FF3400, #CC0000);
}

.score--primary {
    border: 1px solid #CC0000;
    font-size: 30px;
    font-weight: bold;
}

Also, I just saw that you are using rgba() so it is better to declare a fall back for that as well, so use,

.score {
    /* Other properties */
    box-shadow: 0 0 4px #333; /*Equivalent to rgb(51,51,51) */
    box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
}

Upvotes: 3

Related Questions