0

I want my block to be set by line-height (just like i do with text). As i know i should use display: inline-block in this case, but this doesn't work for me. Why?

HTML:

<div class="block">
    <div></div>
</div>
<div class="block">
    test
</div>

CSS:

.block {
    line-height: 50px;
    height: 50px;
    width: 50px;
    border: 1px solid black;
}
.block div {
    height: 40px;
    width: 28px;
    background-color: #f0f;
    display: inline-block;
}

Live demo: jsFiddle

2
  • 2
    That is doing exactly what you told it to do, what is your expected outcome? Commented Aug 16, 2012 at 11:07
  • I want pink ".block div" to be centered in the middle of the main ".block". I don't want to use hacks for block-elements because i have to change the height of the text and the inline div in the same time. Commented Aug 16, 2012 at 11:14

2 Answers 2

1

hi now add your div aertical-align middle in your css

.block div {
     vertical-align: middle;
}

Demo

-------------------------------------------- now if you want to center this box than add text-align center as like this

.block {
    text-align: center;
}

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

vertical-align: middle; was the answer. Thank you.
1

i guess you are trying to center the purple block vertical?

in that case your mixing thing up:

a <div> is a block-level element, where text is not. so if you say line-height, you specify text-alignment of the content for that element, not positioning of a block element, to solve the centering of that purple block, use padding or margin:

.block div {
    height: 40px;/* 50 - 40 = 10pixel/2 = 5px space */
    width: 28px;
    background-color: #f0f;
    margin-top: 5px;
}

Demo over here jsFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.