简体   繁体   English

使背景图像位于 div 之外

[英]making a background image be outside a div

I'm trying to make a background image be outside a div and can't figure out how to do this (if even it's possible).我正在尝试将背景图像放在 div 之外,但不知道如何执行此操作(如果可能的话)。 My HTML:我的 HTML:

<div id="test"></div>

My CSS:我的CSS:

#test {
    width: 50px;
    height:50px;
    background: 0 50px url('https://developers.google.com/_static/images/developers-logo.svg') blue;
}

A stand-alone demo:一个独立的演示:

http://jsfiddle.net/568Zy/ http://jsfiddle.net/568Zy/

The demo shows the image within the 50x50 div.该演示显示了 50x50 div 内的图像。 What I was hoping for was to have the background image start at 0px from the top and 50px from the left.我希望背景图像从顶部 0px 开始,从左侧 50px 开始。

Any ideas?有什么想法吗?

Your question does not make it clear exactly what you want the end result to look like.您的问题并没有明确说明您希望最终结果是什么样子。

It is not possible to make a background image 'overflow' it's element, however you can apply the background image to a pseudo element and make that whatever size you want and position it wherever you want.不可能使背景图像“溢出”它的元素,但是您可以将背景图像应用于伪元素并使其成为您想要的任何大小并将其放置在您想要的任何位置。

I have used this technique on your example: http://jsfiddle.net/ybw750jd/我在你的例子中使用了这种技术: http : //jsfiddle.net/ybw750jd/

#test {
    background: blue;
    height:50px;
    position: relative;
    width: 50px;
}
#test:before {
    background: url("https://picsum.photos/450/100") repeat scroll 0 0 transparent;
    content: " ";
    display: block;
    height: 100px;
    position: absolute;
    width: 450px;
    z-index: -1;
}

If this is not the effect you want, please rephrase your question and consider making a mock up image showing what you want it to look like.如果这不是您想要的效果,请重新表述您的问题并考虑制作一个模拟图像,显示您想要的效果。

Try this: http://jsfiddle.net/568Zy/16/ .试试这个: http : //jsfiddle.net/568Zy/16/ Essentially, you're creating two <div> elements, and set one to be absolute with a z-index: 0;本质上,您正在创建两个<div>元素,并使用z-index: 0;将一个元素设置为绝对元素z-index: 0; on one and z-index: 1;在 1 和z-index: 1; on the other.在另一。

<div id="test">zzz</div>
<div class="z-index"></div>

#test {
    background: blue;
    width: 50px;
    height:50px;
    position: relative;
    z-index: 1;
}

.z-index {
    position: absolute;
    background: url('https://developers.google.com/_static/images/developers-logo.svg');
    z-index: 0;
    width: 300px;
    height: 100px;
   top: 0px;
   left: 50px;
}

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

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