Isabella
Joined: 15 Jun 2006 Posts: 103
|
Posted: Fri Aug 11, 2006 11:41 pm Post subject: Aligning Images With CSS |
|
|
The most obvious CSS solution is to use the text-align property to center the image. Unfortunately, that has the same effect as adding align="center" to the image tag: browsers ignore it entirely!
Instead, you'll have to apply the text-align property to the container element (the paragraph, DIV, or other block-level element that contains the image).
Create a style class and add it to the HEAD section of your page. Even better, add it to your external style sheet and use it on every page!
| Code: | <style type="text/css">
.centeredImage
{
text-align:center;
margin-top:0px;
margin-bottom:0px;
padding:0px;
}
</style> |
Then, apply the class to the container element:
| Code: | | <p class="centeredImage"><img src="imgName.gif" alt="image description" height="100" width="100"></p> |
That's slightly more trouble than just applying align="center" to the paragraph tag, but it has the added benefit of giving you more control over the spacing around the image. Note that in our example, we set the margin and padding values to zero pixels to avoid extra spacing that may push important content farther down the page.
|
|