简体   繁体   English

背景图像可以延伸到div的边界之外吗?

[英]Can background image extend beyond div's borders?

Can background image extend beyond div's borders? 背景图像可以延伸到div的边界之外吗? Does overflow: visible apply to this? 溢出:可见是否适用于此?

No, a background can't go beyond the edge of an element. 不,背景不能超出元素的边缘。

The overflow style controls how the element reacts when the content is larger than the specified size of the element. overflow样式控制当内容大于元素的指定大小时元素如何反应。

However, a floating element inside the div can extent outside the div, and that element could have a background. 但是, div内的浮动元素可以扩展到div之外,并且该元素可以具有背景。 The usefulness of that is limited, though, as IE7 and earlier has a bug that causes the div to grow instead of letting the floating element show outside it. 不过,这样做的作用是有限的,因为IE7和更早版本存在一个错误,该错误会导致div增大,而不是让浮动元素显示在其外部。

Following up on kijin 's advice, I'd like to share my solution for image offsets: 遵循kijin的建议,我想分享我的图像偏移解决方案:

/** 
  * Only effective cross-browser method to offset image out of bounds of container AFAIK, 
  * is to set as background image on div and apply matching margin/padding offsets:
  */

#logo {
  margin:-50px auto 0 auto;
  padding:50px 0 0 0;
  width:200px;
  height:200px;
  background:url(../images/logo.png) no-repeat;
}

I used this example on a simple div element <div id="logo"></div> to position my logo with a -50px vertical offset. 我在一个简单的div元素<div id="logo"></div>上使用了此示例,将我的徽标定位为-50px垂直偏移。 (Note that the combined margin/padding settings ensure you don't run into collapsing margin issues.) (请注意,合并的边距/填充设置可确保您不会遇到崩溃的边距问题。)

not possible to set a background image 'outside' it's element, 无法将背景图像设置在其元素的“外部”,

BUT YOU CAN DO what you want with using 'PSEUDO' element and make that whatever size you want and position it wherever you want. 但是 ,您可以使用“ PSEUDO”元素来做您想做的事情,并使其具有所需的大小,然后将其放置在所需的位置。 see here : i have set the arrow outside the span 看到这里:我将箭头设置在跨度之外 在此处输入图片说明

here is the code 这是代码

HTML : HTML:

<div class="tooltip">
<input class="cf_inputbox required" maxlength="150" size="30" title id="text_13" name="name" type="text"><span class="msg">dasdasda</span>
</div>

strong text 强文本

 .tooltip{position:relative; float:left;}
    .tooltip .msg {font-size:12px;
        background-color:#fff9ea;
        border:2px #e1ca82 solid;
        border-radius:5px;
        background-position:left;
        position:absolute;
        padding:4px 5px 4px 10px;
        top:0%; left:104%;
        z-index:9000; position:absolute; max-width:250px;clear:both;
    min-width:150px;}


    .tooltip .msg:before {
        background:url(tool_tip.png);
        background-repeat:no-repeat;
    content: " ";
        display: block;
        height: 20px;
        position: absolute;
        left:-10px; top:1px;
        width: 20px;
        z-index: -1;
        }

see here example: http://jsfiddle.net/568Zy/11/ 参见此处示例: http : //jsfiddle.net/568Zy/11/

No, the background won't extend beyond the borders. 不,背景不会超出边界。 But you can stretch the border as far as you want using padding and some clever tweaking of negative margins & position. 但是,您可以使用padding和负边距和位置的一些巧妙调整来将边框延伸到所需的范围。

I understand this is really really late, and I am not even sure if this is best practice but I found a little way to do this with my footer. 我知道这真的很晚,我甚至不确定这是否是最佳做法,但我找到了一些方法来实现此页脚。 My last section had a background image that I wanted to overflow into the footer and I fixed it with a few lines of CSS. 我的最后一部分有一个背景图像,我想溢出到页脚中,并用几行CSS对其进行了修复。 Also added a little padding the section with the background image. 还添加了一些小节来填充背景图像。

footer{
 background-color: transparent!important;
 top: -50px;
 margin-bottom: -50px;
}

I tried using negative values for background-position but it didn't work (in firefox at least). 我尝试对background-position使用负值,但它不起作用(至少在Firefox中)。 There's not really any reason for it to. 确实没有任何理由这样做。 Just set the background image on one of the elements higher up in the hierarchy. 只需在层次结构中较高的元素之一上设置背景图像。

经过一点研究:否和否:)

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

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