在怪异模式下,是否可以将 img 的大小调整为其包含的表格单元格?

发布于 2024-10-21 11:19:44 字数 477 浏览 3 评论 0原文

这似乎是过去的一次爆炸,但由于项目限制,我陷入了怪癖模式和表格......

我正在处理的表格在每个单元格中都有一个图像,其中所有单元格应该具有相同的大小。表的宽度和高度设置为父容器的百分比。

问题是图像不会缩小,无论我做什么,它们似乎都保持原始大小。然后,表格不符合其设置的大小,它已调整大小以容纳所有图像。在标准模式下,我相信图像上的“宽度:100%”更接近我想要实现的目标。

我正在考虑一个 javascript 解决方案,它循环遍历每个图像,计算它们的大小应该是什么并手动调整大小。但这可能会在开始时导致一些加载时间,这并不理想。

编辑:

我在JSBin编写了一个基本示例。我想要实现的是能够设置表格的大小并调整图像的大小,无论是增大还是缩小到单元格。

第四个 jsbin 修订版使用虚拟图像。

This may seem like a blast from the past but due to project constraints I am stuck with quirks mode and tables...

The tables that i'm dealing with have a single image in each cell where all the cells should be the same size. The tables width and height are set as percentages of a parent container.

The problem is the images don't resize down, they stay at their original size seemingly no matter what I do. Then the table doesn't adhere to its set size, it has resized to hold all of the images. In standards mode I believe 'width: 100%' on the image gets closer to what I want to achieve.

I'm considering a javascript solution which loops over each image calculating what their size should be and resizing manually. But this is probably going to cause a bit of a loading time at the start which isn't ideal.

Edit:

I have written a basic example at JSBin. What I want to achieve is to be able to set the size of the table and have the images resize, whether growing or shrinking to their cell.

The 4th jsbin revision uses the dummy images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

瑕疵 2024-10-28 11:19:44

认为我已经解决了这个问题。

我已经在 Chrome、Firefox、Internet Explorer、Safari、Opera 中测试了这个演示;它们都一致地呈现。

  • 我必须在每个单元格中添加一个包装 div 。我知道这不太好,但必须这样做才能使其在 Chrome 中工作。
  • 我添加了 table-layout:fixed 以使其在 Internet Explorer 中正常工作。

现场演示

CSS:

#mycontainer {
    width: 300px;
    height: 300px;
    border: 1px solid red;
}
#mytable {
    height: 50%;
    width: 50%;
    table-layout: fixed;
}
#mytable div {
    width: 100%;
    height: 100%;
}
#mytable img {
    width: 100%;
    height: 100%;
}

HTML:

<div id="mycontainer">
    <table id="mytable" cellpadding="0" cellspacing="0">
        <tr>
            <td><div><img src="http://dummyimage.com/28x28/000/fff.png&text=Dummy" /></div></td>
            ...
        </tr>
        ...
    </table>
</div>

I think I've solved this.

I've tested this demo in Chrome, Firefox, Internet Explorer, Safari, Opera; they all render consistently.

  • I had to add a wrapper div in each cell. I know this isn't awesome, but it had to be done to make it work in Chrome.
  • I added table-layout: fixed to make it work in Internet Explorer.

Live Demo

CSS:

#mycontainer {
    width: 300px;
    height: 300px;
    border: 1px solid red;
}
#mytable {
    height: 50%;
    width: 50%;
    table-layout: fixed;
}
#mytable div {
    width: 100%;
    height: 100%;
}
#mytable img {
    width: 100%;
    height: 100%;
}

HTML:

<div id="mycontainer">
    <table id="mytable" cellpadding="0" cellspacing="0">
        <tr>
            <td><div><img src="http://dummyimage.com/28x28/000/fff.png&text=Dummy" /></div></td>
            ...
        </tr>
        ...
    </table>
</div>
软糖 2024-10-28 11:19:44

我已经通过 JavaScript 解决了我的问题。我找不到一种方法可以让所有浏览器都能正常运行而不强制它们。

我基本上循环遍历每个图像,检查其父表的大小应该是多少,然后除以行数以找到图像高度,并除以列数以获得宽度。

I have solved my problem via JavaScript. I couldn't find a way to make all the browsers play nice without forcing them.

I basically loop over each image checking what their parent tables size was supposed to be, then divide that by the number of rows to find the image height and by columns for width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文