盒模型:box-model
- CSS认为每个html元素都可以看作一个盒子,从而对盒子进行尺寸和方位的修改,但一般是指块元素
- 从内到外,分为内容区域,内边距,边框,外边距
- 默认尺寸是作用在内容区域上的
- 块元素振勇的总空间:内容区域+内边距+边框+外边距
示例:index.css
@charset "utf-8";
#myul{
/* 内容区域 */
width: 300px;
height: 200px;
background-color: pink;
/* 盒模型相关样式 */
/* 外边距 */
/* 上下左右 */
/* margin: 30px; */
/* margin-top: 30px;
margin-right: 50px;
margin-bottom: 20px;
margin-left: 10px; */
/* 左上右下。按照顺时针方向 */
/* margin: 20px 30px 40px 40px; */
/* 上,左右,下 */
/* margin: 20px 30px 40px; */
/* 上下,左右 */
margin: 20px 30px;
/* 内边距 */
padding: 30px;
/* 边框 */
border: 30px solid #000;
display: inline-block;
}
生成效果CSS.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 按tab键和enter键生成的不一样 -->
<link rel="stylesheet" type="text/css" href="./css/index.css"/>
</head>
<body>
<div
<ul id="myul"></ul>
</div>
</body>
</html>
背景图设置
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.outer{
width: 800px;
height: 300px;
border: 1px solid #000;
margin: 0 auto;
/* 内容溢出时处理 */
/* 移除的内容会被裁剪,裁掉的内容,不占用空间 */
/* overflow: hidden; */
/* 加滚动轴 */
overflow: scroll;
display: inline-block;
}
.outer>img{
/* 父容器的100% */
width: 50%;
}
</style>
</head>
<body>
<div class="outer">
<img src="img/1.jpg" >
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.mydiv{
width: 800px;
height: 300px;
/* 背景图 */
background-color: #FFC0CB;
background-image: url(img/1.jpg);
/* 当背景图太小时,不重复 */
background-repeat: no-repeat;
/* 背景图大小 auto cover*/
background-size: auto;
/* 背景图的显示位置 水平和垂直方向均显示为中间位置*/
background-position: center center;
/* 位置:left center right top bottom */
/* 默认显示背景图的左上 */
/* background-position: left top; */
/* 可以用百分比进行显示,50% 50%表示显示上下左右的中间 */
/* background-position: 50% 50%; */
/* 也可以用像素进行显示,此显示为左上 */
/* background-position: 0px 0px; */
/* 将背景图合写到一行,注意书写顺序 */
/* background: pink url(img/1.jpg) no-repeat center center/auto 100%; */
}
</style>
</head>
<body>
<div class="mydiv">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.mydiv1{
width: 800px;
height 300px;
display: inline;
margin-bottom: 30px;
}
span{
width: 800px;
height: 300px;
background-color: pink;
/* 块元素 */
display: block;
/* 内联元素 */
display: inline-block;
}
</style>
</head>
<body>
<div class ="mydiv1">div元素</div>
<!-- 内联元素不可以设置大小 -->
<span>span元素</span>
</body>
</html>
练习
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.nav {
/* 列表符号类型 */
list-style-type: none;
margin: 0;
padding: 0;
/* a用来控制透明度 */
background-color: rgba(100, 100, 100, 5);
}
.nav>li {
display: inline-block;
padding: 5px;
}
.nav>li>a {
text-decoration: none;
}
/* 鼠标滑过有颜色 */
.nav>li>:hover {
background-color: #FFC0CB;
}
.nav>li>:hover>a {
color: #0000FF;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">公司介绍</a></li>
<li><a href="#">产品介绍</a></li>
<li><a href="#">设备介绍</a></li>
<li><a href="#">人事招聘</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</body>
</html>
浮动:破坏默认的文档流布局
- 默认的文档流布局,块元素垂直排列,从下向上,行内元素,从左到右排列
- 浮动元素遵循浮动布局规则:
- 需要定义尺寸
- 按照定义的顺序,left左浮动,right右浮动,依次向右
- 浮动元素会释放原来占用的空间
- 清除浮动
- 必须是一个块元素(<div>最合适)
- clear:both(清除左浮动,右浮动,使用的最多),left,right
- 必须是清除元素的兄弟元素
示例一
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.d1,.d2,.d3 {
width: 300px;
height: 200px;
/* 左浮动 */
float: left;
text-align: center;
font-size: 40px;
}
.d1 {
background-color: aquamarine;
}
.d2 {
background-color: #FFC0CB;
}
.d3 {
background-color: brown;
}
.d4 {
width: 1200px;
height: 100px;
background-color: #FF0000;
}
.c {
/* 清除浮动 */
clear: both;
}
</style>
</head>
<body>
<div class="d1">1</div>
<div class="d2">2</div>
<div class="d3">3</div>
<!-- 清除浮动 -->
<div class="c"></div>
<div class="d4"></div>
</body>
</html>
示例二
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.nav {
/* 列表符号类型 */
list-style-type: none;
margin: 0;
padding: 0;
background-color: rgba(100, 100, 100, 0.5);
width: 500px;
}
.nav>li {
float: left;
padding: 5px 10px;
}
.nav>li>a {
text-decoration: none;
color: #666;
}
.nav::after {
content: "";
display: block;
clear: both;
width: 0;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">公司介绍</a></li>
<li><a href="#">产品介绍</a></li>
<li><a href="#">设备介绍</a></li>
<li><a href="#">人事招聘</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</body>
</html>
分割div
方法1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.mydiv {
width: 90%;
height: 400px;
border: 1px solid #ccc;
margin: 0 auto;
overflow: hidden;
}
.wrapper {
margin-right: -10px;
/* background-color: rgba(0,0,0,0.5); */
height: 100%;
}
.wrapper>.item {
width: 25%;
height: 100%;
float: left;
}
.wrapper::after {
content: "";
display: block;
clear: both;
width: 0;
}
.wrapper>.item>div {
margin-right: 10px;
background-color: pink;
height: 100%;
}
</style>
</head>
<body>
<div class="mydiv">
<div class="wrapper">
<div class="item">
<div></div>
</div>
<div class="item">
<div></div>
</div>
<div class="item">
<div></div>
</div>
<div class="item">
<div></div>
</div>
</div>
</div>
</body>
</html>
方法二
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.main {
width: 90%;
height: 400px;
/* border: 1px solid #ccc; */
margin: 0 auto;
background-color: #000000;
}
.class1 {
float: left;
height: 100%;
width: calc((100% - 40px) / 4);
background-color: blueviolet;
margin-right: 10px;
}
/* .class1:last-child {
margin-right: 0;
} */
</style>
</head>
<body>
<div class="main">
<div class="class1"></div>
<div class="class1"></div>
<div class="class1"></div>
<div class="class1"></div>
</div>
</body>
</html>
定位:破坏默认的文档流,可以让一个元素定位到页面的任意位置。
- 遵循定位规则
- 自定义位置
- 自定义定位方式
- 绝对定位:可以定位到页面的任意位置
- 参照物:从当前元素的父元素开始向上查找,第一个非static定位的元素即为参照物,如果找不到,则以boby为参照物
- 相对定位:可以定位到页面的任何位置
- 参照物:以未偏移之前,自己的位置为参照物
- 固定定位
- 参照物:以视区为参照(若页面滚动,则也不变)
示例一
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.main {
width: 400px;
height: 400px;
background-color: pink;
/* 定位 static|absolute|relative|fixed*/
position: absolute;
/* top: 100px;
left: 100px; */
margin-top: 200px;
margin-left: 200px;
/* right: 100px;
bottom: 100px; */
}
.class1 {
width: 100px;
height: 100px;
background-color: green;
/* 小广告 */
position: fixed;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
<div class="main">
<div class="class1"></div>
</div>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
<p>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br>很多文字<br></p>
</body>
</html>
示例二
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.lft {
width: 200px;
height: 20%;
background-color: chocolate;
}
.main {
position: absolute;
top: 0;
left: 200px;
height: 20%;
width: calc(100% - 200px);
background-color: pink;
}
</style>
</head>
<body>
<div class="lft"></div>
<div class="main"></div>
</body>
</html>
示例三
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
html,
body {
height: 300px;
}
body {
margin: 0;
padding: 0;
}
.top {
height: 100px;
background-color: pink;
}
.bot {
position: absolute;
top: 200px;
/* bottom: 0;
left: 0; */
width: 100%;
height: 100px;
background-color: #D2691E;
}
.ctr {
position: absolute;
top: 100px;
left: 0;
height: calc(100% - 200px);
width: 100%;
}
.ctr>div {
float: left;
height: 100%;
}
.ctr>div:first-child {
width: 200px;
background-color: #7FFFD4;
}
.ctr>div:nth-child(2) {
width: calc(100% - 400px);
background-color: blue;
}
.ctr>div:last-child {
width: 200px;
background-color: green;
}
.ctr::after {
content: "";
display: block;
clear: both;
width: 0;
}
</style>
</head>
<body>
<div class="top"></div>
<!-- <div class="ctr">
<div></div>
<div></div>
<div></div>
</div> -->
<div class="bot"></div>
</body>
</html>
常用样式
- transparent:透明色,表示完全透明