如何通过 CSS 使 DIV 捕捉到网格?

发布于 2024-10-01 00:30:14 字数 639 浏览 3 评论 0原文

我试图弄清楚如何通过 CSS 将 DIV 列捕捉到某个固定大小的网格。

这是我正在处理的测试页面: http://mapofemergence.com/test/MoEv2/

我想要得到的是,正确的 div(绿色的)以某种方式捕捉到背景网格四边形:调整浏览器窗口大小时,红色四边形应分布在中间区域,而绿色列应“填充”页面右侧的剩余区域,但始终与网格对齐(在左侧)。

基本上,作为网格的四边形大小,右侧绿色 div 应该具有可变宽度,等于或大于 a,并且无论如何小于 2a (在这种情况下,它应该设置回 a 的宽度,同时还有一个红色四边形从下一行移动到上一行)。

这是一个更好的想法的图像:

alt text

(抱歉,我的声誉不允许超链接)

我不太确定这可以通过 CSS 完成,但我确信你们中的一些人可以帮助找到一些解决方案或解决方法。如果可能的话,我不会使用 javascript。

感谢您的帮助, s

I'm trying to figure out how to have a DIV column snapping to some fixed-size grid, via CSS.

Here's the test page I'm working on: http://mapofemergence.com/test/MoEv2/

What I'm trying to get, is the right div (the green one) someway snapping to the background grid quads: when resizing the browser window, the red quads should distribute in the middle area, while the green column should "fill" the remaining area on the right side of the page, and yet remaining always aligned (on the left) with the grid.

Basically, being a the grid's quad size, the right green div should have a variable width, equal to or greater than a, and anyway minor than 2a (in which case it should set back to a width of a, while having one more red quad moving from the lower row to the upper one).

here's an image to get a better idea:

alt text

(sorry, my reputation doesn't allow to hyperlink)

I'm not really sure this can be done via CSS, but I'm sure that some of you can help finding some solution or workaround. I wouldn't use javascript, if possible.

Thanks for your help,
s

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

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

发布评论

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

评论(1

燃情 2024-10-08 00:30:14

不幸的是,HTML/CSS 没有实现您想要的功能所需的功能。您只能使用 JavaScript 来实现它。

您应该将一个函数绑定到窗口 resize 事件,该事件会将绿色的 div 宽度设置为所需的值。在 jQuery 中,它应该看起来像这样:

$(window).resize(function() {
    $("#rx").width(
        parseInt($("#rx").css("min-width").slice(0, -2)) + (
            ($(window).width() - $("#lx").width() - $("#rx").css("min-width").slice(0, -2)) % $(".module:first").outerWidth(true)
        )
    );
});

请注意,此代码可以轻松优化,但我想让它尽可能简单。

Unfortunately HTML/CSS don't have features necessary to do what you want. You can only achieve it by using JavaScript.

You should bind a function to window resize event which will set green's div width to desirable value. In jQuery it should look something like that:

$(window).resize(function() {
    $("#rx").width(
        parseInt($("#rx").css("min-width").slice(0, -2)) + (
            ($(window).width() - $("#lx").width() - $("#rx").css("min-width").slice(0, -2)) % $(".module:first").outerWidth(true)
        )
    );
});

Note that this code can be easily optimised but I wanted to make it as simple as possible.

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