Andy-Howard.com

 

How to vertically and horizontally center text in an unordered list or div

Congratulations if you've managed to find this tutorial! I've been trying to increase awareness and publicity to this tutorial by going to other tutorials and leaving comments but I'm finding the authors take a competitive edge and do not approve my link to this page. If you find this helpful then please promote it by using the social links further down the page. This should hopefully increase my chances to be found in Google.

The following example shows you how to vertically and horizontally center text in both an unordered list and a div without resorting to JavaScript or css line heights. No matter how much text you have you won't have to apply any special classes to specific lists or divs (the code is the same for each). This works on all major browsers including IE9, IE8, IE7, IE6, Firefox, Chrome, Opera and Safari. There are 2 special stylesheets (1 for IE7 and another for IE6) to help them along due to their css limitations which modern browsers don't have.

If you simply wish for it to work in a div, remove the list items around it (the code will still work).

UPDATE, the code has been altered slightly as of 29/9/2011 to make it easier to work with.

Alternatively, you can download the zip here.

 

Like It? Share It!

 

Need help with this article?

Have you got a suggestion, compliment or need additional help with this article? Simply contact me at ajfhoward[@t]hotmail.com and I'll try to help as much as I can.

 

Your comments

Please leave any comments below. Please note a username and password are NOT required, your messages are managed by your ip address only. A message that was written from your ip address will be highlighted by the big thick border around it. To edit a previous posting, simply click into it and type away! This is a good example of editable content used by HTML5:

 
  • 26
    Jan
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et mi vulputate odio pulvinar tempor. Duis iaculis consequat accumsan. Vestibulum dolor dolor, porttitor aliquam vestibulum vitae, pellentesque venenatis augue. Curabitur dapibus ligula et leo molestie egestas. Vivamus ultrices tortor vitae diam dictum nec fermentum elit accumsan. Nunc consequat dictum massa vel auctor. Phasellus fermentum tincidunt tempor. Nullam mattis blandit nunc et imperdiet. Phasellus id nisi elementum risus vehicula
    By Brian Smith
  • 10
    Mar
    arcu quam, tristique placerat dui. Sed vehicula ultricies magna, quis ultrices urna vestibulum vel. Praesent interdum nisi quis sem iaculis faucibus. Morbi ac varius neque. Mauris libero felis, venenatis vel porttitor at, condimentum et ligula. Proin vitae massa nibh. Sed nec faucibus nibh. Nam vitae nisl mi. Ut posuere pulvinar quam, vitae laoreet purus dapibus eget. Quisque
    By Brian Smith
  • This One's Ures!
    3
    Apr
    Etiam mattis, nibh cursus sollicitudin cursus, leo mauris laoreet risus, sit amet rutrum tortor turpis vel tellus. Mauris semper, libero in laoreet dignissim, lectus magna convallis purus, quis interdum dolor turpis a purus. Duis non turpis eget enim convallis cursus. Aliquam iaculis egestas tellus vitae dapibus. Maecenas massa nunc, imperdiet ac congue id, sollicitudin vel turpis. Ut aliquet mattis nibh, eu sollicitudin leo imperdiet ac.
    By Brian Smith
  • 30
    Dec
    Pellentesque consequat urna nec nibh scelerisque in placerat mi pretium. Etiam sit amet libero urna. Morbi aliquam urna eu lacus aliquet sodales eget sit amet enim. Fusce quis tellus augue. In ac sapien et diam auctor sodales. Curabitur sem sapien, mattis in consequat in, adipiscing ut tellus. Donec scelerisque mollis condimentum. Ut a justo tellus. Duis at lectus purus. Nullam auctor est aliquam arcu congue accumsan. Aliquam hendrerit risus ut lorem pulvinar malesuada. Nunc aliquam urna non tortor interdum ut tincidunt eros scelerisque.
    By Brian Smith