CSS: How to Center Your Images

css-tips-tutorials

If you have a paragraph(<p>) or a <div> with only images, it’s very easy to center them, just set the ‘text-align’ to ‘center’:

p{ text-align: center }

What if there is other content in the paragraph, like text, and you don’t want it to be centered. Well, one way to do this, is to add a container, for example, a <div>, and the ‘text-align’ of the container to ‘center’. But this doesn’t look like a smart way, as the container is quite a waste. Can I do this within the <img>? Of course, you can make this happen with CSS, Check out the following code:

<img style=”display: block; clear: both; margin-left: auto; margin-right: auto;” src=”test.jpg” />

Just set the ‘display’ as ‘block’ and both the left and right margin as ‘auto’, that’s it, or you can add this to you CSS file:

p img{ display: block; clear: both; margin-left: auto; margin-right: auto; }

Let me know if you have a better idea.

This article is also posted on Gearor.com.


Feel free to visit our Forum, subscribe our RRS Feed for news updates, and follow us on Twitter.

Related Posts

This entry was posted in Plugins, Tutorials and Hacks, Web Design and tagged , , , , , . Bookmark the permalink.

One Response to CSS: How to Center Your Images

  1. Pingback: CSS: How To Center Your Images | Gearor Tech Magazine - Tech, Gadgets, Websites, Business news

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>