
批改狀態(tài):合格
老師批語:總得來說,寫的不錯(cuò),建議重要代碼加上效果圖!
序號(hào) | 代碼 | 定義 |
---|---|---|
1. | margin |
外邊距 |
2. | padding |
內(nèi)邊距 |
3. | border |
邊框 |
4. | top |
內(nèi)外邊距邊框?qū)傩?,表示上邊?/td> |
5. | left |
內(nèi)外邊距邊框?qū)傩?,表示左邊?/td> |
6. | bottom |
內(nèi)外邊距邊框?qū)傩?,表示下邊?/td> |
7. | right |
內(nèi)外邊距邊框?qū)傩?,表示表示右邊?/td> |
8. | box-sizing |
重新計(jì)算邊框大小 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>盒/框模型</title>
<style>
.box{
/* 寬、高是內(nèi)容區(qū) */
width:200px;
height:200px;
}
.box.one{
padding:10px;
border:2px solid #000;
background-color:lightgreen;
background-clip:content-box;
margin-bottom:20px;
}
.box.two{
padding:10px;
border:2px solid #000;
background-color:lightcoral;
/* background-clip規(guī)定背景的繪制區(qū)域 */
background-clip:content-box;
margin-top:30px;
}
.box.parent{
background-color:lightblue;
/* 相對(duì)定位是相對(duì)自己原來的位置進(jìn)行移動(dòng),這個(gè)元素在文檔流的原位置不釋放 */
position:relative;
left:30px;
top:50px;
}
.son{
width:100px;
height:100px;
background-color:violet;
/* 絕對(duì)定位會(huì)找到這個(gè)元素有定位的上級(jí),按照上級(jí)的位置進(jìn)行移動(dòng) */
position:absolute;
/* 固定定位,忽略所有的定位上級(jí),只針對(duì)body進(jìn)行定位移動(dòng) */
position:fixed;
left:60px;
top:80px;
}
</style>
</head>
<body>
<!-- 兩個(gè)盒子之間外邊距會(huì)折疊,會(huì)從兩個(gè)盒子的border邊開始計(jì)算距離 -->
<div class="box one"></div>
<div class="box two"></div>
<hr />
<div class="box parent">
<div class="son"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定義元素大小的計(jì)算方式</title>
<style>
/* 樣式初始化,頁面布局常見,會(huì)經(jīng)常用到 */
*{
margin:0;
padding:0px;
box-sizing:border-box;
}
.box{
background-color:violet;
width:200px;
height:200px;
border:3px solid #000;
padding:10px;
background-clip:content-box;
/* box-sizing重新計(jì)算邊框大小 */
/* content-box是默認(rèn)值,意思是以內(nèi)容為準(zhǔn)重新計(jì)算邊框大小 */
box-sizing:content-box;
/* box-sizing:border-box; */
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
因?yàn)轱@示器都是有寬度的,很容易讓元素水平居中。但是網(wǎng)頁高度可以往下滑動(dòng),不容易垂直居中。
我們要將元素進(jìn)行水平垂直居中,就需要定位盒子的4個(gè)角,讓元素以4個(gè)角進(jìn)行定位居中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>margin:auto塊元素的垂直居中</title>
<style>
.content{
width:500px;
height:600px;
background-color:lightblue;
position:relative;
}
.item{
width:100px;
height:100px;
background-color:red;
position:absolute;
/* 設(shè)置水平垂直都居中 從上邊順時(shí)針到右邊結(jié)束*/
top:0;
left:0;
bottom:0;
right:0px;
margin:auto;
}
</style>
</head>
<body>
<div class="content">
<div class="item"></div>
</div>
</body>
</html>
微信掃碼
關(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)