负余量删除静态同级的背景属性 [英] Negative margin remove background property of static siblings

查看:154
本文介绍了负余量删除静态同级的背景属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在底部使用负边距拉动相邻元素重叠电流元素。我打算把它重叠。但我想让整个div重叠在图像上方。但是,事实证明,它删除了拉的元素的背景,以及。有人能解释这个吗?



 <!DOCTYPE html>< html>< head>< script src =https://code.jquery.com /jquery.min.js\"> ;</script> ;<link href =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css =stylesheettype =text / css/>< script src =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js>< / script> < meta charset =utf-8> < meta name =viewportcontent =width = device-width> < title> JS Bin< / title>< / head>< body> < style> .div1 {background-color:black; } img {margin-bottom:-20px; }< / style> < div class =container> < div class =row> < div class =col-xs-4> < img src =http://placehold.it/200x300alt => < div class =div1>这里是示例文本< / div> < / div> < / div> < / div>< / body>< / html>  



http://jsbin.com/mejoci/edit?html,css,output



EDITED:当元素定位(固定|相对|绝对)时,它是有用的,但静态位置是默认位置,即使位置不是组。

解决方案

在示例代码中,两个元素共享相同的堆叠上下文。



在这种情况下,定义元素如何分层的规则在中定义规范如下:(bold是我的)


在每个堆叠上下文中, - 前面的顺序:


  1. 形成堆叠上下文的元素的背景和边框。

  2. 子堆叠上下文与负堆叠级别(最负的第一)。

  3. 流入,非内联级别,不定位后代。 >
  4. 未定位的浮动广告。

  5. 包含内置表格和内嵌广告的流内, / strong>
  6. 子级堆栈上下文与堆栈级别0和定位后代与堆栈级别0。

  7. (最小正第一)。


因此,您可以在同一个堆叠上下文 内联级元素(#3)(#5)

所以在你的case - 因为< img> < div> 共享相同的堆叠上下文,并且< img> 元素是内联级元素 - 它在上面 >< div> - 即使< div> 稍后出现在文档树顺序中。



查看此代码演示,进一步说明了这一点





额外阅读:
$ b

详细说明堆叠上下文



Stacking without z-index(MDN)



Z-Index和CSS堆栈:哪个元素首先显示?


I am using negative margin on bottom to pull the adjacent element to overlap current element. It is my intention to make it overlap. But I want the whole div to be overlapped above the image. But, it turned out that it removes the background of the pulled element as well. Can someone explained this?

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <style>
    .div1 {
      background-color: black;
    }

    img {
      margin-bottom:-20px;
    }
  
  </style>
  
  <div class="container">
    <div class="row">
      <div class="col-xs-4">
        <img src="http://placehold.it/200x300" alt="">
        <div class="div1">
          Here is example text
        </div> 
      </div>
    </div>
  </div>

</body>
</html>

http://jsbin.com/mejoci/edit?html,css,output

EDITED: It is kinda working when the element is positioned(fixed|relative|absolute) but not with static position which is the default position even when position is not set.

解决方案

In your sample code, both elements share the same stacking context.

That being the case, the rules which define how elements are layered are defined in the spec as follows: (bold is mine)

Within each stacking context, the following layers are painted in back-to-front order:

  1. the background and borders of the element forming the stacking context.
  2. the child stacking contexts with negative stack levels (most negative first).
  3. the in-flow, non-inline-level, non-positioned descendants.
  4. the non-positioned floats.
  5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  7. the child stacking contexts with positive stack levels (least positive first).

So you can see that - within the same stacking context - inline-level elements (#5) are painted above non-inline-level elements (#3)

So in your case - since both the <img> and the <div> share the same stacking context and the <img> element is an inline-level element - it is painted above the <div> - even though the <div> occurs later in the document tree order.

Check out this codepen demo which illustrates this point further


Extra reading:

Elaborate description of Stacking Contexts

Stacking without z-index (MDN)

Z-Index And The CSS Stack: Which Element Displays First?

这篇关于负余量删除静态同级的背景属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