| You are here: | About>Computing & Technology>Web Design / HTML> CSS> Beginning CSS> Center Images, Text, and Block-Elements - How to Center |
![]() | Web Design / HTML |
More Centering AdviceCentering Stuff with CSSHow to Center Images, Center Text, and Center BlocksThere are four ways to center:
Centering TextThe easiest thing to center is text. There is only one style property you need to know to center text: p.center { text-align: center; } Any paragraph written with the "center" class will be centered vertically on the page. <p class="center">This text is centered.</p> Text that is centered with the text-align property will be centered within whatever element contains it. Centering BlocksBlocks can be any type of block that has a defined width. The proper way to to this with CSS is to set both the left and right margins to "auto". div.center { <div class="center">This block is centered also.</div> As long as your block has a set width, it will center itself inside the containing element. But any text that is contained in the block will not be centered. Centering ImagesImages are a little trickier. While most browsers will display images centered using the same text-align property, it's not a good idea to rely on that technique, as it is not recommended by the W3C. Instead, you need to explicitly tell the browser that the image is a block element. Then you can center it like you would a block. img.center { <img src="blwebcam-sample.jpg" alt="Suni" class="center" /> Vertically Centering Images and BlocksCSS 2 doesn't have explicit support for centering items vertically on the page. There is a way to trick some browsers into displaying the contents vertically centered, but it doesn't work well in IE 7 and not at all in IE 6. This is how the W3C recommends you center text vertically until CSS 3 support is available.
.vcenter { <div class="vcenter"> In the meantime, you can use margins and padding to get some effect of centering in IE, but it won't be 100%. There are some ways to force IE to center and then use conditional comments so that only IE sees the styles. But they are ugly. I'd rather just wait for real vertical centering to be supported in the browser. Center Your Entire PageCentering the entire contents of your Web page depends upon whether you're using a fixed width layout or a liquid layout. Center a Document with a Fixed Width Layout More Centering Advice |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


