求问一个关于position为absolute时的问题_html/css_WEB-ITnose

WBOY
Freigeben: 2016-06-24 11:22:05
Original
1132 Leute haben es durchsucht


如图 说是absolute时如果没有设置left和top时 它不会占据位置 这里该怎么理解?
我尝试了一下如下的代码:

<style type="text/css">    .z{      position: relative;      background-color: #00c0ef;      margin: 10px 20px 10px 20px;    }    .p{      background-color: #00a65a;      position: absolute;      width: 200px;    }</style><body>  <div class=" z">    zpc    <div class=" p">      zpc    </div>  </div></body>
Nach dem Login kopieren

然后发现实际效果是这样:

也就是说,当没有设置left和top时,内嵌的div并没有被包括在外部的div中 也就是不占位置,但是这个究竟是什么原理?


回复讨论(解决方案)

这时.z的relative写不写都是一样的,因为.p已经不以父元素为参考元素:


However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

这时.z的relative写不写都是一样的,因为.p已经不以父元素为参考元素:


However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.


.p不是没有设定TRBL吗 应该以父级为参照点 就是.z?

这时.z的relative写不写都是一样的,因为.p已经不以父元素为参考元素:


However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.


还是说我理解错了 如果没有定义TRBL 则直接以body为参照?
如果这样 那div.p怎么会有这样的偏移量呢向左偏了和z一样的10px?


这时.z的relative写不写都是一样的,因为.p已经不以父元素为参考元素:


However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.


还是说我理解错了 如果没有定义TRBL 则直接以body为参照?
如果这样 那div.p怎么会有这样的偏移量呢向左偏了和z一样的10px?
看了半天终于懂了= =

绝对定位position:absolute;说它不占位置,是因为它不再影响后面兄弟元素的定位。

你可以这样理解:未设置定位的元素是二维的,它们之间在同一平面,互相影响,而设置了定位的元素是三维的;
绝对定位相当于它跑到另外一个平面去了,当然对以前的平面不再有影响;
另外,当然 相对定位也可以这样理解,只不过它比较霸道,不但自己跑到另外一个平面去了,原来的平面上的位置还要给它留着。
不知道你看懂了没?如果懂了,恭喜你!那么你肯定也懂了 z-index属性在什么情况下会起作用了

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!