
批改狀態(tài):合格
老師批語(yǔ):
padding內(nèi)邊距
border邊框
將背景色裁切到內(nèi)容區(qū),讓padding可視化
background-clip:content-box
盒子尺寸多大小的計(jì)算公式:width/height + padding2 +border2
一般減去padding和border的值就是盒子內(nèi)容 calc();ie盒模型
box-sizing:讓用戶(hù)自己決定計(jì)算盒子大小的方案
.box{
box-sizing:content-box;標(biāo)準(zhǔn)盒模型 不計(jì)算border,padding
box-sizing:border-box;ie盒模型 padding,border計(jì)算在盒子大小內(nèi)
}
<style>
.box {
width: 200px;
height: 200px;
background-color: cyan;
padding: 5px 10px 15px 20px;
background-clip: content-box;
border: 1px solid #ddd;
box-sizing: border-box;
}
</style>
<body>
<div class="box"></div>
</body>
效果圖
margin:上 右 下 左
margin塌陷 大者勝出
margin只會(huì)對(duì)頁(yè)面中的元素的位置或者多個(gè)元素的排列產(chǎn)生影響,對(duì)盒子大小無(wú)影響
無(wú)論是行內(nèi)元素還是行內(nèi)塊元素 使用text-align: center都可以實(shí)現(xiàn)居中;
<style>
.box {
width: 15em;
height: 15em;
border: 1px solid #ccc;
}
.box {
text-align: center;
}
</style>
<body>
<div class="box">
<a href="">這是內(nèi)容</a>
</div>
</body>
效果圖
設(shè)置一個(gè)行高等于容器的高度即可,
<style>
.box {
width: 15em;
height: 15em;
border: 1px solid #ccc;
}
.box {
text-align: center;
line-height: 15em;
}
</style>
<body>
<div class="box">
<a href="">這是內(nèi)容</a>
</div>
</body>
效果圖
使用maigin:0 auto;意味上下間距為0,左右間距自動(dòng)
<style>
.box {
width: 15em;
height: 15em;
border: 1px solid #ccc;
}
.box>div {
width: 5em;
height: 5em;
background-color: yellow;
margin: 0 auto;
}
</style>
<body>
<div class="box">
<div></div>
</div>
</body>
效果圖
1、padding方法:父容器不要設(shè)置高度,由子元素?fù)伍_(kāi),給父元素設(shè)置上下相等的內(nèi)邊距即可實(shí)現(xiàn)垂直居中
<style>
.box {
width: 15em;
border: 1px solid #ccc;
padding: 5em 0;
box-sizing: border-box;
}
.box>div {
width: 5em;
height: 5em;
background-color: yellow;
margin: 0 auto;
}
</style>
<body>
<div class="box">
<div></div>
</div>
</body>
效果圖
2、margin方法 使用定位
<style>
.box {
position: relative;
width: 15em;
border: 1px solid #ccc;
padding: 5em 0;
box-sizing: border-box;
}
.box>div {
width: 5em;
height: 5em;
background-color: yellow;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
<body>
<div class="box">
<div></div>
</div>
</body>
效果圖
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)