①代码例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
color: aqua;
font-size: 24px;
}
</style>
</head>
<body>
<div class="father">
父级
<div class="son">
子级
</div>
</div>
</body>
</html>
②效果图:父级的样式继承给了子级
①代码例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
color: aqua;
font-size: 24px;
height: 100px;
width: 100px;
background-color: rgb(79, 79, 238);
}
.son{
background-color: rgb(202, 72, 72);
}
</style>
</head>
<body>
<div class="father">
父级
<div class="son">
子级
</div>
</div>
</body>
</html>
②效果图:父级的大小没有继承给子级
①代码例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
color: aqua;
font-size: 24px;
height: 100px;
width: 100px;
background-color: rgb(79, 79, 238);
}
.son{
background-color: rgb(202, 72, 72);
height: inherit;
}
</style>
</head>
<body>
<div class="father">
父级
<div class="son">
子级
</div>
</div>
</body>
</html>
②效果图:父级所有样式都继承给了子级
概念:CSS假定每⼀个标签都是⼀个矩阵,围绕着这个矩阵,从内到外展开⼀系列的属性来描述它,这⼀系列就被称为【盒⼦模型】
组成:content、padding、border、margin
举例:篮球场有⼀个箱⼦📦,箱⼦⾥有⼀颗篮球🏀
篮球场⼜有⼀个箱⼦📦,箱⼦⾥也有⼀颗篮球🏀
①上下左右四个方向padding-left/right/top/bottom
a.一个值
b.两个值:1上下,2左右
c.三个值:1上,2左右,3下
d.四个值:顺时针,上右下左
②注意:系统在设置 padding时 ,默认撑⼤宽⾼,拿出对应的间距作为内间距
如果希望 padding 从宽⾼本身去减⼩的话,设置 box-sizing:border-box 属性
①三部分
a.边框样式:border-sstyle
b.边框颜色:border-color
c.边框粗细:border-width
②可单独设置
border-⽅向-style/color/width
③注意:系统在设置border-width的时候,默认先撑⼤宽⾼,再拿出对应的间距作为边框粗细如果我们希望边框粗细从宽⾼本身去减⼩的话,设置box-sizing:border-box属性
④圆角:border-radius
a.正方形圆角=边长一半时,是圆形
b.可以单独设置某角:border-top/bottom-left/right-radius
同padding一样,四个方向:margin-left/right/top/bottom
补充1:
①背景颜色会自动填充到margin以内的区域
②内容一直在content区域 ③属性box-sizing:content-box(默认,宽高只控制content区域,使用boder,padding时会改变整体大小)、border-box(宽高控制整个盒子,在设定的宽高中改变) ④盒子居中 margin: auto (使标签处于所占据⽂档流的正中央)
⑤内容居中text-align: center (使得内容处于他所占据位置的正中央)
⑥height:容器高度、line-height:行高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 500px;
height: 500px;
background-color: rgb(25, 144, 223);
padding: 3px;/* 内间距 */
box-sizing: border-box;
border: 5px solid red;/* 边框 */
border-radius: 50px;
margin: auto;/* 居中 */
text-align: center;/* 文本内容居中 */
line-height: 500px;/* 文本高度居中 */
}
</style>
</head>
<body>
<div class="box">内容</div>
</body>
</html>
补充2:盒子模型存在的问题
(1)子级设置的margin-top等等会传递给父级
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background-color: aqua;
}
.son{
width: 100px;
height: 100px;
background-color: black;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
(2)解决措施:在父级写属性padding-top: 100px;box-sizing: boder-box;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background-color: aqua;
}
.son1{
width: 100px;
height: 100px;
background-color: black;
margin-bottom: 50px;
}
.son2{
width: 200px;
height: 100px;
background-color: rgb(215, 37, 37);
margin-top: 100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son1"></div>
<div class="son2"></div>
</div>
</body>
</html>
(1)none:隐藏(不会显示出来,检查时可找到) (2)block:转成块级元素(会自动换行h1,div,p) (3)inline:转成内联元素(不会自动换行span,a)
(4)inline-block:行内块(转换成具有自己大小的且横向排列的元素,块与块之间会有间隙,会占位置)
(1)float浮动:会脱离文档流,不占位置,针对块级元素(left/right)
(2)style="display: none;"占位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1,.box2,.box3{
width: 100px;
height: 100px;
}
.box1{
background-color: rgb(25, 144, 223);
float: left;
}
.box2{
background-color: rgb(223, 25, 187);
float: left;
}
.box3{
background-color: rgb(84, 223, 25);
float: right;
}
.box4{
width: 300px;
height: 300px;
background-color: black;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
position: relative(通常占位置)
①position: absolute(通常不占位置)
父子关系(子元素进行定位,相对于其父级【设置了定位/没有定位而是找到浏览器边缘】的绝对定位)
②fiexd:窗口定位(任何浏览器放大缩小任意窗口时,数值不变)
(1)margin-top的效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 200px;
height: 200px;
background-color: aqua;
margin-top: 50px;
}
.son1 {
width: 100px;
height: 100px;
background-color: black;
}
.son2 {
width: 100px;
height: 100px;
background-color: rgb(212, 134, 24);
}
</style>
</head>
<body>
<div class="father">
<div class="son1"></div>
</div>
<div class="son2"></div>
</body>
</html>
(2)相对定位position: relative的效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 200px;
height: 200px;
background-color: aqua;
/* margin-top: 50px; */
position: relative;
top: 50px;
}
.son1 {
width: 100px;
height: 100px;
background-color: black;
}
.son2 {
width: 100px;
height: 100px;
background-color: rgb(212, 134, 24);
}
</style>
</head>
<body>
<div class="father">
<div class="son1"></div>
</div>
<div class="son2"></div>
</body>
</html>
(3)绝对定位position: absolute的效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 200px;
height: 200px;
background-color: aqua;
/* margin-top: 50px; */
position: absolute;
top: 50px;
}
.son1 {
width: 100px;
height: 100px;
background-color: black;
}
.son2 {
width: 100px;
height: 100px;
background-color: rgb(212, 134, 24);
}
</style>
</head>
<body>
<div class="father">
<div class="son1"></div>
</div>
<div class="son2"></div>
</body>
</html>
代码例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="box">
<img src="https://img0.baidu.com/it/u=4002835238,1224729013&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="">
</div>
</body>
</html>
代码效果图
实际全图
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。