html float属性6,CSS float属性

浮动在CSS中一直是一个非常重要的话题。

它被用在很多有创造性的用法中,因为它是为数不多的几种方法之一,和表格一样,我们可以真正实现一些布局。例如,在过去,我们常常将侧边栏浮动到左边,以便在屏幕的左侧显示它,并为主要内容添加一些边距。

幸运的是,时代变了,今天我们有Flexbox和Grid来帮助我们进行布局,而

float属性支持3种值:

left

right

none(默认)

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

这里有一些代码:

This is some random paragraph and an image. 39cbcf5e679cbc032a1fe3cac6d9478b.png 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;

}

显示的效果如下:

9876214f73fbd4750c740839f21401b5.png

如您所见,默认情况下,正常的流将以内联方式考虑图像,并在行中为其留出空间。

如果我们给图像添加float: left和一些padding:

img {

float: left;

padding: 20px 20px 0px 0px;

}

结果如下:

9876214f73fbd4750c740839f21401b5.png

下面是通过float:right,调整padding,得到的结果如下:

img {

float: right;

padding: 20px 0px 20px 20px;

}

9876214f73fbd4750c740839f21401b5.png

浮动元素从页面的正常流中移除,其他内容围绕它流动。

您也不局限于浮动图像,这里我们用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

}

结果如下:

9876214f73fbd4750c740839f21401b5.png

原创文章,作者:犀牛前端部落,如若转载,请注明出处:https://www.pipipi.net/4575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值