Center Oversized Image in Div
I have been trying to sort out how to center an oversized image within a div using css only.
We are using a fluid layout, so the width of the image containers varies as the page width does (height of div is fixed). The image sits within a div, withvalue an inset boxshadow so as to appear as if you are looking through the page at the image.
The image itself has been sized to fill the surrounding div at its widest possible value (the design has a max-width
value).
It is pretty easy to do if the image is smaller than the surrounding div:
margin-left: auto;
margin-right: auto;
display: block;
But when the image is larger than the div it simply starts at the left edge and is off center to the right (we are using overflow: hidden
).
We could assign a width=100%
, but browsers do a lousy job of resizing images and the web design centers around high quality imagery.
Any ideas on centering the image so that overflow:hidden
cuts off both edges evenly?
Try something like this. This should center any huge element in the middle vertically and horizontally with respect to its parent no matter both of their sizes.
.parent {
position: relative;
overflow: hidden;
}
.child {
position: absolute;
top: -9999px;
bottom: -9999px;
left: -9999px;
right: -9999px;
margin: auto;
}
这是一个古老的Q,但是没有Flexbox或绝对位置的现代解决方案就像这样。
margin-left: 50%;
transform: translateX(-50%);
.outer {
border: 1px solid green;
margin: 20px auto;
width: 20%;
padding: 10px 0;
/* overflow: hidden; */
}
.inner {
width: 150%;
background-color: gold;
/* Set left edge of inner element to 50% of the parent element */
margin-left: 50%;
/* Move to the left by 50% of own width */
transform: translateX(-50%);
}
<div class="outer">
<div class="inner">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos exercitationem error nemo amet cum quia eaque alias nihil, similique laboriosam enim expedita fugit neque earum et esse ad, dolores sapiente sit cumque vero odit! Ullam corrupti iure eum similique magnam voluptatum ipsam. Maxime ad cumque ut atque suscipit enim quidem. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Excepturi impedit esse modi, porro quibusdam voluptate dolores molestias, sit dolorum veritatis laudantium rem, labore et nobis ratione. Ipsum, aliquid totam repellendus non fugiat id magni voluptate, doloribus tenetur illo mollitia. Voluptatum.</div>
</div>
Put a large div inside the div, center that, and the center the image inside that div.
This centers it horisontally:
HTML:
<div class="imageContainer">
<div class="imageCenterer">
<img src="http://placekitten.com/200/200" />
</div>
</div>
CSS:
.imageContainer {
width: 100px;
height: 100px;
overflow: hidden;
position: relative;
}
.imageCenterer {
width: 1000px;
position: absolute;
left: 50%;
top: 0;
margin-left: -500px;
}
.imageCenterer img {
display: block;
margin: 0 auto;
}
Demo: http://jsfiddle.net/Guffa/L9BnL/
To center it vertically also, you can use the same for the inner div, but you would need the height of the image to place it absolutely inside it.
链接地址: http://www.djcxy.com/p/87500.html上一篇: 动态调整字体大小以适应容器大小
下一篇: 在Div中中心放大图像