Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial 关于浮动和绝对定位的区别,到底是怎么回事?_html/css_WEB-ITnose

关于浮动和绝对定位的区别,到底是怎么回事?_html/css_WEB-ITnose

Jun 24, 2016 am 11:35 AM

这有一个小例子:
样式:


结构:



1111111


1111111


1111111


1111111




2222222


2222222


2222222


2222222




3333333


3333333


3333333


3333333





结果是这样的:


我的疑问有:
1、浮动是不是使div1彻底脱离了文档流,在文档流里有没有占据位置?
2、为什么div1和div2重合之后,把div2里的文字挤到了边上去?就是说div1与div2重合了,为什么不能与div2里的段落重合?

请求高手详解!!!


回复讨论(解决方案)

绝对定位 才会脱离文档流
楼主 可以去google看看  float和 display   clear  

float是浮动 没有脱离文档流

 一堆有次序的东西 其中有一个是 float 
那么你可以想象 他像铁块碰见磁铁一样飞向边缘 空间还是占着(默认的width属性变成auto了)

你说的这两个问题,看W3C中,关于float的解释多直白的,下面两句就是W3C中关于float的叙述:
浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。

详细内容,直接点击查看: http://www.w3school.com.cn/css/css_positioning_floating.asp

还有你说的文字为啥会出现这个情况,也有说明。

你说的这两个问题,看W3C中,关于float的解释多直白的,下面两句就是W3C中关于float的叙述:
浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。

详细内容,直接点击查看: http://www.w3school.com.cn/css/css_positioning_floating.asp

还有你说的文字为啥会出现这个情况,也有说明。


“由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。”这句话有点儿糊涂,求详细的解释

<html><head><title></title><meta content="text/html; charset=GB2312" /><style>.a{border:1px solid red;width:960px;margin:0 auto;}.b{float:left;height:100px;width:100px;background-color:#aaa}.c{height:50px;background-color:#ddd;}</style></head><body><div class = "a">	<div class = "b"></div>	<div class = "c"></div></div></body></html>
Copy after login
Copy after login


效果图:


就拿这段代码说吧,我用a,b,c表示这三个div。
a和c是普通流,b是浮动框。

1:按理论来说,a的高度是以它子元素的总的高度计算的,但是在图中可以看出,a的高度只是和c的高度相同,就是它忽略了b的高度。
2:块框之间,都是上下排列的,每一个块框都占据单独的一行,可以这里,c就当没有看到b存在一样。
这不就是再说,普通流的布局是不考虑是不是在它之前,有浮动框的?

这样说,不知你能清楚我的意思不。

为什么文字会避开,就如同你上面给的代码,这个应该是浏览器本身有对文字进行处理吧,你可以用浏览器调试工具看下,文字的标签

元素,其实还是占据一整行的,只是文字并没有从最左边开始。文字表现这块,是个特例。

<html><head><title></title><meta content="text/html; charset=GB2312" /><style>.a{border:1px solid red;width:960px;margin:0 auto;}.b{float:left;height:100px;width:100px;background-color:#aaa}.c{height:50px;background-color:#ddd;}</style></head><body><div class = "a">	<div class = "b"></div>	<div class = "c"></div></div></body></html>
Copy after login
Copy after login


效果图:


就拿这段代码说吧,我用a,b,c表示这三个div。
a和c是普通流,b是浮动框。

1:按理论来说,a的高度是以它子元素的总的高度计算的,但是在图中可以看出,a的高度只是和c的高度相同,就是它忽略了b的高度。
2:块框之间,都是上下排列的,每一个块框都占据单独的一行,可以这里,c就当没有看到b存在一样。
这不就是再说,普通流的布局是不考虑是不是在它之前,有浮动框的?

这样说,不知你能清楚我的意思不。

为什么文字会避开,就如同你上面给的代码,这个应该是浏览器本身有对文字进行处理吧,你可以用浏览器调试工具看下,文字的标签

元素,其实还是占据一整行的,只是文字并没有从最左边开始。文字表现这块,是个特例。


谢谢你的耐心解答,我说的我都知道,但是就是文字表现这块儿不懂,大家都说是特例,但我想明白这是怎么回事,在元素浮动的时候,普通文档流是怎么变化的。如果你能回答就太好了!不管怎样,这分就给你了!

这个,我也不知道咋回事,应该是浏览器规定的吧,不过没有看到过相关的文章。
所以,还真是回答不了。

这个问题我也纳闷 貌似这篇文章http://blog.sina.com.cn/s/blog_5f90da9b01016cmo.html有讲到但是我看不懂

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

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.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

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

See all articles