Add New Notes

This commit is contained in:
geekard
2012-08-08 14:26:04 +08:00
commit 5ef7c20052
2374 changed files with 276187 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-05-15T20:18:55+08:00
====== CSS中float浮动属性详解 ======
Created Sunday 15 May 2011
http://www.tonjay.com/website/css-float.html
搞网页布局的朋友都知道浮动float真是一个好用的css属性。但有些时候一不小心就有可能因为这个小小的属性带来很大的麻烦。今天我们就打入到敌人内部看看浮动float究竟是个什么玩意儿。知已知彼方能百战不殆嘛。
===== 什么是浮动? =====
浮动是 css 的定位属性。我们可以看一下印刷设计来了解它的起源和作用。印刷布局中,文本可以按照需要围绕图片。一般把这种方式称为“文本环绕”。这是一个例子:
{{./1-print-layout.png}}
在排版软件里面,存放文字的盒子可以被设置为允许图文混排,或者无视它。无视图文混排将会允许文字出现在图片的上面,就像它甚至不会在那里一样。这就是图片是否是页面流的一部分的区别。网页设计与此非常类似。
在网页设计中应用了CSS的float属性的页面元素就像在印刷布局里面的被文字包围的图片一样。**浮动的元素仍然是网页流的一部分**。这与使用绝对定位的页面元素相比是一个明显的不同。**绝对定位的页面元素被从网页流里面移除了**,就像印刷布局里面的文本框被设置为无视页面环绕一样。绝对定位的元素不会影响其它元素,其它元素也不会影响它,无论它是否和其它元素挨着。
像这样在一个元素上用CSS设置浮动
#sidebar { float: right; }
fload属性有四个可用的值Left 和Right 分别浮动元素到各自的方向None (默认的) 使元素不浮动Inherit 将会从父级元素获取float值。
===== 浮动的用处 =====
除了简单的在图片周围包围文字,浮动可用于创建全部网页布局
{{./2-web-layout.png}}
浮动对小型的布局同样有用。例如页面中的这个小区域。如果我们在我们的小头像图片上使用浮动,当调整图片大小的时候,盒子里面的文字也将自动调整位置:
{{./3-reflow-example-1.png}}
同样的布局可以通过在**外容器使用相对定位,然后在头像上使用绝对定位来实现**。这种方式中,文本不会受头像图片大小的影响,不会随头像图片的大小而有相应变化。
{{./4-reflow-example-2.png}}
===== 清除浮动 =====
清除(clear)是浮动(float)的相关属性.一个设置了清除浮动的元素不会如浮动所设置的一样,向上移动到浮动元素的边界,而是会忽视浮动向下移动。如下,一图顶千言。
{{./5-unclearedfooter.png}}
上例中,侧栏向右浮动,并且短于主内容区域。页脚(footer)于是按浮动所要求的向上跳到了可能的空间。要解决这个问题,可以在页脚(footer)上清除浮动,以使页脚(footer)待在浮动元素的下面。
{{./6-clearedfooter.png}}
#footer { clear: both; }
清除(clear)也有4个可能值。最常用的是 both清楚左右两边的浮动。left 和 right 只能清楚一个方向的浮动。none 是默认值只在需要移除已指定的清除值时用到。inherit 应该时第五个值,不过很奇怪的是 IE 不支持(这个不奇怪吧IE 从来都这么特立独行吧 -糖伴西红柿注)。只清除左边或右边的浮动,实际中很少见,不过绝对有他们的用处。
===== 伟大的塌陷 =====
使用浮动(float)的一个比较疑惑的事情是__他们怎么影响包含他们的父元素的__。__如果父元素只包含浮动元素那么它的高度就会塌缩为零__。如果父元素不包含任何的可见背景这个问题会很难被注意到但是这是一个很重要的问题。
{{./8-collapse.png}}
塌陷的直观对立面更不好,看看下面的情况:
{{./7-directionalclearing.png}}
当上面的块级元素自动扩展以适应浮动元素时,段落间的文本流中会出现非自然的空白换行,而且没有有效的方法来修正这个问题。对于这种情况,设计师的抱怨会更甚于对塌陷的抱怨。
为了防止怪异的布局和跨浏览器的问题塌陷问题几乎总是被要处理的。__我们在容器中的浮动元素之后容器结束之前来清除浮动__。
===== 清除浮动的技术 =====
如果你很**明确的知道接下来的元素会是什么**,可以使用 clear:both; 来清除浮动。这个方法很不错,它不需要 hack不添加额外的元素也使得它有良好的语义性。当然事情并不是都可以这样解决的工具箱中还是需要另外几个清除浮动的工具。
__空div方法 __ 从字面来看,是一个空的 div。有时可能会用或者一些其他元素但是 div 是最常用的,因为它**没有浏览器默认样式;没有特殊功能,而且一般不会被 css 样式化**。这个方法因为只是为了表现,**对页面没有上下文涵义而被纯语义论者嘲笑**。诚然,从严格的角度来说他们是对的,但是这个方法有效而且没有任何伤害。
__ overflow方法__ 在父元素上设置 overflow 这个 css 属性。如果父元素的这个属性设置为 auto 或者 hidden父元素就会扩展以包含浮动。这个方法有着较好的语义性因为他不需要额外元素。但是如果需要增加一个新的 div 来使用这个方法,其实就和空 div 方法一样没有语义了。而且要记住overflow 属性不是为了清除浮动而定义的。要小心不要覆盖住内容或者触发了不需要的滚动条。
__简单清除方法 __使用了一个聪明的 css 伪选择符(:after)来清除浮动。比起在父元素上设置 overflow只需要给它增加一个额外的类似于”clearfix”的类。这个类使用如下 css:
.clearfix:after {content: “.”; visibility: hidden; display: block; height: 0; clear: both;}
这会在清除浮动的父元素之后应用一点看不见的内容。这不是全部内容,还需要一些额外的代码来适应那些老旧的浏览器。
不同的情况需要不同的浮动清除方法。以一个具有不同样式块的网格为例:
{{./10-grid-blocks.png}}
为了从视觉上较好的把相似的块联系起来,需要在必要的地方开启新行,这里是颜色改变的地方。如果每个颜色组都有一个父元素的话,我们可以使用 overflow 或者简单清除方法,或者在每组之间用一个空div方法。
{{./11-grid-blocks-cleared.png}}
===== 浮动的问题 =====
浮动因脆弱而饱受诟病。大多数的脆弱性来自于 IE6 及其一系列的浮动相关 bug。因为越来越多的设计师不再支持 IE6 了,你也可以不关注它了。不过对于那些要关注的人来说,这里有些大概。
__推倒__ 是浮动元素内的元素大多是图片比浮动元素本身宽造成的现象。大多数的浏览器会在浮动之外渲染图片但是不会有伸出来的部分影响其他布局。IE 会扩展浮动来包含图片,精彩大幅度地影响布局。一个普遍的例子是突破伸出主内容之外把侧栏推到下面(这是因为两栏都是浮动的)。
{{./12-pushdown2.png}}
快速修正:确保不是图片造成这种情况,使用 **overflow:hidden** 来切除多余的部分。
双倍边距bug 处理 IE6 时,另一个需要记住的事情是,如果在和浮动方向相同的方向上设置外边距(margin),会引发双倍边距。快速修正:给浮动设置 display:inline; 而且不用担心,它依然是块级元素
3像素间距 是指挨着浮动元素的文本会神奇的被踢出去3像素好像浮动元素的周围有一个奇怪的力场一样。快速修正在受影响的文本上设置宽度或高度。
IE7 中,底边距 bug是当浮动父元素有浮动子元素时这些子元素的底边距会被父元素忽略掉。快速修正用父元素的底内补白(padding)代替。

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@@ -0,0 +1,101 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-05-15T16:47:17+08:00
====== How to completely enclose a floated element in CSS2 ======
Created Sunday 15 May 2011
http://www.cs.hmc.edu/~mbrubeck/clear-after/
Author: Matt Brubeck
Date: 2003-06-24
===== Introduction =====
Consider the following CSS and HTML:
.item { border: 1px solid black; padding: 0.5em; }
img { float: left; }
<div class="item">
<img src="monkey.png"/>
<p>One time I hired a monkey to
take notes for me in class.</p>
</div>
Here's how this renders in Mozilla (example 1):
{{./screenshot1.png}}
Screenshot 1
**PS:**这是因为img已经脱离了文档流**div的大小由其padding和内部的段落空间决定**。下图为一个设置了padding:10px的dvi其中内容为一个段落。
注意段落框大小由其上下margin和内容高度决定。
{{./space-of-dvi.png?width=600}}
If the image is taller than the text, it overruns the border of the enclosing div (except in IE/Win; see browser compatibility below). In some cases this is the desired behavior, but what if we want the border to fully enclose both the image and the text? This document describes the only way I know to do this using completely general, standards-compliant, clean CSS and HTML.
===== Spacer div hack =====
One common solution to this problem is known as the "spacer div" hack. We can __force the outer div to fully contain both elements by adding an element with __**clear: both **__to the end of the div:__
img { float: left; padding: 0.5em; }
.item { border: 1px solid black; }
**.spacer { clear: both; }**
<div class="item">
<img src="monkey.png"/>
<p>One time I hired a monkey to
take notes for me in class.</p>
**<div class="spacer"></div>**
</div>
This works, but it requires us to add a new element to our HTML for purely presentational reasons. That means we would have to add markup to every "item" div on our site, __costing us time and bandwidth.__ A CSS-only solution would save effort and bandwith, and avoid polluting our markup with meaningless elements.
**PS**div的框参数为0因此上面加粗的空内容div大小为0div 没有浏览器默认样式,没有特殊功能,而且一般不会被 css 样式化。
===== CSS3 clear-after property =====
The CSS3 working draft defines a new property,__ clear-after__, which has the desired effect without any markup changes. We can return to the original markup, and simply add the following rule to our stylesheet:
__.item __{ clear-after: both; }
This rule has the same effect as an element with clear: both at the end of the div, as in the "spacer div" hack above. Unlike the spacer div hack, it achieves this effect without needing the change the markup.
Unfortunately, the clear-after property is not implemented by any current browsers. CSS3 is still in development, and the clear-after property may change before it is standardized.
===== Solution: CSS2 generated content =====
Fortunately, we can create the same effect using only CSS2:
.item__:after __{ content: ""; display: block; height: 0; clear: both; }
This uses the same method as the "spacer div" hack:** it adds an element at the end of the container div**, forcing its bottom border to clear the float. By using the __:after pseudo-selector__ we can generate the extra element from our stylesheet, with no changes to our HTML files.
Here's the new version of our markup:
.item { border: 1px solid black; padding: 0.5em; }
img { float: left; }
__.item:after { content: ""; display: block; height: 0; clear: both; }__
<div class="item">
<img src="monkey.png"/>
<p>One time I hired a monkey to
take notes for me in class.</p>
</div>
and this what it looks like in Mozilla (example 2):
{{./screenshot2.png}}
Screenshot 2
**PS**上面的display:block是必需的因为行内元素的上下margin是不起作用的。
==== Browser compatibility ====
The :after pseudo-selector is supported in Mozilla 1.x, Netscape 6 and 7, and Opera 7.
It is not supported in IE 5 or 6, for either Windows or Macintosh (I haven't tested IE/Mac).
(Updated 2003-06-24) Adding height: 100%; causes IE6/Win to expand a containing block around its floated children. Example 3 uses this technique to create a page that works in Mozilla, Netscape 6/7, IE6, and Opera 7 (not tested with other browsers). Note that the containing block must be inside another block-level element. If its immediate parent is the body element, then the height: 100% style will make it fill the entire viewport, at least in IE6.
About this document
The "spacer div" hack has been discussed in many places, including this article on A List Apart.
My CSS2 implementation of clear-after is based on a suggestion by Lydia Lalopolis on the www-style mailing list. My version corrects the problem in Lydia's suggestion pointed out by David Baron.
An article on Mezzoblue discusses this problem and its solutions. Mr. Brownstone's comment points out that adding height: 100%; leads to correct results in IE6.
Please send any comments to mbrubeck@hmc.edu.

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

View File

@@ -0,0 +1,157 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-05-15T13:55:43+08:00
====== containing float ======
Created Sunday 15 May 2011
http://www.complexspiral.com/publications/containing-floats/#fn1
As powerful and useful as they are, floats can make for tricky layout tools. Chances are that you may have seen something like the situation shown in Figure 1, which is accomplished with just two div elements, each with a floated image inside it.
{{./fig1.gif}}
Figure 1. That's not right!
This is probably not what the author had in mind, but given the styles used, it's the correct layout. Here's how we created it:
div.item {border: 1px solid; padding: 5px;}
div.item img {float: left; margin: 5px;}
That's all it takes. The result seen in Figure 1 happens because __the div elements don't "stretch" to contain the floated images within them__. To look at the situation from the reverse angle, it happens because the floated images "stick out" of the bottom of the div elements.
**PS:**这是因为float的元素成为块级元素且脱离了原来的文档流。
This is not a bug. It's also not a flaw in CSS. It is, in fact, __the behavior that most authors will want most of the time__. It's just not what they would want in the example shown in Figure 1.
===== Understanding the Problem =====
So when in the name of all that's good and right would authors want floats to stick out of their containing elements? Simple: in what is historically the most common case for float use. Consider Figure 2, and the basic markup structure that produced it.
{{./fig2.gif?width=331}}
Figure 2. Floating an image, flowing the text.
<p>
...text...
__<img__ src="jul31-03-sm.jpg" height="200" border="0" class="picture">
...text...
__</p>__
<p>
...text...
</p>
**PS**图片脱离了所在段落的文档流且float到__原来所在行__(因为开始img是一个行内元素)的父元素的左侧图片被float后成为一个块级元素且其__margin-top与所在行平行__。
1.行内元素inline-element的横向框参数起作用(宽度等于字符内容的总宽度而不像块元素没指定宽度时占满所在行的全部宽度)但纵向框参数如padding, border, margin没有起到应有的像块级元素一样的隔离和突出作用而是溢出所在的行并有可能覆盖前后行的内容。
{{./10-inline-elem.png?width=500}}
{{./1-inline-block.png?width=500}}
{{./11-inline-elem.png?width=500}}
2.float行内元素后该元素float到父元素的左侧成为一个块元素**该块元素的margin-top与所在行平行**。
{{./3-floated-element.png?width=500}}
当一行容纳不下float对象时他将下移多行直到可以容纳下为止如
{{./8-before-float-inline-elem.png?width=500}}
PS本来将float的元素从第二行开始但由于容纳不下故下移一行从第三行开始
{{./9-after-float-inline-elem.png?width=500}}
{{./12-float.png?width=500}}
3.被float的元素将脱离原来的文本流。该元素前面的块级元素不受影响但该元素后面的块级元素或所在行的其他字符将受影响。如下图所示包含float段落的上一段落没受影响但所在的段落文字将环绕float元素。
{{./5-float-element-.png?width=500}}
注意float元素位于文本流上方会受到文本流中背景的影响。
{{./7-float-background.png?width=500}}
The practice of flowing text around an image goes back a long, long time. That's why the ability was added to the Web starting with Netscape 1.1, and why CSS makes it possible using the property float.[1] But look closely at Figure 2. __The floated image is sticking out of its containing element.__ We can see this more clearly by adding borders to the paragraphs, as shown in Figure 3.
{{./fig3.gif?width=352}}
Figure 3. Adding borders to the paragraphs.
So now we can see why it's important that __floats stick out of their containing elements__. If they didn't, then Figure 2 would have looked like Figure 4 instead.
{{./fig4.gif}}
Figure 4. If floats expanded their ancestor elements.
That's something __designers would never have accepted.__ So, in order to keep with Web design tradition and author expectation, CSS is written to allow floated elements to stick out of the bottom of their containing elements. While this is necessary for normal text flow, i__t's a major problem when floats are used for layout purposes__.
===== A Clear Solution =====
__If floats are to be used in creating non-table layouts, then there needs to be a way to make their containing elements stretch around them. __At present, this requires __a bit of a structural hack__. Since we want the bottom of the containing element to be placed clear past the bottom of the float, then clear is our answer. __We need only insert a block-level element just before the end of the container, and clear it.__ Consider:
<div class="item">
<img src="w6144.gif">
Widget 6144
<br>$39.95
__ <hr>__
</div>
<div class="item">
<img src="w6145.gif">
Widget 6145
<br>$44.95
__<hr>__
</div>
Now we apply the following rules to the preceding markup, and get the result shown in Figure 5.
div.item hr {**display: block; clear: left;** margin: -0.66em 0; **visibility: hidden**;}
{{./fig5.gif}}
Figure 5. Using a horiztonal rule to force expansion.
By ensuring that __the hr elements are block-level__, part of the normal flow, and cleared, we force the divs to "stretch around" the left-floated images.
**PS**因为inline-level 元素的纵向框参数不起突出和隔离作用而是溢出所在的行block-level的纵向margin总是起作用。
The __negative top and bottom margins __have the general effect of closing up the space that the hr occupies. However, this effect is not precise, and not necessarily identical across browsers. The semi-mysterious nature of horizontal rules makes it difficult to predict exactly what will happen. The effective height of the hr might be zero, or a small positive amount, or even a negative height.
Therefore, in situations where a precise clearing effect is needed, authors can __use a div instead of an hr to create a clearing effect__. For example:
div.clearer {clear: left; **line-height: 0; height: 0;**}
<div class="item">
<img src="w6144.gif">
Widget 6144
<br>$39.95
** <div class="clearer">&nbsp;</div>**
</div>
PSheight是框里内容的高度使用div块元素是因为其框参数如padding,border,margin都为0。下图中蓝色为内容所占的height宽度为块元素的特性占满一行。
{{./div-margin-0.png?width=700}}
将height设为0时会使内容溢出但默认的overflow=visible会使溢出区域显示。
{{./div-height=0.png?width=800}}
===== Set a Float to Fix a Float =====
There is a way to avoid over-use of the structural hacks discussed so far, although they are still necessary at times. In most browsers, and as defined in CSS2.1, __a floated element will expand to contain any floated elements that descend from it. __So in our widget example, we could remove all of the "clearer" elements and instead write these styles:
div.item {**float: left;** border: 1px solid; padding: 5px; **width: 60%**;}
div.item img {float: left; margin: 5px;}
Note that we've floated both the images and the "item" divs. By setting the width of the divs to be greater than 50%, we ensure that they will **never be next to each other**, but will instead stack up vertically. This has the result shown in Figure 6.
{{./fig6.gif}}
Figure 6. Using floats to stretch around floats.
This is obviously simpler to manage, both in terms of markup and style. However, the hacks discussed thus far are still useful. Suppose you want to put some advisory text underneath the items. In order to keep the text from flowing to the right of the items(因为上面的两个div都是浮动的接着第二个div的元素有可能会浮动到它的右边), we need to__ insert a clearing hack__. This would lead us to create markup like the following, which is illustrated in Figure 7.
<div class="item">
<img src="w6144.gif">
Widget 6144
<br>$39.95
</div>
<div class="item">
<img src="w6145.gif">
Widget 6145
<br>$44.95
</div>
**<div class="clearer">&nbsp;</div>**
<p>Widgets are sold on an "as is" basis without
warranty or guarantee.</p>
div.clearer {clear: left; **line-height: 0; height: 0;**}
{{./fig7.gif}}
Figure 7. Floats and hacks get the desired layout.
The clearing div effectively **pushes the normal flow downward**, forcing any following content to flow after the cleared element, and therefore after the floated divs.
The potential drawback to using floats to contain floats is that you rely on browsers to consistently interpret the layout of multiple nested floated elements. The situation becomes more fragile if these floats are part of a more complicated layout, one possibly using floats, positioning, or tables. This is not to say such layouts are impossible to achieve. They may, however, involve a good deal of trial and error to avoid obscure floating and other layout bugs that may lurk inside rendering engines.
===== Summary =====
By understanding how floats and the normal flow relate to each other, and understanding how clearing can be used to manipulate the normal flow around floats, authors can employ floats as a very powerful layout tool. Because floats were not originally intended to be used for layout, some hacks may be necessary to make them behave as intended. **This can involve floating elements that contain floats, "clearing" elements, or a combination of both.**
Looking to the future, there have been a variety of proposed enhancements to CSS that would allow an author to declare that an element should stretch to contain any floated elements within itself. These would obviously be welcome additions to CSS, but as of this writing, support for such abilities is likely to be a long time coming.
[1] The term "float" refers to the way in which an element floats to one side and down, as described in the original "Additions to HTML 2.0" document that accompanied the release of Netscape 1.1.

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB