前端学习之初识CSS
一、CSS盒模型
1.盒模型的概念
盒模型由四个部分组成:
内容(content)、填充(padding)、边框(border)、外边距(margin)。
盒模型示例图:
padding、border和margin都有四个方向,分别是上(top)、下(bottom)、左(left)、右(right)。
(1)内容(content)
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
这里设置的就是content的宽高:
内容区域称为content-box
(2)填充(padding)
填充的区域:边框到内容之间的距离。
padding可以设置四个方向的值:
上内边距:padding-top;
右内边距:padding-right;
下内边距:padding-bottom;
左内边距:padding-left。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
padding-top: 20px; /*设置上内边距*/
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
结果:
如果只设置padding,如:padding: 20px;则是设置四个方向都为20px,若只设置三个值,则是按照上右下的顺序设置,左内边距默认和右边一致,如设置padding: 20px 30px 10px;则上内边距为20px,下内边距为10px,左右内边距都为30px,设置两个值的话则是设置的上右内边距,下左内边距与上右内边距一致。
padding区域加上content区域称为padding-box。
(3)边框(border)
宽度:border-width;
样式:border-style;
颜色:border-color。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
padding: 20px 30px;
border-width: 10px;
border-style: solid;
/*直线的样式,也有其他的不同显示样式*/
border-color: yellow;
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
border的三个属性都有对应的上右下左的设置,语法是border-left-width:10px;等等,border也有简写的方式,如:border: 10px solid green;
也可以设置border-top等等。
边框区域加填充区域加内容区域称为border-box。
(4)外边距(margin)
margin就是盒子和盒子的间距。
margin也有对应的四个方向的设置,分别是:
上外边距:margin-top:
右外边距:margin-right;
下外边距:margin-bottom;
左外边距:margin-left;
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
padding: 20px 30px;
border-width: 10px;
border-style: solid;
border-color: yellow;
margin: 20px 30px 10px;
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
结果:
margin的简写和padding是一样的,这里就不再说明了。
margin也有和padding不一样的地方,margin可以写作:
margin: 0 auto;可以让整个div进行水平居中:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
padding: 20px 30px;
border-width: 10px;
border-style: solid;
border-color: yellow;
margin: 0 auto;
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
结果:
2.标准盒模型和IE盒模型
标准盒模型
符合W3C。
当在网页头部写了<!Doctype html> 声明后,无论是哪种内核的浏览器,盒模型都会被解释为标准盒模型。
元素总宽度 = 内容宽度 + padding(左右) + border(左右) + margin(左右);
元素总高度 = 内容高度 + padding(上下) + border(上下) + margin(上下);
IE盒模型
当我们没有写声明,或者是声明丢失的时候,部分IE内核的浏览器,会触发怪异的盒模型。
元素总宽度 = 内容宽度 + margin(左右);
元素总高度 = 内容高度 + margin(上下);
注:content部分包含了padding和border。
在CSS3中有一个属性box-sizing可以完成标准和IE盒模型的切换。
标准盒模型的box-sizing属性值是content-box,而IE盒模型的box-sizing属性是border-box。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: green;
padding: 20px 30px;
border-width: 10px;
border-style: solid;
border-color: yellow;
margin: 0 auto;
box-sizing: border-box;/*设置为IE盒模型*/
}
</style>
<body>
<div>盒模型</div>
</body>
</html>
这里的话,content的宽度是100px,从图中可以看出,这个100包括了内容和padding、border:
二、CSS列表
在html中有讲到有序列表和无序列表,他们都有各自的默认属性,但是在日常开发中都是使用CSS来控制样式。
CSS中的列表样式:
list-style-type:设置列表项标记的类型。
list-style-image:将图像设置为列表项标记。
list-style-position:设置列表中列表项标记的位置。
list-style:简写样式。
1.list-style-type
用来设置列表项标记的类型。
无序列表:
disc:实心圆,默认值。
circle:空心圆。
square:实心方块。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
.style1{
list-style-type: disc;
}
.style2{
list-style-type: circle;
}
.style3{
list-style-type: square;
}
</style>
<body>
<ul class="style1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul class="style2">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul class="style3">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
结果:
有序列表
decimal:(阿拉伯数字,默认值)
lower-roman:(小写罗马数字,形如:i)
upper-roman:(大写罗马数字,形如:I)
lower-alpha:(小写英文,形如:a)
upper-alpha:(大写英文,形如:A)
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
.style1{
list-style-type: decimal;
}
.style2{
list-style-type: lower-roman;
}
.style3{
list-style-type: upper-roman;
}
.style4{
list-style-type: lower-alpha;
}
.style5{
list-style-type: upper-alpha;
}
</style>
<body>
<ol class="style1">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="style2">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="style3">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="style4">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="style5">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
</body>
</html>
2.list-style-image
用来将图像设置为列表项标记。
语法:
list-style-image:url();
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
.image1{
list-style-image: url(../img/csdn.png);
}
</style>
<body>
<ul class="image1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
结果:
这里由于图片大小的问题,导致了显示上有些奇怪,这个属性设置的就是每一项的标识符。
3.list-style-position
用来设置列表中列表项标记的位置。
常用属性:
outside:保持标记位于文本左侧。列表项目标记放置在文本外,且环绕文本不根据标记对齐。
inside:列表项目标记放置在文本以内,且环绕文本根据标记对齐。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
ul{
list-style-type: circle;
list-style-position: inside;
}
ol{
list-style-type: lower-alpha;
list-style-position: outside;
}
</style>
<body>
<ul>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
</ul>
<ol>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
</ol>
</body>
</html>
结果:
由此可以看出,inside是将标记也包含在文字内,而outside是将标记单独列出。
4.list-style
list-style可以实现上述三个属性的简写,具体语法如下:
list-style: type position image;
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
ul{
list-style:circle inside url(../img/csdn.png);
}
</style>
<body>
<ul>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
<li>这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字这是一段很长的文字</li>
</ul>
</body>
</html>
结果:
由此也可以看出,图片的权重比设置成circle更高。
三、CSS浮动
1.标准文档流
元素在排版布局过程中,自动自上而下分成一行一行,并在每行中按照从左到右的顺序流式排列。
块级元素和行级元素的排版方式就构成了文档流。
2.CSS浮动的用法
CSS浮动会使元素脱离文档流,并且向左或向右移动。
浮动是使用float实现的,float的值有:
none:默认不浮动;
left:左浮动;
right:右浮动。
示例:
未添加浮动时:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
ul{
list-style-type: none;
}
ul li{
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
<body>
<ul>
<li>浮动演示</li>
<li>浮动演示</li>
<li>浮动演示</li>
</ul>
</body>
</html>
自上而下排列:
添加左浮动:
ul li{
width: 100px;
height: 100px;
border: 1px solid blue;
float: left;
}
自左而右:
添加右浮动:
ul li{
width: 100px;
height: 100px;
border: 1px solid blue;
float: right;
}
浮动在右侧并自右向左显示:
一个浮动元素会尽量向左或向右移动,直到他的边缘碰到包含框或者另一个浮动框的边框为止。
浮动元素之前的元素不会受到影响,之后的元素会围绕他。例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
ul{
list-style-type: none;
}
ul li{
width: 100px;
height: 100px;
border: 1px solid blue;
float: left;
}
</style>
<body>
<ul>
<li>浮动演示1</li>
<li>浮动演示2</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
</ul>
<p>浮动演示浮动演示浮动演示浮动演示浮动演示浮动演示浮动演示浮动演示</p>
</body>
</html>
结果:
这个就是“高度塌陷”的问题,浮动的元素脱离了文档流,使得子元素的高度无法撑起父元素的高度。这里给他设置了一个border,很明显地看到,border的高度是0:
如何解决:
清除浮动:在浮动元素后面添加一个带clear的空元素。如:
.clearfix{
clear: both;
}
<ul>
<li>浮动演示1</li>
<li>浮动演示2</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
<li>浮动演示3</li>
<div class="clearfix"></div>
</ul>
结果:
但是像上述的这个方法并不规范,更为规范的是使用伪元素选择器。
ul::after{
content: '';
display: block;
height: 0;
clear: both;
}
结果:
除了通过伪元素清除浮动外,我们还可以通过父元素添加overflow属性来清除浮动。如:
ul{
list-style-type: none;
border: 2px solid red;
overflow:hidden;/*超出隐藏,若文字超过区域,会进行隐藏*/
}
结果:
四、CSS定位
1.position
静态定位:static(默认)。
相对定位:relative。
绝对定位:absolute。
固定定位:fixed。
relative
元素相对原来的位置进行了偏移;
元素没有脱离文档流。
因为relative需要相对原来的位置进行偏移,所以需要设置偏移量,而偏移量的设置就要通过以下几个属性来:
top、right、bottom、left。
单位是px或%。百分比的计算是参照父元素的。
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 300px;
height: 100px;
position: static;
}
.position1{
background-color: red;
}
.position2{
background-color: pink;
position: relative;/*相对定位*/
top: 50px;
left: 50px;
}
.position3{
background-color: yellow;
}
.position4{
background-color: aqua;
}
</style>
<body>
<div class="position1"></div>
<div class="position2"></div>
<div class="position3"></div>
<div class="position4"></div>
</body>
</html>
结果:
设置成%为单位的话:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 300px;
height: 100px;
position: static;
}
.position1{
background-color: red;
}
.position2{
background-color: pink;
position: relative;
top: 50px;
left: 50px;
}
.position3{
background-color: yellow;
}
.position4{
background-color: aqua;
}
.position5{
width: 50px;
height: 50px;
background-color: red;
left: 50%;
top: 50%;
position: relative;
}
</style>
<body>
<div class="position1"></div>
<div class="position2">
<div class="position5"></div>
</div>
<div class="position3"></div>
<div class="position4"></div>
</body>
</html>
结果可以看出是以父元素为基准进行偏移:
absolute
元素相对第一个带有定位属性的祖先元素(即有设置position属性,且不能为static)进行偏移。
如果祖先元素都不是定位元素,那么相对整个网页进行偏移。
使用绝对定位的话,元素会脱离文档流。
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 300px;
height: 100px;
position: static;
}
.position1{
background-color: red;
position: relative;
}
.position2{
background-color: pink;
position: absolute;
top: 50px;
left: 50px;
}
.position3{
background-color: yellow;
}
.position4{
background-color: aqua;
}
</style>
<body>
<div class="position1"></div>
<div class="position2"></div>
<div class="position3"></div>
<div class="position4"></div>
</body>
</html>
相对网页进行了偏移:
相对于距离最近的祖先元素进行偏移:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 300px;
height: 100px;
position: static;
}
.position1{
background-color: red;
}
.position2{
background-color: pink;
position: absolute;
top: 50px;
left: 50px;
}
.position5{
background-color: red;
position: absolute;
top: 50px;
width: 200px;
height: 80px;
}
.position6{
background-color: black;
position: absolute;
top: 20px;
width: 50px;
height: 50px;
}
</style>
<body>
<div class="position1"></div>
<div class="position2">
<div class="position5">
<div class="position6"></div>
</div>
</div>
</body>
</html>
这里距离position6最近的祖先元素时position5,所以相对position5发生了偏移:
fixed
元素相对浏览器的可视窗口进行偏移,不随着滚动条的滚动而滚动。
元素脱离文档流。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 300px;
height: 100px;
position: static;
}
.position1{
background-color: red;
}
.position2{
background-color: pink;
position: absolute;
top: 50px;
left: 50px;
}
.position5{
background-color: red;
position: absolute;
top: 50px;
width: 200px;
height: 80px;
}
.position6{
background-color: black;
position: absolute;
top: 20px;
width: 50px;
height: 50px;
}
.position3{
background-color: aqua;
top: 50px;
left: 50px;
position: fixed;
}
</style>
<body>
<div class="position1"></div>
<div class="position2">
<div class="position5">
<div class="position6"></div>
</div>
</div>
<div class="position3"></div>
</body>
</html>
结果:
类似于tb等的侧边导航栏就可以这样实现。
定位总结:
定位方式 | 取值 | 相对的元素 | 是否在文档流中 |
---|---|---|---|
相对定位 | relative | 元素本身的位置 | 在文档流中 |
绝对定位 | absolute | 元素相对第一个带有定位属性的祖先元素进行偏移,如果没有定位属性的祖先元素,就相对整个网页进行偏移 | 脱离文档流 |
固定定位 | fixed | 浏览器的可视窗口 | 脱离文档流 |
这一部分到这里就结束啦,有不足的地方还望大家指正啦,蟹蟹!