如何使用 CSS 使图像适合 200 像素的方框?
我有一堆图像,它们都适合 400px × 400px 的盒子(也就是说,其中一个尺寸为 400px,另一个尺寸较小)。我希望能够使用 CSS,但如果需要,可以使用 jquery/javascript,将该图像适合 200px x 200px 的框,以便图像的两个边缘接触框,并且图像的其他两个边缘之间有间隙盒子。 必须保持纵横比。
我的 HTML 如下:
<div class="small">
<img src="/images/photos/View.jpg" alt="View" />
</div>
我的 CSS 为:
div.images div.small
{
width:200px;
height:200px;
line-height:200px;
text-align:center;
}
div.images div.small img
{
vertical-align:middle;
max-width:200px;
max-height:200px;
}
您可以在此处查看示例。
不幸的是,我的风景图像紧贴盒子的顶部,而我希望它们居中。另外,我不确定依赖 max-width
/max-height
是否明智。
如何将我的图像置于这些框中的中心?
I have a bunch of images which all fit into a 400px × 400px box (that is, one of their dimensions is 400px and the other is smaller). I would like to be able to, using CSS, but jquery/javascript if necessary, fit that image to a 200px by 200px box, so that two edges of the image touch the box, and there is a gap between the other two edges of the box.
Aspect ratio must be maintained.
My HTML is as follows:
<div class="small">
<img src="/images/photos/View.jpg" alt="View" />
</div>
And my CSS is:
div.images div.small
{
width:200px;
height:200px;
line-height:200px;
text-align:center;
}
div.images div.small img
{
vertical-align:middle;
max-width:200px;
max-height:200px;
}
You can see a sample here.
Unfortunately, my landscape images hug the top of the box, whereas I would like them to be centered. Also, I'm not sure of the wisenees of relying upon max-width
/max-height
.
How can I center my images within these boxes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在我的电脑上设置了这个并且运行良好。查看示例页面后,问题是因为您将图像设置为
display:block
。要么从您的常规img
规则中删除该规则(无论如何,全局设置很奇怪),或者将您上面发布的图像规则更改为:默认情况下,
img
元素和其他“替换”元素(Flash 等)是“内联块” - 这意味着它们像文本一样内联流动,但具有宽度和高度。I set this up on my computer and it worked fine. After looking at your example page, the problem is because you've set the image to
display:block
. Either remove that rule from your generalimg
rule (weird thing to set globally, anyway), or change the image rule you posted above to this:By default, the
img
element and other "replaced" elements (Flash, etc) are "inline-block" - this means they flow inline like text, but have a width and height.我前段时间需要做同样的事情,我在 this 中找到了一个很好的实现链接。
“将图像在 div 内垂直和水平居中,而不知道图像的大小”。
它按预期对我有用。
I needed to do the same some time ago, and I found a good implementation in this link.
"Center an image inside a div, vertical and horizontal, without knowing the image's size".
It works for me as intended.
我曾经遇到过这个垂直居中问题,为了让它在所有浏览器中正常工作,我求助于 javascript / jQuery:
请注意,如果图像尺寸是,您将需要 $(window).load 而不是 $(document).ready html 中没有给出。
I ran into this vertical centering problem once and to get it working correctly in all browsers, I resorted to javascript / jQuery:
Please note that you will need $(window).load instead of $(document).ready if the image dimensions are not given in the html.
您是否尝试过使用:
应该将块级元素居中
have you tried using:
that should center block level elements
无论您想要什么尺寸,这里都有一个解决方案。它将缩小但不会放大,垂直和水平居中,并且仅使用 CSS。我花了一段时间才弄清楚。
将
放入
中,然后根据需要放置 div(即,为其指定所需的大小,确保设置 < code>position 属性),然后应用此:
Here is a solution that will work no matter what size you want. It will downscale but not upscale, center both vertically and horizontally, and only uses CSS. It took me a while to figure it out.
Put your
<img>
inside a<div>
, then position the div as you want (i.e., give it the size you want, make sure to set theposition
attribute), then apply this: