This is probably a stupid question but since the usual ways of center aligning an image are not working I thought I would ask. How can I center align (horizontally) an image inside its container div?
Here's the HTML and CSS. I've also included the CSS for the other elements of the thumbnail. It runs in descending order so the highest element is the container of everything and the lowest is inside everything.
#thumbnailwrapper {
color: #2A2A2A;
margin-right: 5px;
border-radius: 0.2em;
margin-bottom: 5px;
background-color: #E9F7FE;
padding: 5px;
border: thin solid #DADADA;
font-size: 15px
}
#artiststhumbnail {
width: 120px;
height: 108px;
overflow: hidden;
border: thin solid #DADADA;
background-color: white;
}
#artiststhumbnail:hover {
left: 50px
}
<!--link here-->
<a href="NotByDesign">
<div id="thumbnailwrapper">
<a href="NotByDesign">
<!--name here-->
<b>Not By Design</b>
<br>
<div id="artiststhumbnail">
<a href="NotByDesign">
<!--image here-->
<img src="../files/noprofile.jpg" height="100%" alt="Not By Design" border="1" />
</a>
</div>
<div id="genre">Punk</div>
</div>
Okay, I have added the markup without the PHP in so should be easier to see. Neither solution seems to work in practice. The text at top and bottom cannot be centered and the image should be centered within its container div. The container has overflow hidden so I want to see the center of the image as that's normally where the focus is.
Use positioning. The following worked for me... (Horizontally and Vertically Centered)
With zoom to the center of the image (image fills the div):
Without zoom to the center of the image (image does not fill the div):
I just found this solution below on the W3 CSS page and it answered my problem.
Source: http://www.w3.org/Style/Examples/007/center.en.html
Put the picture inside a
newDiv
. Make the width of the containingdiv
the same as the image. Applymargin: 0 auto;
to thenewDiv
. That should center thediv
within the container.The best thing I have found (that seems to work in all browsers) for centering an image, or any element, horizontally is to create a CSS class and include the following parameters:
CSS
You can then apply the CSS class you created to your tag as follows:
HTML
You can also inline the CSS in your element(s) by doing the following:
...but I wouldn't recommend writing CSS inline because then you have to make multiple changes in all your tags using your centering CSS code if you ever want to change the style.
I am going to go out on a limb and say that the following is what you are after.
Note, the following I believe was accidentally omitted in the question (see comment):
So what you need is:
http://jsfiddle.net/userdude/XStjX/3/