I want to align some text in the middle of my element using CSS. This is my markup:
<div id="testimonial">
<span class="quote">Some random text that spans two lines</span>
</div>
And the relevant CSS:
#testimonial {
background: url('images/testimonial.png') no-repeat;
width: 898px;
height: 138px;
margin: 0 auto;
margin-top: 10px;
text-align: center;
padding: 0px 30px 0px 30px;
}
.quote {
font-size: 32px;
font-family: "Times New Roman", Verdanna, Arial, sans-serif;
vertical-align: middle;
font-style: italic;
color: #676767;
text-shadow: 1px 1px #e7e7e7;
}
Usually to get .quote
in the vertical middle of #testimonial
, I'd do:
.quote { line-height: 138px; }
But this breaks the layout because the text in .quote
spans more than one line.
As you can see I've tried doing vertical-align: middle;
and that doesn't work either.
Any help is appreciated. Cheers.
I recently found out that vertical centering of something which has undefined dimensions goes very well with
vertical-align: middle;
in combination withline-height: 0;
.Check out this demonstration fiddle.
HTML:
CSS:
as nobody's answered it with the table cell solution yet..
here you go - with an an IE6/7 solution too (though it involves a yukky HTML hack) as @thirtydot says in comments, table display properties are not supported by IE7 and below -
if you don't want/like the extra HTML element, you could just give IE7 and below some top padding on the
.quote
- so that while it wouldn't be vertically centered accurately it may be an acceptable fall backCSS:
IE CSS:
HTML:
This website offers a plethora of options for vertical centering with css.
If you can set a height on
.quote
, I think Method 2 would work in this situation:Another option is to use
display:table-cell; vertical-align:middle;
method in CSS, but this option does not work in IE, so you'll have to also set an IE-specific version.You can do it simpler with single span http://jsfiddle.net/7ebLd/
This site:
http://www.emblematiq.com/blog/vertical_align_with_css/
With the result:
http://www.emblematiq.com/blog/vertical_align_with_css/assets/03.html
Uses the following markup/css to center vertically multiple lines using the
display: table-cell
,vertically-align: middle
method: