I am trying to find the most effective way to align text with a div. I have tried a few things and none seem to work.
.testimonialText {
position: absolute;
left: 15px;
top: 15px;
width: 150px;
height: 309px;
vertical-align: middle;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
padding: 1em 0 1em 0;
}
<div class="testimonialText">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
You need to add the
line-height
attribute and that attribute must match the height of thediv
. In your case:In fact, you could probably remove the
height
attribute altogether.This only works for one line of text though, so be careful.
Update - Here's a great resource
http://howtocenterincss.com/
Update - Using Flexbox
Inline with keeping this post up to date with the latest tech, here's a much easier way to center something using flexbox. Flexbox isn't supported in
IE9 and lower
.Here's some great resources:
jsfiddle with browser prefixes
HTML
CSS
Update - Another solution
This is from zerosixthree and lets you center anything with 6 lines of css
This method isn't supported in
IE8 and lower
jsfiddle
HTML
CSS
Previous answer
Simple and cross browser approach, useful as links in the marked answer are slightly outdated.
Andy Howard - How to vertically and horizontally center text in an unordered list or div
Edit: As I didn't care much for IE7/6 for the last project I worked on, I used a slightly stripped down version (i.e. removed the stuff that made it work in IE7 and 6). Might be useful for somebody else...
jsfiddle
HTML
And the CSS:
UPD: This days (we don't need IE6-7-8 no more) i would just use css display: table for this issue
This is my favorite solution for this issue (simple and very well browser supported):
Check this simple solution:
HTML
CSS
JSFiddle
Hmm, there're obviously many ways to solve this.
But I have a
<div>
that's positioned absolutely,height:100%
(actually,top:0;bottom:0
and fixed width) anddisplay:table-cell
just didn't work to center text vertically. My solution did require an inner span element, but I see many of the other solutions do also, so I might as well add it:My container is a
.label
and I want the number vertically centered in it. I did it by positioning absolutely attop:50%
and settingline-height:0
And the CSS is as follows:
See it in action: http://jsfiddle.net/jcward/7gMLx/
There are several Tricks to display content/image in center of Div. Some of answers are really nice and I am fully agree with these too.
Absolute Horizontal And Vertical Centering In CSS
http://www.css-jquery-design.com/2013/12/css-techniques-absolute-horizontal-and-vertical-centering-in-css/
There are more than 10 techniques with Examples. Now it's up to you which you prefer.
No doubt,
display:table; display:table-Cell
is a better trick.Some good Tricks are following:
Trick 1 - By using
display:table; display:table-cell
HTML
CSS Code
Trick 2 - By using
display:inline-block
HTML
CSS code
Trick 3 - By using
position:relative;position:absolute