简体   繁体   English

一张背景图片,多个 Div

[英]One Background Image, Multiple Divs

I am going to explain my question with a series of images.我将用一系列图像来解释我的问题。

A div container will have the following background image:一个 div 容器将具有以下背景图像:

在此处输入图片说明

On top of the image, there will be tile like divs:在图像的顶部,会有像 div 一样的平铺:

在此处输入图片说明

My goal is to make the background image visible to only the tiles, and the rest hidden:我的目标是使背景图像仅对图块可见,其余部分隐藏:在此处输入图片说明

Any help is greatly appreciated, thank you!非常感谢任何帮助,谢谢!

Use the background-attachment attribute like in the following fiddle:使用 background-attachment 属性,如以下小提琴所示:

http://jsfiddle.net/1ovd3cnk/1/ http://jsfiddle.net/1ovd3cnk/1/

each image block gets the following:每个图像块获得以下信息:

background-image: url(http://i.stack.imgur.com/fOV15.png);
background-attachment: fixed;

edit: was setting margin on row instead of conatiner编辑:在行上设置边距而不是容器

Sorry reply like this, I don't have reputation enough to add comment in other comments, but making some adds to the earlier answer:抱歉这样回复,我没有足够的声誉在其他评论中添加评论,但对之前的答案做了一些补充:

The image shifted could be fixed using:可以使用以下方法修复移动的图像:

background-size: cover;

But remember that cover property isn't supported in old browsers.但请记住,旧浏览器不支持cover属性。 http://caniuse.com/#search=background-size http://caniuse.com/#search=background-size

So if you want cross old browser compatibility I recommend you to try javascript and do some maths using the width and height from your elements to adjust de background image position.因此,如果您想要跨旧浏览器兼容性,我建议您尝试使用 javascript 并使用元素的宽度和高度进行一些数学运算来调整背景图像位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM