2

I've centered my text in my div but I've only managed to center it horizontally by using text-align: center However I'm having trouble centering the text vertically. I've tried using vertical-align: middle; but it didn't work

Here is a codepen: http://codepen.io/anon/pen/Ioiaq

the code with the text is right at the bottom of the html panel the text is "INFO"

and here's the code so far:

<div id="inf" style="
                     text-align:center;  
                     background-color: white; 
                     position: absolute; top:50%; left: 45%; width: 10%; height: 6%; 
                     z-index: 10;  
                     display: inline-block;">

  <a href="#"><span>INFO</span></a>
  </div>

any ideas?

I don't mind what code is used to fix the alignment it doesn't have to be css.

Chris Lad
  • 349
  • 4
  • 8
  • 24

3 Answers3

1

To center a single line vertically, you can use CSS line-height whith the same value as the height value you are using.

tomasofen
  • 1,330
  • 2
  • 12
  • 16
1

line-height with a vertical-align: middle; is probably the best way to go if you aren't using JS.

FIDDLE

HTML:

<div id="inf">
    <a href="#"><span>INFO</span></a>
</div>

CSS:

#inf { 
    text-align:center;  
    background-color: white; 
    z-index: 10;  
    display: inline-block;
    width: 100%;
    line-height: 400px;
    vertical-align: middle;
}
stevenspiel
  • 5,775
  • 13
  • 60
  • 89
0

You could use the CSS property vertical-align.

vertical-align: middle;
Chad
  • 164
  • 10