干货1——背景的复合属性
<style>
/*
复合写法:
1.用空格隔开
2.顺序可以换
3.可以只取一个值,放在后面能覆盖前边的值
4.background-size属性只能单独用
5.位置诸如center top要挨着
*/
div{
width:600px;
height: 600px;
background: yellow url(008/32.jpg) no-repeat fixed;
}
</style>
干货2——浮动属性
属性一览表(图源自网络):
浮动的情况一览(图源自网络):
举例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
}
.red{
background: red;
}
.blue{
background: blue;
}
.yellow{
background: yellow;
}
</style>
</head>
<body>
<div class="red"></div>
<div class="blue"></div>
<div class="yellow"></div>
</body>
</html>
没加float时效果如下:
在red类中加上左浮动
.red{
background: red;
float:left;
}
效果如下:
若在red类上加上向右浮动,效果如下:
若将三个类都加上左浮动/右浮动(右浮动和左浮动效果不一样,但原理差不多)
.red{
background: red;
float:left;
}
.blue{
background: blue;
float: left;
}
.yellow{
background: yellow;
float: left;
}
效果如下:
若对三个类大小设置如下:
.red{
background: red;
float:left;
width: 1000px;
height: 1000px;
}
.blue{
background: blue;
float: left;
width: 600px;
height: 600px;
}
.yellow{
background: yellow;
float: left;
width: 300px;
height: 300px;
}
效果如下(图太大了这是截取的一部分):
若对三个类大小设置如下:
.red{
background: red;
float:left;
width: 1000px;
height: 1000px;
}
.blue{
background: blue;
float: left;
width: 400px;
height: 400px;
}
.yellow{
background: yellow;
float: left;
width: 300px;
height: 300px;
}
效果如下(图太大,未截全):
若在第二类控制的div中加上文字:
<div class="red"></div>
<div class="blue">大家好</div>
<div class="yellow"></div>
效果如下:
- 清浮动
法一,直接清:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1, .box2{
width: 200px;
height: 200px;
float: left;
}
.box1{
background-color: yellow;
}
.box2{
background-color: blue;
}
.box{
width: 300px;
height: 300px;
background-color: red;
clear:left;
/* left代表禁止左浮动,若为right则代表禁止右浮动
若为both代表禁止左右浮动,若为none代表允许浮动
*/
}
</style>
</head>
<body>
<div>
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box"></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>Document</title>
<style>
.box1, .box2{
width: 200px;
height: 200px;
float: left;
}
.box1{
background-color: yellow;
}
.box2{
background-color: blue;
}
.box{
width: 300px;
height: 300px;
background-color: red;
}
.container{
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
<div class="box"></div>
</body>
</html>
法三,当前浮动元素后面补一个盒子,不设置宽高,clear:both:
<div>
<div class="box1"></div>
<div class="box2"></div>
<div style="clear: both;"></div>
</div>
法四,overflow:hidden,用bfc让浮动元素计算高度:
.container{
overflow:hidden;
}
干货3——盒子模型
形象的盒子(图源网络):
- 内边距
举例与重点如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 500px;
height: 300px;
background-color: red;
text-align: justify;
/* 使左右与边框紧密贴合 */
/*
内边距:
1个值,4个方向一样
2个值,上下,左右
3个值,上,左右,下
4个值,上,右,下,左
*/
}
</style>
</head>
<body>
<div>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Voluptatum recusandae suscipit exercitationem ullam officia, ratione,
autem nostrum veniam rerum dolore in mollitia cum aspernatur
consequuntur nihil, sint asperiores. Aliquam, repellendus.
</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>Document</title>
<style>
/*
1.背景色可以蔓延到内边距
2.可以设置单一方向边距
padding-方向:top bottom left right
*/
div{
width: 300px;
height: 300px;
/* padding: 30px 0 0 0; */
padding-top: 30px;
background: yellow;
}
ul{
padding-left: 0;
/* 不可以是负数喔 */
}
</style>
</head>
<body>
<div>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quaerat et nesciunt soluta perspiciatis alias culpa temporibus,
facilis voluptate sint nam, ullam quos accusamus vel
ducimus vitae! Velit illum laborum quod.
</div>
</body>
</html>
效果如下:
或者用通配符选择器:
*{
padding: 0;
}
- 边框
举例与重点如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* eg:border:1px solid gray */
.box1{
width: 100px;
height: 100px;
background: blue;
border: 10px dotted red;
/* 样式:solid(实线) double(双实线) dashed(虚线) dotted(圆点) */
/* 背景色也能蔓延到边框 */
}
.box2{
width: 100px;
height: 100px;
background: blue;
border-top: 5px solid red;
border-left: 10px dashed yellow;
}
.box3{
width: 100px;
height: 100px;
background: green;
border-width: 10px 20px 30px 40px;
border-color: yellow blue black rebeccapurple;
border-style: solid;
/*border
border-width
border-style
border-color
*/
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></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>Document</title>
<style>
div{
width: 200px;
height: 200px;
}
.box1{
background:red;
border: 1px solid yellow;
margin-left: 10px;
}
.box2{
background: green;
border: 1px solid blue;
}
.box3{
background: blueviolet;
margin: 0 auto;
/* auto auto竖向并不会居中
0可以换成别的数值的px效果会有不同
*/
}
/*
1.margin-方向:四个方向
2.背景色没有蔓延
3.*{margin:0}
4.外边距可以有负值
5.横向居中: margin:0 auto;
*/
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></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>Document</title>
<style>
.box1, .box2{
width: 200px;
height: 200px;
}
.box1{
background: red;
margin-bottom: 100px;
}
.box2{
background: blue;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
效果如下:
此时若给另一个盒子也加上外边距:
.box1{
background: red;
margin-bottom: 100px;
}
.box2{
background: blue;
margin-top: 50px;
}
效果不会发生任何改变。
可见:
当两个盒子为兄弟关系时:
垂直方向:两个盒子的间距取两盒子中外边距较大的值。
水平方向:两个盒子的间距是两盒子外边距之和。
当给“子”盒子加外边距时:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
background: yellow;
}
.box2{
width: 200px;
height: 200px;
background: red;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
效果如下:
可以发现“子”盒子的外边距居然没起作用,而是让“父”盒子外边距增大了。
那么如何能让红色盒子在黄色盒子中下移100px呢?
方法有四,法一:
把子margin-top==>父的padding-top,再调整父的高度height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 500px;
height: 400px;
background: yellow;
padding-top: 100px;
}
.box2{
width: 200px;
height: 200px;
background: red;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
得到了预期效果:
法二:给父盒子设置边框,子盒子设置margin.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
background: yellow;
border: 1px solid transparent;
}
.box2{
width: 200px;
height: 200px;
background: red;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></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>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
background: yellow;
}
.box2{
width: 200px;
height: 200px;
background: red;
margin-top: 100px;
float: left;
/* 加在上边也行 */
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
也可得到预期效果。
法四:父盒子加overflow:hidden(BFC相关),子盒子写margin
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 500px;
height: 500px;
background: yellow;
overflow:hidden;
}
.box2{
width: 200px;
height: 200px;
background: red;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
以上内容是筱筱根据b站视频总结而成,学习前端的第六天,加油٩(๑òωó๑)۶