0

I am having trouble vertical aligning text in a div. I have tried suggestions that i have read on other posts. I'm sure its obvious what I am doing wrong but its not working.

http://jsfiddle.net/o6gqvtoo/

<div class="banner">
  <div class="span10"> <!-- bootstrap -->
    <div class="banner-text">
        <div class="pull-left">Here is the first line of text i want to center</div>
        <div class="pull-left">Here is the second line of text i want to center</div>
        <div class="clear"></div>
    </div>
</div>

.banner{
margin-top:-2;
height: 70px;
background-color: #cf0000;
color: white;
height: 70px;
position: relative;
}

.banner-text{
display: block;
vertical-align: middle;
}

.pull-left {
display: block;
}

3 Answers3

0

vertical-align is sort of an odd one.

Generally you should use it on the elements themselves, not their containers and it's really only for inline and inline-block elements.

But even then it probably won't vertically center like (I'm guessing) you are wanting. So a little trick is to set your line-height to the same as your height and voila:

http://jsfiddle.net/06c7eosv/

Note: With display: inline-block and width: 50% you can't have any white space between the elements in your html

Good:

<div>...</div><div>...</div>

Bad:

<div>...</div>
<div>...</div>
Jonathan Dumaine
  • 5,575
  • 3
  • 38
  • 50
0

Based on the supplied image and limited information availableI believe that the best option available is to absolutely position the banner-text wrapper div

.banner {
  margin-top: -2;
  height: 70px;
  background-color: #cf0000;
  color: white;
  height: 70px;
  position: relative;
}
.banner-text {
  display: block;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
.pull-left {
  display: block;
}
<div class="banner">
  <div class="span10">
    <!-- bootstrap -->
    <div class="banner-text">
      <div class="pull-left">Here is the first line of text i want to center</div>
      <div class="pull-left">Here is the second line of text i want to center</div>
    </div>
  </div>
</div>

That said, other options may be available if the two internal divs can be combined.

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

Usually, vertical-align sets the alignment of inline level elements relatively to the line box.

However, in when applied to table cells, it has a different meaning: it sets the alignment of the content inside the cell.

Therefore, you can use CSS tables:

.banner {
    display: table;
    width: 100%;
}
.banner > .span10 {
    display: table-row;
}
.banner-text {
    display: table-cell;
    vertical-align: middle;
}

.banner{
  margin-top:-2;
  height: 70px;
  background-color: #cf0000;
  color: white;
  height: 70px;
  position: relative;
  display: table;
  width: 100%;
}
.banner > .span10 {
  display: table-row;
}
.banner-text{
  display: table-cell;
  vertical-align: middle;
}
.pull-left {
  display: block;
  /* float:left; */
}
<div class="banner">
  <div class="span10"> <!-- bootstrap -->
    <div class="banner-text">
      <div class="pull-left">Here is the first line of text i want to center</div>
      <div class="pull-left">Here is the second line of text i want to center</div>
      <div class="clear"></div>
    </div>
  </div>
</div>
Oriol
  • 274,082
  • 63
  • 437
  • 513