musium
musium

Reputation: 3072

Center content of inline-block div elements

I try to do something like this in CSS/HTML:

example

Each “Box” should be a link to the details of the error. The text and the red div inside of the box should be horizontally centered, as shown in the image.

Here is my code: JSFiddle

.outerDiv {
    display: inline-block;
    border-radius: 10px;
    border: 2px solid #c3c3c3;
    height: 100px;
    width: 100px;
    margin: 5px;
}

.innerDiv {
    width: 50%;
    height: 100%;
    margin: 0 auto;
}

.errorLabel {    
    background-color: #a90329;
    display: inline;
    padding: .2em .6em .3em;
    font-size: 75%;
    color: #fff;
    border-radius: .25em;
    line-height: 1;
    vertical-align: baseline;
    font-weight: 700;
    text-align: center;
    white-space: nowrap;
    box-sizing: border-box;
}

Edit: After the answer of Fabrizio Calderan

If detected two small issue.

New JsFiddle

I was able to fix the first issue by turning the label into a block element. Now I’m looking for a way to make sure that all label have the same space to the bottom border. (It should look like the image, and not like the fiddle)

enter image description here

Upvotes: 0

Views: 536

Answers (2)

musium
musium

Reputation: 3072

I found a solution for my problem: JsFiddle

.outerDiv {
    text-align: center;
    float: left;
    border-radius: 10px;
    border: 2px solid #c3c3c3;
    height: 100px;
    width: 100px;
    margin: 5px;
    line-height: 50px;
}

.errorLabel {
  display:inline-block;
    vertical-align: bottom;
    margin-bottom:7px;
    width:84%;
    background-color: #a90329;
    padding: .2em .6em .3em;
    color: #fff;
    border-radius: .25em;
    line-height: 1;
    font-weight: 700;
    white-space: nowrap;
    box-sizing: border-box;
}

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123387

add a line-height to the inner div, e.g. line-height: 50px; and text-align: center to the outer div

http://jsfiddle.net/2mxud8uk/3/

.outerDiv {
    text-align: center;
    ...
}

.innerDiv {
    line-height: 50px;
    width: 50%;
    ... 
}

Result

enter image description here

Upvotes: 2

Related Questions