博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css float属性_CSS float属性和清除
阅读量:2504 次
发布时间:2019-05-11

本文共 4120 字,大约阅读时间需要 13 分钟。

css float属性

Floating has been a very important topic in the past.

过去,浮动是一个非常重要的话题。

It was used in lots of hacks and creative usages because it was one of the few ways, along with tables, we could really implement some layouts. In the past we used to float the sidebar to the left, for example, to show it on the left side of the screen and added some margin to the main content.

它被用于许多技巧和创意用途,因为它是表格和我们可以真正实现某些布局的少数几种方法之一。 过去,我们曾经将边栏浮动到左侧,例如,将其显示在屏幕的左侧,并在主要内容上添加了一些边距。

Luckily times have changed and today we have Flexbox and Grid to help us with layout, and float has gone back to its original scope: placing content on one side of the container element, and make its siblings show up around it.

幸运的是,时代已经改变,如今我们有了Flexbox和Grid来帮助我们进行布局,而float已回到其原始范围:将内容放置在容器元素的一侧,并使它的同级元素显示在容器元素周围。

The float property supports 3 values:

float属性支持3个值:

  • left

    left

  • right

    right

  • none (the default)

    none (默认)

Say we have a box which contains a paragraph with some text, and the paragraph also contains an image.

假设我们有一个框,其中包含带有一些文本的段落,并且该段落还包含图像。

Here’s some code:

这是一些代码:

This is some random paragraph and an image. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text.

.parent {  background-color: #af47ff;  padding: 30px;  width: 500px;}.child {  background-color: #ff4797;  padding: 30px;}.box {  background-color: #f3ff47;  padding: 30px;  border: 2px solid #333;  border-style: dotted;  font-family: courier;  text-align: justify;  font-size: 1rem;}

and the visual appearance:

和视觉外观:

As you can see, the normal flow by default considers the image inline, and makes space for it in the line itself.

如您所见,默认情况下,正常流程会考虑图像内联,并在行本身中为其留出空间。

If we add float: left to the image, and some padding:

如果我们在图片上添加float: left并添加一些填充:

img {  float: left;  padding: 20px 20px 0px 0px;}

this is the result:

结果是:

and this is what we get by applying a float: right, adjusting the padding accordingly:

这就是我们通过应用float来得到的:正确,相应地调整填充:

img {  float: right;  padding: 20px 0px 20px 20px;}

A floated element is removed from the normal flow of the page, and the other content flows around it.

从页面的常规流中删除浮动元素,其他内容在页面周围流动。

You are not limited to floating images, too. Here we switch the image with a span element:

您也不仅限于浮动图像。 在这里,我们使用span元素切换图像:

This is some random paragraph and an image. Some text to float The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text.

span {  float: right;  margin: 20px 0px 20px 20px;  padding: 10px;  border: 1px solid black}

and this is the result:

结果如下:

清算 (Clearing)

What happens when you float more than one element?

当您浮动多个元素时会发生什么?

If when floated they find another floated image, by default they are stacked up one next to the other, horizontally. Until there is no room, and they will start being stacked on a new line.

如果在浮动时找到另一个浮动图像,则默认情况下它们会水平堆叠在一起。 直到没有空间,它们都将开始堆叠在新的行上。

Say we had 3 inline images inside a p tag:

假设我们在p标签中包含3个内联图片:

If we add float: left to those images:

如果我们将float: left在这些图像上:

img {  float: left;  padding: 20px 20px 0px 0px;}

this is what we’ll have:

这是我们将拥有的:

if you add clear: left to images, those are going to be stacked vertically rather than horizontally:

如果您添加clear: left在图像上,则将垂直堆叠而不是水平堆叠:

I used the left value for clear. It allows

我用left值表示clear 。 它允许

  • left to clear left floats

    left以清除左浮动

  • right to clear right floats

    right清除右浮动

  • both to clear both left and right floats

    both可以清除左右浮动

  • none (default) disables clearing

    none (默认)禁用清除

翻译自:

css float属性

转载地址:http://soqgb.baihongyu.com/

你可能感兴趣的文章
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>