简体   繁体   English

如何将此背景图像效果添加到div?

[英]How to add this background-image effect to a div?

I need to add the following detail image to each div of a certain class on my page. 我需要在页面上某个类的每个div上添加以下详细图像。

堆文件详细

In the wireframes, it looks like this: 在线框中,它看起来像这样:

线框图像

I can't just use the above image entirely because the white space can be variable heights. 我不能只使用上面的图像,因为空白可以是可变高度。 How would I achieve this using the background image property in CSS? 如何使用CSS中的background image属性实现此目的? Everything I've tried so far doesn't work. 到目前为止,我尝试的所有操作均无效。 Any ideas? 有任何想法吗?

You could use :after combined with absolute positioning. 您可以将:after与绝对定位结合使用。

See: http://jsfiddle.net/thirtydot/ChJnr/4/ 参见: http : //jsfiddle.net/thirtydot/ChJnr/4/

HTML: HTML:

<div class="box"></div>

CSS: CSS:

.box {
    width: 250px;
    background: #ccc;
    position: relative;
    padding: 16px;
}
.box:after {
    content: '';
    position: absolute;
    left: 16px;
    right: 16px;
    bottom: -16px;
    background: url(http://dummyimage.com/250x16/f0f/fff);
    height: 16px;
}

If you need this to work in older browsers, you could instead add a harmless little span to replace the use of :after . 如果您需要在较旧的浏览器中使用它,则可以添加一个无害的小span来代替:after

2 Options: 2个选项:

Option 1: You make 3 divs which would look something like this: 选项1:您制作了3个div,看起来像这样:

<div class="container">
<div class="content">Stuff</div>
<div class="bottom"></div>
</div>

Since you are using a transparent image for your bottom image, you need to make sure you don't set a background-color to the container div, instead set it to the content div. 由于您在底部图像上使用的是透明图像,因此需要确保您没有为容器div设置背景色,而是将其设置为内容div。

Option 2: Use CSS3 border-image property. 选项2:使用CSS3 border-image属性。

The one drawback to this is that the image you are using for the bottom border has to have the same color background as the background of whatever div it's on top of. 这样做的一个缺点是,用于底部边框的图像必须具有与位于其上的任何div的背景相同的颜色背景。 Example: 例:

With transparent background: http://jsfiddle.net/charlescarver/k9uyx/ 具有透明背景: http//jsfiddle.net/charlescarver/k9uyx/

This also requires the browser supports CSS3. 这还需要浏览器支持CSS3。

.someClass {
    backgroung-image: url('path-to-image-form-css-file');
    height: 100px;
    width: 100px;
    padding: ...
}

使其分为3个div-一个包含整个“原始” div,一个div浮动到容器div的底部以容纳底部图像,另一个包含其他所有内容。

Personally, I would create a class like that. 就个人而言,我将创建一个类似的类。 It's a guess, but I believe this should work OK: 这是一个猜测,但是我认为这应该可以:

.someClass {
    background: #FFF url('path') no-repeat center bottom;
    width: 175px;
}

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

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