-2

I am trying to align a text vertically in a DIV. I tried it with CSS line-height property, but no luck.

This is my HTML -

  <div class="vertical">
     <h4>Content one</h4>
  </div>

  <br><br><br><br>

  <div class="vertical">
     <h4>This is Content Tow</h4>
  </div

My CSS -

.vertical {
    background: #8C3A0A;
    border-radius: 6px;
    color: #FFFFFF;
    float: left;
    height: 48px;
    line-height: 50px;
    margin-right: 10px;
    padding: 5px;
    text-align: center;
    width: 25%;
}

Any body can tell me how I set these text to vertically center?

Thank You.

TNK
  • 4,263
  • 15
  • 58
  • 81

3 Answers3

1

You can try using vertical-align.

The key is that it is applied only to elements with display set to inline, inline-block or table-cell. Moreover, the display property of the parent of these elements also has to be set to one of these 3 values.

And it does vertically align not the element itself, but it's contents relative to the parent.

**Example**, try playing with the vertical-align of the span element.

And also try changing the display property of the div and span elements.

I suggest reading w3c specifications or the Mozilla Developer Network article for more information

knitevision
  • 3,023
  • 6
  • 29
  • 44
0

Change your css to this:

.vertical {
    background: #8C3A0A;
    border-radius: 6px;
    color: #FFFFFF;
    float: left;
    margin-right: 10px;
    text-align: center;
    width: 25%;
}

h2 default style.

h2{ font-size: 1.5em; margin: .75em 0 }

Default style sheet

There is no need to use line height :)

fiddle

Alex Char
  • 32,879
  • 9
  • 49
  • 70
0

The H4 element is not align as you want. Add a margin: auto and it will solve your problem.

h4{
    margin: auto;
}

Fiddle

Itay Gal
  • 10,706
  • 6
  • 36
  • 75