Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center image inside overflow-hidden-parent

Tags:

css

I have an image inside a span tag, the span has a set width and height, and is set to overflow hidden. so it only reveals a small portion of the image. This works but the small portion of the image that is visible is the top left corner. I would like it to be the center of the image that is visible. I think I need to absolutely position the image, but the size of the image can vary though. Does anyone know how to do what I am trying to do?

Thanks!

Here is the HTML:

<div class="lightbox_images">
                <h6>Alternate Views</h6>
                <span>
                    <a href="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 1">
                        <img src="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" />
                    </a>
                </span>
                <span>
                    <a href="https://www.kranichs.com/product_images/Simon-G@346_M_346_M.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 2">
                        <img src="https://www.kranichs.com/product_images/Simon-G@346_M_346_M.jpg" />
                    </a>
                </span>
                <span>
                    <a href="http://www.kranichs.com/images/simong/sim_banner_01.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 3">
                        <img src="http://www.kranichs.com/images/simong/sim_banner_01.jpg" />
                    </a>
                </span>
                <span>
                    <a href="http://www.kranichs.com/images/psu/psu_banner.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 4">
                        <img src="http://www.kranichs.com/images/psu/psu_banner.jpg" />
                    </a>
                </span>
            </div>

Here is the CSS:

.lightbox_images{
    background-color:#F9F9F9;
    border:1px solid #F0F0F0;
}
.lightbox_images h6{
    font-family:Verdana, Arial, Helvetica, sans-serif;
    color:#333333;
    font-size:14px;
    font-weight:bold;
    font-style:italic;
    text-decoration:none;
    margin:0px;
}
.lightbox_images span{
    padding:5px;
    padding-bottom:15px;
    background-color:#DFDFDF;
    margin:5px;
    display:inline-block;
    border:1px solid #CCC;
}
.lightbox_images a{
    display:inline-block;
    width:60px;
    height:60px;
    overflow:hidden;
    position:relative;
}

.lightbox_images a img{
    position:absolute;
    left:-50%;
    top:-50%;
}

.lightbox_images span:hover{
    border:1px solid #BBB;
    background-color:#CFCFCF;
}
like image 241
JD Isaacks Avatar asked Mar 27 '09 14:03

JD Isaacks


3 Answers

As proposed in https://stackoverflow.com/a/14837947/2227298 by Billy Moat, there is a solution without knowing the image height and width.

Try it here: http://jsfiddle.net/LSKRy/

<div class="outer">     <div class="inner">     <img src="http://3.bp.blogspot.com/-zvTnqSbUAk8/Tm49IrDAVCI/AAAAAAAACv8/05Ood5LcjkE/s1600/Ferrari-458-Italia-Nighthawk-6.jpg" alt="" />     </div> </div>  .outer {     width: 300px;     overflow: hidden; }  .inner {     display: inline-block;     position: relative;     right: -50%; }  img {     position: relative;     left: -50%; } 
like image 168
KrisWebDev Avatar answered Sep 30 '22 06:09

KrisWebDev


Given this sort of HTML:

<span><img src="..." width="..." height="..." alt="..." /></span>

You could use CSS like this:

span {
  position: relative;
  display: block;
  width: 50px;  /* Change this */
  height: 50px; /* Change this */
  overflow: hidden;
  border: 1px solid #000;
}
span img {
  position: absolute;
  left: -10px; /* Change this */
  top: -10px;  /* Change this */
}

You can then center the image based on its exact dimensions.

Alternatively, if you're able to modify the HTML, you could instead use something like this:

<div>
  <a href="...">[name of picture]</a>
</div>

Then, match it with this CSS:

div {
  width: 50px;
  height: 50px;
  background: transparent url(...) center center no-repeat;
  border: 1px solid #000;
}
div a {
  display: block;
  height: 100%;
  text-indent: -9999em; /* Hides the link text */
}

In this case, the background will be automatically centered regardless of its dimensions, and it'll still be clickable.

like image 40
Ron DeVera Avatar answered Sep 30 '22 05:09

Ron DeVera


This example, the images are at the center of the element, regardless of its size

HTML:

<div class="box">
    <img src="example.jpg">
</div>

CSS:

div.box{
    width: 100px;
    height: 100px
    overflow: hidden;
    text-align: center;
}

div.box > img{
    left: 50%;
    margin-left: -100%;
    position: relative;
    width: auto !important;
    height: 100px !important;
}
like image 45
Vitor Deco Avatar answered Sep 30 '22 07:09

Vitor Deco