纯 CSS 中的模块化高度 div(100px 的倍数)

发布于 2024-11-27 13:11:41 字数 301 浏览 1 评论 0原文

我有一个带有重复背景的 div。背景图像为 50 像素 x 50 像素。

由于重复图像的高度为 50 像素,因此我希望 div 高度为 50 的倍数。这将确保背景图像重复,但不会被 div 的边缘截断。

  1. 有没有一种纯 CSS 方法可以在没有媒体查询的情况下做到这一点?
  2. 有没有办法通过媒体查询来做到这一点?
  3. 我知道 JavaScript 可以做到这一点,但我宁愿不必使用它(有很多具有这种类型样式的对象,并且不想将它们全部使用 $.each() )。

I have a div with a repeating background. The background image is 50px x 50px.

Because the repeating image is 50px high, I would like the div height to be in MULTIPLES OF 50. This will assure that the background image is repeated, but not cut-off by the edge of the div.

  1. Is there a pure CSS way to do this without media queries?
  2. Is there a way to do this with media Queries?
  3. I know it's possible with JavaScript, but I wold rather not have to use it (there are lots of objects with this type of styling, and would rather not have to $.each() them all).

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

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

发布评论

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

评论(1

岁月静好 2024-12-04 13:11:41

使用当前实现的 CSS 来接近此效果的唯一方法是 border- image,但它不会完全按照您想要的方式工作。您无需将元素大小设置为 50px 的倍数,而是缩放 50px 组件,以便它们适合元素。

它的工作原理如下:用现有背景图像的九个图块制作一个 150 像素 x 150 像素的图像,然后指定如下规则:

border-image: url(bg.png) 50 round fill;

round 关键字意味着图像的中间切片将重复并适当拉伸以适合元素。仅当您将元素的最小尺寸设置为至少 150 像素正方形时,这才真正起作用。在实用性方面,支持有点参差不齐,我不认为任何浏览器都已实现当前规范。使用供应商特定属性时,您可以省略 fill 关键字。

除此之外,CSS3 Line Grid 模块设置为允许调整元素是行高的倍数,但实现起来还有很长的路要走。

The only way to get close to this with currently implemented CSS is border-image, but it wouldn't work exactly as you want. Instead of making the element size a multiple of 50px, you would instead scale the 50px components so that they fit the element.

It would work something like this: make a 150px by 150px image out of nine tiles of your existing background image, then specify a rule something like this:

border-image: url(bg.png) 50 round fill;

The round keyword means the middle slices of the image will repeat and stretch as appropriate to fit the element. This would only really work at all if you set minimum dimensions on the element to at least 150px square. On the practicalities front, support is a bit patchy, I don't think any browsers have implemented the current spec yet. You can leave off the fill keyword when using the vendor specific properties.

Other than that, the CSS3 Line Grid module is set to allow the sizing of elements in multiples of line height, but implementations are still a long way off for that.

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