求问一个关于position为absolute时的问题_html/css_WEB-ITnose
如图 说是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>
然后发现实际效果是这样:
也就是说,当没有设置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属性在什么情况下会起作用了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit
