图像库中的内部填充

发布于 2024-10-03 12:58:40 字数 1174 浏览 1 评论 0原文

所以,我有这个画廊功能,只在网格中显示图像。我目前正在使用 TABLE,但我想转向 CSS,以便在图像上使用 100% 宽度,这样它可以很好地缩放。

是的,所以最好通过查看此页面来解释: http://sandman.net/test/css_gallery.php< /a>

蓝色边框位于外部 DIV 上,图像保留在 DIV 层内。代码看起来像这样:

<div class="thumbs">
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
</div>

等等。样式表目前是这样的:

<style type='text/css'>
    .thumbs {
        width: 400px;
        border: 1px solid blue;
        overflow: hidden;
    }
    .thumb {
        width: 25%;
        float: left;
    }
    .thumb > .inner {
        padding: 0 10px 10px 0;
    }
</style>

SO - 我的问题。如您所见,内边距当前为 10px,这也是应该的。但不是在第四列!基本上,我希望图像是四列,中间有三个空白列。就像现在一样,每个 .thumb 包含一个计算宽度为 90px 且右侧填充为 10px 的图像。但是,它们的宽度应为 92.5 像素,并且间距均匀。

因为 - 一个问题是我无法在前三列和第四列上设置不同的边距,因为这样 100% 宽度的图像会改变大小,这是不可取的。因此,填充必须以某种方式均匀地应用于所有图像。

那么,你有什么好办法吗? :)

So, I have this gallery function that just shows images in a grid. I am currently using TABLE, but I want to move over to CSS in order to use width 100% on the images, so it scales nicely.

Right, so best explained by looking at this page: http://sandman.net/test/css_gallery.php

The blue border is on the outer DIV and the images are kept within to layers of DIV's. The code looks something like this:

<div class="thumbs">
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
</div>

And so on. And the stylesheets are currently this:

<style type='text/css'>
    .thumbs {
        width: 400px;
        border: 1px solid blue;
        overflow: hidden;
    }
    .thumb {
        width: 25%;
        float: left;
    }
    .thumb > .inner {
        padding: 0 10px 10px 0;
    }
</style>

SO - to my problem. As you can see, the padding is currently 10px, which it should be. But not on the fourth column!! Basically, I want the images to be four columns with three whitespace columns in between. As they are now, each .thumb contains an image with 90px width calculated and 10px padding to the right. But, they should instead be 92.5 pixels wide and be evenly spaced.

Because - one problem is that I can't sett different margins on the first three and the fourth column since then the 100% width image would change size, which is not desirable. So the padding has to somehow be applied uniformly over all the images.

So, do you have a good way to do it? :)

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

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

发布评论

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

评论(3

南…巷孤猫 2024-10-10 12:58:41

您还可以在 tumbs div 中添加一个包含所有 tumb div 的容器 div,并给该容器一个负边距以补偿拇指 div 边缘的填充,这不是一个漂亮的解决方案,但它适用于所有浏览器,即使是那个浏览器这与 nternet xplorer 押韵。 :)

<div class="thumbs">
  <div class="container">
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
  </div> <!--container-->
</div>

<style type='text/css'>
    .container {
    margin: 0 -10px 0 -10px;
    }
</style>

You can also add a container div in tumbs div that contains all the tumb divs and give this container a negative margin to compensate for the padding on the edges of the thumb divs, not a beautiful solution but it works in all browsers, even that one that rhymes with nternet xplorer. :)

<div class="thumbs">
  <div class="container">
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
    <div class="thumb">
        <div class="inner">
            <img />
        </div>
    </div>
  </div> <!--container-->
</div>

<style type='text/css'>
    .container {
    margin: 0 -10px 0 -10px;
    }
</style>
两仪 2024-10-10 12:58:41

好吧,我能看到的最简单的修复方法就是再使用 1 个 div 和一个微小的 CSS 调整。将 div.thumbs 包装在另一个 div 中,如下所示:

<div class="thumbs-wrapper">
    <div class="thumbs>
        <!-- same content here as before -->
    </div>
</div>

并将边框从 div.thumbs 移到新包装上:

