如何使用 CSS 使图像适合 200 像素的方框?

发布于 2024-08-02 06:54:52 字数 779 浏览 2 评论 0原文

我有一堆图像,它们都适合 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

汹涌人海 2024-08-09 06:54:52

我在我的电脑上设置了这个并且运行良好。查看示例页面后,问题是因为您将图像设置为 display:block。要么从您的常规 img 规则中删除该规则(无论如何,全局设置很奇怪),或者将您上面发布的图像规则更改为:

div.images div.small img
{
    vertical-align: middle;
    max-width: 200px;
    max-height: 200px;
    display: -moz-inline-box; /* Firefox 2 */
    display: inline-block;
}

默认情况下, 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 general img rule (weird thing to set globally, anyway), or change the image rule you posted above to this:

div.images div.small img
{
    vertical-align: middle;
    max-width: 200px;
    max-height: 200px;
    display: -moz-inline-box; /* Firefox 2 */
    display: inline-block;
}

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.

时光沙漏 2024-08-09 06:54:52

我前段时间需要做同样的事情,我在 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.

难理解 2024-08-09 06:54:52

我曾经遇到过这个垂直居中问题,为了让它在所有浏览器中正常工作,我求助于 javascript / jQuery:

$(document).ready(function() {
    $('img').each(function() {
        image_height = $(this).height();
        margin_top = (200 - image_height) / 2;
        $(this).css('margin-top', margin_top + 'px');
    });
});

请注意,如果图像尺寸是,您将需要 $(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:

$(document).ready(function() {
    $('img').each(function() {
        image_height = $(this).height();
        margin_top = (200 - image_height) / 2;
        $(this).css('margin-top', margin_top + 'px');
    });
});

Please note that you will need $(window).load instead of $(document).ready if the image dimensions are not given in the html.

暖树树初阳… 2024-08-09 06:54:52

您是否尝试过使用:

display:block;
margin-left:auto;
margin-right:auto;

应该将块级元素居中

have you tried using:

display:block;
margin-left:auto;
margin-right:auto;

that should center block level elements

我还不会笑 2024-08-09 06:54:52

无论您想要什么尺寸,这里都有一个解决方案。它将缩小但不会放大,垂直和水平居中,并且仅使用 CSS。我花了一段时间才弄清楚。

放入

中,然后根据需要放置 div(即,为其指定所需的大小,确保设置 < code>position 属性),然后应用此:

.mydiv > .myimg {
    vertical-align: bottom;
    max-height:     100%;
    max-width:      100%;
    position:       absolute;
    margin:         auto;
    top:            0;
    right:          0;
    bottom:         0;
    left:           0;
}

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 the position attribute), then apply this:

.mydiv > .myimg {
    vertical-align: bottom;
    max-height:     100%;
    max-width:      100%;
    position:       absolute;
    margin:         auto;
    top:            0;
    right:          0;
    bottom:         0;
    left:           0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文