Jump to content

How to Center Images in CSS


vajra_hendry

Recommended Posts

This may sounds overly simple.  However I have looked his up in my CSS book, and looked at severla websites.

 

My problem is that I want to center an image on my page (well center the image within its containing block).

 

I would normally just use Absolute or Relative positioning, however the Banner that im trying to align is  an images thats passed from PHP so its different everytime!!  COuld be like 125x125 pixles or it might be 468 x 60 pixels.  So whatever the image is , I need CSS to just align is Center!!

 

Any ideas please?!  ::)

 

Thanks

Link to comment
Share on other sites

I would do the following:

 

/*css*/
.image_center {margin:0 auto;}

 

<!-- xhtml -->
<div class="image_center">
   <img src="" alt="" />
</div>

 

The margin value of "auto" will automatically create equal left/right margins around an element with class="image_center"...which centers it within the container element.

 

Make sure you're using a valid doctype and validate your CSS and markup.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.