小白教程
所有教程
关于
Search
172.69.6.102
172.69.6.102
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“CSS Float(浮动)”的源代码
本页内容
上一节:
CSS_Overflow
下一节:
CSS_布局_–_水平_&_垂直对齐
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:CSS Float(浮动)}}[[Category:CSS 教程|22]] = CSS Float(浮动) = == 什么是 CSS Float(浮动)? == CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列。 Float(浮动),往往是用于图像,但它在布局时一样非常有用。 == 元素怎样浮动 == 元素的水平方向浮动,意味着元素只能左右移动而不能上下移动。 一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。 浮动元素之后的元素将围绕它。 浮动元素之前的元素将不会受到影响。 如果图像是右浮动,下面的文本流将环绕在它左边: <sample title="" desc="" lang="html" hererun="1"> img { float:right; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> img { float:right; } </style> </head> <body> <p>在下面的段落中,我们添加了一个 <b>float:right</b> 的图片。导致图片将会浮动在段落的右边。</p> <p> <img src="logocss.gif" width="95" height="84" /> 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 </p> </body> </html> </run> == 彼此相邻的浮动元素 == 如果你把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。 在这里,我们对图片廊使用 float 属性: <sample title="" desc="" lang="html" hererun="1"> .thumbnail { float:left; width:110px; height:90px; margin:5px; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> .thumbnail { float:left; width:110px; height:90px; margin:5px; } </style> </head> <body> <h3>图片库</h3> <p>试着调整窗口,看看当图片没有足够的空间会发生什么。</p> <img class="thumbnail" src="/images/klematis_small.jpg" width="107" height="90"> <img class="thumbnail" src="/images/klematis2_small.jpg" width="107" height="80"> <img class="thumbnail" src="/images/klematis3_small.jpg" width="116" height="90"> <img class="thumbnail" src="/images/klematis4_small.jpg" width="120" height="90"> <img class="thumbnail" src="/images/klematis_small.jpg" width="107" height="90"> <img class="thumbnail" src="/images/klematis2_small.jpg" width="107" height="80"> <img class="thumbnail" src="/images/klematis3_small.jpg" width="116" height="90"> <img class="thumbnail" src="/images/klematis4_small.jpg" width="120" height="90"> </body> </html> </run> == 清除浮动 - 使用 clear == 元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用 clear 属性。 clear 属性指定元素两侧不能出现浮动元素。 使用 clear 属性往文本中添加图片廊: <sample title="" desc="" lang="html" hererun="1"> .text_line { clear:both; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> .thumbnail { float:left; width:110px; height:90px; margin:5px; } .text_line { clear:both; margin-bottom:2px; } </style> </head> <body> <h3>图片库</h3> <p>试着调整窗口,看看当图片没有足够的空间会发生什么。.</p> <img class="thumbnail" src="/images/klematis_small.jpg" width="107" height="90"> <img class="thumbnail" src="/images/klematis2_small.jpg" width="107" height="80"> <img class="thumbnail" src="/images/klematis3_small.jpg" width="116" height="90"> <img class="thumbnail" src="/images/klematis4_small.jpg" width="120" height="90"> <h3 class="text_line">第二行</h3> <img class="thumbnail" src="/images/klematis_small.jpg" width="107" height="90"> <img class="thumbnail" src="/images/klematis2_small.jpg" width="107" height="80"> <img class="thumbnail" src="/images/klematis3_small.jpg" width="116" height="90"> <img class="thumbnail" src="/images/klematis4_small.jpg" width="120" height="90"> </body> </html> </run> == 更多实例 == [[try.php?filename=trycss_float2|为图像添加边框和边距并浮动到段落的右侧]] 让我们为图像添加边框和边距并浮动到段落的右侧 [[try.php?filename=trycss_float3|标题和图片向右侧浮动]] 让标题和图片向右侧浮动。 [[try.php?filename=trycss_float4|让段落的第一个字母浮动到左侧]] 改变样式,让段落的第一个字母浮动到左侧。 [[try.php?filename=trycss_float6|创建一个没有表格的网页]] 使用 float 创建一个网页页眉、页脚、左边的内容和主要内容。 == CSS 中所有的浮动属性 == "CSS" 列中的数字表示不同的 CSS 版本(CSS1 或 CSS2)定义了该属性。 {| class="table table-striped table-hover" ! 属性 ! 描述 ! 值 ! CSS |- | [[pr class clear|clear]] | 指定不允许元素周围有浮动元素。 | left right both none inherit | 1 |- | [[pr class float|float]] | 指定一个盒子(元素)是否可以浮动。 | left right none inherit | 1 |}
返回至“
CSS Float(浮动)
”。
上一节:
CSS_Overflow
下一节:
CSS_布局_–_水平_&_垂直对齐