user1121487
user1121487

Reputation: 2680

CSS place image in middle of div: with overflow hidden

I wanna place a profile picture that has a width of 200px and an unknown height, inside a div container. The div is 150px x 150px. The img should be centered horizontal with the bleed hidden on each side. I cannot get this to work, the unknown height is messing it up, since the way the img width will appear is depending on the height. - A landscape img will get a higher width to fill out the height difference and keep the img in proportion. If it's a portrait image, the width should be 150px as the div...

Watch image: http://s18.postimage.org/52hfcc5h3/Sk_rmavbild_2012_06_26_kl_19_12_49.png

#profilePicture {
    display: block;
    float: left;
    width: 150px;
    height: 150px;
    overflow: hidden;
    position: relative;
    margin: 0 auto;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}
#profilePicture img {
    min-width: 150px;
    min-height: 150px;
    left: 50%;
    margin-left: -100px;
    top: 50%;
    margin-top: -100px;
    position:absolute;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

Upvotes: 1

Views: 3270

Answers (2)

Paulo R.
Paulo R.

Reputation: 15619

Have you considered using background-images? It would make it much easier.

.img {
    overflow: hidden;
    background-image: url('/xyz.jpg');
    background-size: cover;
    background-position: center;
}

Here's The fiddle -> http://jsfiddle.net/haNj3/1/

Upvotes: 3

Rorok_89
Rorok_89

Reputation: 375

I think what you want to do is this.

.main{
width:150px;
margin: 0 auto;
overflow:visible;
position: relative;
height: 200px;
outline: 1px solid blue;
}

img.absolute{
width: 200px;
left: 50%;
margin-left: -100px;
margin-top: 0;
position:relative;
outline: 1px solid red; 
}

I think there is no need to use absolute positioning in this case.

Upvotes: 0

Related Questions