search
HomeWeb Front-endHTML TutorialCSS学习笔记--CSS中定位的浮动float_html/css_WEB-ITnose

昨天在解决了盒模型的问题之后又出现了新的知识模糊点:浮动和绝对定位?今天先解决浮动相关的问题,首先列举出想要解决的问题:

1.浮动到底是怎么样的?

2.浮动对元素的影响有什么?

3.浮动主要用来干什么?

第一个问题:浮动到底是怎么样的?

W3CSCHOOL对浮动属性的解释: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。所以浮动也有left、right、none三种。

我个人的理解是: HTML 文件就像是一个方形的水槽,它在浏览器中加载的过程就好比是向水槽中放水,而这些水就代表的是页面中的各个元素,他们都是有顺序的进入水槽(文档流的顺序 和我们写字一样,从上到下从左到右)。当出现了一个具有浮动属性(float)的元素时,就好像是水流中多了一块泡沫,它会浮在水面上(也就是说明元素脱 离了文档流)。在水流停止后(页面加载完毕),这个元素会停靠在水槽的边缘或者停靠在别的泡沫边缘(浮动元素会处于包含框的边缘或者另一个浮动元素的边 缘)。

对于浮动元素是否脱离了文档流,这个我刚开始也很迷糊,因为在看教学视频的时候一个老师说没有,另一个老师说有。于是就自己敲了代码做了点实验:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><style>    #div1{        height: 100px;        background-color: #e13b00;        float: left;        clear: both;    }</style><body>    <div id="div1">        div1div1d    </div>    <p>1234567890    </p>    <p>1234567890    </p></body></html>

上面代码的效果如下:

红色背景的Div是设置了左浮动的元素,根据浮动的定义它应该是脱离文档流的,具体表现如下图:

当我们审查第二个p标签时发现这个标签其实是覆盖了DIV块的,还是独自占据一行,并不是从DIV块的边缘开始的。这就说明:

设置的浮动的元素其实是脱离的文档流的,但是这个元素的内容还是会在视觉上占据空间,不会覆盖其他元素或者被其他元素覆盖。

2.浮动的元素对其他元素的影响有什么?

浮动元素对其他元素的影响就如同上面的例子显示的,浮动的元素会根据自己内容的大小把他之后的元素的内容挤到后面(特别注意,这里说的是内容!是内容!是内容!)。

这里特别说明一下: 有些视频教学里说浮动的影响只会作用在紧邻在它后面的元素,这个说法还是有问题的,我上面举得那个例子就说明了红色的DIV块浮动影响的是它后面的两个p元素,而浮动的元素到底会影响多大范围是根据浮动的元素它的内容的大小决定的。

清除浮动带来的影响主要是通过两种方式:

第一种: 给不想受到影响的元素增加属性clear:both/right/left. 这个属性的意思并不是清除什么,而是应该理解为拥有这个属性的元素左边或者右边不允许存在浮动元素的内容。

例如我们给第一个p标签增加了clear:left;这样一个属性,那么它就会从DIV块下面开始出现,同时会把第二个p标签也带下来(因为他们是按照顺序显示的)。而我们给第二个p标签增加clear:left,第一个p标签还是受到浮动的影响的。

第二种: 给不想受到影响的元素增加属性width:100%;overflow:hidden;  这个方法也是有效的。

3.浮动主要用来干什么?

其实浮动的作用还是很重要的, 我们可以利用它实现很常见的两列或者多列的网页布局 ,下面举个例子:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><style>    *{margin: 0px;padding: 0px}    #div{        width: 70%;        margin: auto;    }    #header{        height: 200px;        background-color: darkslategrey;    }    #main{        height: 300px;        padding: 2%;        background-color: #bfbfbf;    }    #left{        float: left;        width: 30%;        height: 300px;        background-color: #0044aa;    }    #right{        float: right;        width: 65%;        height: 300px;        background-color: yellow;    }    #footer{        height: 200px;        background-color: rosybrown;    }</style><body>    <div id="div">        <div id="header">        </div>        <div id="main">            <div id="left"></div>            <div id="right"></div>        </div>        <div id="footer">        </div>    </div></body></html>

效果如下:

这就是我们经常看到的一种网页布局,当然我这个例子有点简陋,其实也可以把中间的部分换成三列或是更复杂的形式。

对浮动的学习就到这里了,今天有一种感受就是:学习知识的过程中还是要多敲代码多实验,不能只看书中怎么做或者视频里面老师怎么做,他们思想也并不是就是完全正确。对于疑惑的问题不能马虎带过。当然不仅仅只是学习,在任何时候都该保持有自己的想法。

明天解决绝对定位的问题。

2016年01月09日

不积跬步,无以至千里

PS:我查阅的大部分资料都是来自于网络,如有侵权,请联系我删除

Statement
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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

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.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

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.

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 <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

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

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

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

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

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor