Home Web Front-end HTML Tutorial css浮动与绝对定位的关系_html/css_WEB-ITnose

css浮动与绝对定位的关系_html/css_WEB-ITnose

Jun 24, 2016 am 11:15 AM

  人还是要学会思考。我初学css时候,老觉得浮动与绝对定位的功能很类似,原因很简单,因为他俩都能使正常html元素脱离元素流。

相对于绝对定位强悍的功能,浮动其实并没有完全脱离元素流,这个后面会说。

  那么什么是脱离元素流呢?要搞清楚这个问题,首先得了解什么是元素流。我的理解,元素流是html标签本应该出现在页面的位置,比如下面这段代码。

1 <div>2     <p class="p1">p1</p>3     <p class="p2">p1</p>4     <p class="p3">p3</p>5 </div>
Copy after login

 1 div>p{ 2   line-height:20px;   3 } 4 .p1{ 5   background-color:red;   6 } 7 .p2{ 8   background-color:blue;   9 }10 .p3{11   background-color:pink;  12 }
Copy after login

页面效果

正常情况下p2应该处于p1的下面,p3的上面。这是p2处在正常元素流的位置。如果给p2加一个左浮的样式会发生什么:

神奇吧,p2貌似跑到了p3的头顶,但还是位于p1下面,同时p3上面的文字却没有在p2下面,这就是p2半脱离元素流的效果(为什么是半脱离,因为它只是使它后面的元素位于它的下面,并没有影响它与p1的关系,同时p3上面的文字不会出现在p2的下面)

说到这里,你可能还是糊涂的,到底什么是真正的脱离元素流。其实,css是一门十分感性的语言,某些效果需要有强大的想象力支持才能想的通。

举个例子,我们凡人就好比没有脱离元素流的p1、p2、p3,世世代代生活在地球上,受重力影响,受诸多条件限制,老二排在老大后面,老三最小。突然有一天,老二造出了一对翅膀(浮动),随时可以离开他世世代代生活的地方(元素流)去蓝天翱翔,但是他毕竟是凡人,只是征服了蓝天而已,仍然脱离不了地球的束缚飞出大气层。况且他的翅膀是人造的,功能相对于鸟类来说还是弱爆了,他只能占据他的弟弟头顶上那一片蓝天,无法改变位置(只能去最左边或者最右边),而且如果他弟弟中也有人‘飞’起来了,他们会处在同一片天空下。

不知道看懂了没有,这就是半脱离元素流的效果,虽然享受到了蓝天的自由,但仍受到诸多限制。也许你会好奇,为什么p3上面的文字不会处在p2下面,这其实并不是bug,这才是浮动设计的初衷,浮动最初是为了实现文字环绕效果而被提出来的。

 

下面说一说position:absolute,还是刚才的p1、p2、p3,如果给p3绝对定位会发生什么:

和浮动效果很类似,只不过p2完全位于p3的上面,其实这时候p2位于p1与p3的最上面,只不过他默认会出现在他自己头顶的位置。

其实这也很好理解,刚才的浮动效果是p2借助人造翅膀实现的,是半脱离元素流的,这次p2直接成仙了,直接飞出来地球(完全脱离元素流),完全掌控他们头顶上的那一片天空,而且通过left、right、top、bottom可以控制他的位置,真是想去哪去哪。

仙人也是有等级的,佛教讲欲界六天,从低到高分别是四天王天、忉利天、夜摩天、兜率天、乐化天、他化自在天。这刚好对应绝对定位的z-index,一层盖一层,自己体会。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

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

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

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...

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

See all articles