• Register
Hi, Welcome to PHPHunts.com, a PHP community website where you can ASK Question or search for answers to your PHP, JQuery and CSS queries. Click here to Register

368 questions

279 answers

22 comments

245 users

How to center images

0 votes

How to center a image in a div.

<div class="div1">
     <img src="image1.jpg" alt=""  />
</div>

<div class="div2">
     <img src="image2.jpg" alt=""  />
</div>
asked Sep 1, 2012 in CSS by Tamal Dey New (650 points)
   

2 Answers

0 votes

Yo can center the image by just centering the whole div tag.,Here is the example,

<div class="div1" align="center">
     <img src="image1.jpg" alt=""  />
</div>

OR

Give width to div tag and then center the image,,here is the example,

<div class="div2" width="100%">
     <img src="image2.jpg" alt="" align="center" width="50%"  />
</div>
answered Sep 5, 2012 by Vishal Modi New (480 points)
Hi, thanks vishal for the answer :)
Your welcome... :)
0 votes

I have another solution using css,

div{width:100%;}
div img{ display:block; margin: 0 auto; }

This will bring the image in the centre of the relative div

answered Sep 5, 2012 by Tamal Dey New (650 points)
Yeah...This is also one of solutions....Nice one...
But the problem is that it will apply 100% width to all div tags and so for the div img....
SO use it carefully...
...