Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center image horizontally within a div

Tags:

html

css

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.

like image 276
Jacob Windsor Avatar asked Jun 12 '12 00:06

Jacob Windsor


People also ask

How do I center an object horizontally in a div?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I center inside a div?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I align an image to a div?

Aligning an image means to position the image at center, left and right. We can use the float property and text-align property for the alignment of images. If the image is in the div element, then we can use the text-align property for aligning the image in the div.


1 Answers

#artiststhumbnail a img {     display:block;     margin:auto; } 

Here's my solution in: http://jsfiddle.net/marvo/3k3CC/2/

like image 161
Marvo Avatar answered Sep 28 '22 07:09

Marvo