CSS中BFC(还有margin-top和top)

本文介绍了CSS中的布局方式,包括标准流、浮动布局、定位布局和弹性布局。重点讨论了BFC(Block Formatting Context)的概念,它是CSS布局中的一种重要机制,用于理解元素定位和外边距重叠。BFC可以通过特定条件触发,如根元素、浮动、overflow值等。同时,BFC在解决浮动元素高度塌陷和防止元素被覆盖等问题上起到关键作用。
摘要由CSDN通过智能技术生成

1.前置知识介绍

HTML中常用布局方式:
1.标准流/普通流
2.浮动布局
3.定位布局
4.弹性布局

块级元素:div、H1-H5、p、table、ol/ul/li

内联元素(可置换/不可置换):a、span、img、input

margin-top和top附注:

top/right/bottom/left和margin-top/margin-right/margin-bottom/margin-left都可以用在relative/absolute中,但是relative一般用margin-top/margin-right/margin-bottom/margin-left,absolute一般用top/right/bottom/left(absolute中使用margin-top是一种特殊的“相对定位”),两者的区别,可以通过查看盒模型知道。

还有一条:
1.使用top、bottom、left、right的前提是本元素position被定义为relative、absolute和fixed中的一种,不能是static
2.使用margin-top、margin-bottom、margin-left、margin-right的元素的position可以是static、relative、absolute和fixed
3.relative下right和bottom是从right/bootom方向偏移,比如

position: relative;
background: blue;
width: 100px;
height: 100px;
right: 50px;
bottom: 50px;

是从right/bottom方向偏移50px࿰

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ul.head{ border-style: groove; position: fixed; top:0; width:99%; list-style-type: none; margin-top:0; padding-left: 0px; /*创建一个新的BFC*/ overflow: hidden; background:indianred; border-radius:8px;/*圆角边框*/ font-size:18px; } li a{ /* !*实现横向导航栏, display:inline;也可,如果你想链接到具有相同的大小,你必须使用浮动的方法*!*/ float:left; height:20px; text-align: center; color: white; background-color: darksalmon; padding: 12px 20px; text-decoration: none; } li a:hover{ background-color: coral; color: black; } body{ background-attachment: fixed; background-image: url(../resources/image/blue_pink.jpg); background-size:100%; } iframe{ height:300px; width:400px; float:right; } select{ font-size: 20px; color:olive; background-color:cornsilk; } div{ margin-top:60px; } input{font-size: 16px;} button { border-radius:5px;/*圆角边框*/ font-size: 18px; background-color: #4CAF50; color: white; cursor: pointer; transition: background-color 0.3s ease; } #pauseBtn{ position:absolute; bottom:17%; left:40%; } #startBtn{ position:absolute; bottom:59%; left:15%; } #increase-btn1 { position:absolute; left: 17%; bottom:9%; } #decrease-btn1 { position:absolute; left: 19%; bottom:9%; } #increase-btn2 { position:absolute; right: 19%; bottom:25%; } #decrease-btn2 { position:absolute; right: 17%; bottom:25%; } #speedRange{ position:absolute; bottom:17%; left:50%; } button:hover { background-color: #3e8e41; } #introduction{ margin-top: 20px; width:300px; border-style: groove; font-size: 14px; color:darkred; overflow: auto; min-height:300px; max-height:300px; } footer{ position:absolute; bottom:0; width:100%; height:30px; text-align: center; } #canvas{ /*width: 880px;*/ /*height: 440px;*/ width: 100%; height: 100%; /*margin: -210px auto;*/ /*border: 2px solid yellowgreen;*/ /*display: block;*/ overflow: scroll; } .container { height: 440px; width: 880px; margin: -225px auto; border: 2px solid yellowgreen; /*display: block;*/ box-sizing:border-box; overflow: scroll; } #log{ border:2px palegoldenrod; position:absolute; top:9%; right:0.5%; background-color:lightyellow; height: 430px; /* 设置固定高度 */ width: 310px; /* 设置固定宽度 */ font-size: 16px; /* 设置默认字体大小 */ resize: none; /* 其他样式属性 */ } .red-text { color: red; }
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值