Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center image inside div with overflow hidden without knowing the width

Tags:

css

I have an image which is e.g. the width 450px, and a container which is only 300. Is it possible to center the image inside the container with CSS, when the width of the image isn't constant (Some images might be 450 wide, other 600 etc.). Or do I need to center it with JavaScript?

like image 811
Dofs Avatar asked Feb 12 '13 16:02

Dofs


People also ask

How do I center a div without the width?

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 an inside section of 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 hide the overflow of a picture?

To activate the overflow property enclose the image, within a div of a particular width and height, and set overflow:hidden . This will ensure that the base container will retain its structure, and any image overflow will be hidden behind the container.

How do I fix the overflow image in CSS?

To be able to rotate such img and make it fit vertically, the transform needs to know the aspect ratio of the wrapper before hand, to be able compensate the overflow, and in this sample it is known (150px / 250px = 0.6), so by adding scale to the transform we can avoid an overflow.


2 Answers

This any good? 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 84
Billy Moat Avatar answered Sep 24 '22 13:09

Billy Moat


Proposition 1 :

.crop {
    float:left;
    margin:.5em 10px .5em 0;
    overflow:hidden; /* this is important */
    border:1px solid #ccc;
}
/* input values to crop the image: top, right, bottom, left */
.crop img {
    margin:-20px -15px -40px -55px;
}

Proposition 2 :

.crop{
    float:left;
    margin:.5em 10px .5em 0;
    overflow:hidden; /* this is important */
    position:relative; /* this is important too */
    border:1px solid #ccc;
    width:150px;
    height:90px;
}
.crop img{
    position:absolute;
    top:-20px;
    left:-55px;
}

proposition 3:

.crop{
    float:left;
    position:relative;
    width:150px;
    height:90px;
    border:1px solid #ccc;
    margin:.5em 10px .5em 0;
}
.crop p{
    margin:0;
    position:absolute;
    top:-20px;
    left:-55px;
    clip:rect(20px 205px 110px 55px);
}

Proposition 4 (hold-school efficiency):

.container {
    width:400px;
    height:400px;
    margin:auto;
    overflow:hidden;
    background:transparent url(your-image-file­.img) no-repeat scroll 50% 50%;
}

Of course you will need to ajust the .css to suit your own needs

Carry on.

like image 22
Milche Patern Avatar answered Sep 25 '22 13:09

Milche Patern