.thumbs-wrapper {
    border: 1px solid blue;
    overflow: hidden;
    width: 390px; /* cuts off the pesky extra padding */
}

.thumbs {
    width: 400px;
}

CSS 的其余部分保持不变。结果:
截图

Okay, so the simplest fix that I can see is to use just 1 more div and a tiny CSS tweak. Wrap the div.thumbs in another div, like this:

<div class="thumbs-wrapper">
    <div class="thumbs>
        <!-- same content here as before -->
    </div>
</div>

and move the border off the div.thumbs, onto the new wrapper:

.thumbs-wrapper {
    border: 1px solid blue;
    overflow: hidden;
    width: 390px; /* cuts off the pesky extra padding */
}

.thumbs {
    width: 400px;
}

The rest of the CSS is unchanged. The result:
screenshot

滿滿的愛 2024-10-10 12:58:41

使用深奥的伪类没有任何意义……只需创建自己的伪类即可!

首先,我只是直接为图像设置一个类...不需要每个图像上都有一个容器。我还认为“边距”是比“填充”更明智的选择,所以我最终得到的 HTML 看起来像:

<div class="thumbs">
    <div class="thumb">
        <img class="inner first" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner last" src="" />
    </div>
    <div class="thumb">
        <img class="inner first" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner last" src="" />
    </div>
</div>

等等...

我假设你的数学是:(400px 宽) - (3 x 10px 边距) = 370 像素 / 4 列 = 每列 92.5 像素...但通常您不想使用半个像素,因此我将使用每列 92 像素 strong>,边距后总宽度为 368 像素。那么,既然您正在为第一个和最后一个设置自己的类,那么您的样式表应该类似于:

    .thumbs {
        width: 398px; // 368px + 30px for margin
        border: 1px solid blue; // 1px for each side, results in a total width of 400px
        overflow: hidden;
    }
    .thumb {
        width : 92px;
        float : left;
    }
    .inner {
        width : 92px;
        margin : 0 10px 10px 10px;
    }
    .first {
        margin : 0 10px 10px 0!important; //important should make sure it overrides        .inner
    }
    .last {
        margin : 0 0 10px 10px!important; //important should make sure it overrides .inner
    }

现在,我还没有测试过这个,但我认为它应该可以工作......如果没有别的,希望我的策略是有洞察力的,以便您可以根据您的需求进行调整。您可以应用手动分配和堆叠类的相同理论,以确保顶行和底行的顶部和底部分别为 10px。

希望这有帮助!

No point in using esoteric pseudo-classes... just make your own!

First of all, I'd just set a class to the image directly... no need to have a container on each image. I also think 'margin' is a smarter choice than 'padding,' so the HTML I end up with looks like:

<div class="thumbs">
    <div class="thumb">
        <img class="inner first" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner last" src="" />
    </div>
    <div class="thumb">
        <img class="inner first" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner" src="" />
    </div>
    <div class="thumb">
        <img class="inner last" src="" />
    </div>
</div>

etc...

Your math I assume is: (400px wide) - (3 x 10px margin) = 370px / 4 columns = 92.5 px per column... but typically you don't want to work with half of a pixel so I'll use 92px per column, with 368px total width after margins. So then, since you're setting up your own classes for first and last--your stylesheet should look something like:

    .thumbs {
        width: 398px; // 368px + 30px for margin
        border: 1px solid blue; // 1px for each side, results in a total width of 400px
        overflow: hidden;
    }
    .thumb {
        width : 92px;
        float : left;
    }
    .inner {
        width : 92px;
        margin : 0 10px 10px 10px;
    }
    .first {
        margin : 0 10px 10px 0!important; //important should make sure it overrides        .inner
    }
    .last {
        margin : 0 0 10px 10px!important; //important should make sure it overrides .inner
    }

Now, I haven't tested this but I think it should work... if nothing else, hopefully my strategy is insightful so that you could tailor it to your needs. You could apply the same theory of manually assigning and stacking classes to make sure the top and bottom rows both have 10px on top and bottom respectively.

Hope this helps!

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