当前位置:网站首页>盒子模型中过度约束问题及其解决办法
盒子模型中过度约束问题及其解决办法
2022-08-05 05:25:00 【Y-琼眸】
盒子模型就是用来布局的,之前已经讲了盒子模型的基本内容,接下来我们就来看看用盒子模型进行水平布局时遇到的一个问题——过度约束问题。
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>首先我们先建立两个盒子box1 box2,然后我们给这两个盒子设置样式,如下图
<style>
.box1{
width: 600px;
height: 200px;
border: 10px red solid;
}
.box2{
width: 100px;
height: 100px;
background-color: aqua;
padding: 0 20px;
margin: 0 10px;
}
</style>这样我们就得到了两个不一样的盒子。
我们来先看看能改变box2的水平方向大小位置的一些属性
有下列七个属性:margin-left、border-left、padding-left、width、padding-right、border-right、margin-right
浏览器规定,水平方向七个值相加必须等于其父元素内容区的宽度,如果不等于,浏览器会自动调整七个值中的一个值让其成立,这就是过度约束。
这个时候我们看看上面那串代码,七个值的和为20+10+10+100+10+10+20=180,这并不等于我们的父元素600,所以浏览器就会自动调整一个值让它们成立。
那么下来我们就来看看浏览器是如何调整的。
1.七个值中如果没有auto,那么浏览器默认调整margin-right
2.七个值中如果有auto,那么浏览器就调整auto
可以设置auto的属性有margin-right width margin-left
(1)一个auto,谁是auto就调整谁
(2)两个auto,只要width为auto就调整width
margin-right width为auto margin-left固定值 浏览器调整width
margin-right margin-left为auto width固定值 浏览器同时调整margin-left 和margin-right,距离平分,盒子居中显示
margin-left width为auto margin-right固定值 浏览器调整width
(3)三个auto,调整width
总的来说,浏览器要调整,肯定优先调整width。
边栏推荐
猜你喜欢

自营商城提高用户留存小技巧,商城对接小游戏分享

花花省V5淘宝客APP源码无加密社交电商自营商城系统带抖音接口

农场游戏果园系统+牧场养殖系统+广告联盟模式流量主游戏小程序APP V1

初识网页与浏览器

docker部署完mysql无法连接

System basics - study notes (some command records)

Introduction to Network Layer Protocols

Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station

Late night drinking, 50 classic SQL questions, really fragrant~

Transformer详细解读与预测实例记录
随机推荐
云计算基础-学习笔记
单片机期末复习大题
js判断文字是否超过区域
sql server duplicate values are counted after
Network Protocol Fundamentals - Study Notes
D39_Vector
reduce()方法的学习和整理
cs231n学习记录
txt文件英语单词词频统计
[issue resolved] - jenkins pipeline checkout timeout
Cloud Computing Basics - Study Notes
VS Code私有服务器部署(私有化)
LeetCode practice and self-comprehension record (1)
Some basic method records of commonly used languages in LeetCode
config.js related configuration summary
获取预训练模型的网络输入尺寸
Browser Storage WebStorage
Shadowless Cloud Desktop
UI刘海屏适配方式
邮件管理 过滤邮件