求问一个关于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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...
