CSS
什么是CSS?
Cacading Style Sheet :层叠样式表
用来修饰html标签
CSS的使用方式(三种)
- 行内样式,在html标签中使用
每一个html标签都有style属性 : 写入样式属性
弊端:
1.只能针对某一个html进行修饰
2.css代码和html标签混到一块,不好管理
<a href="#" style="font-size: 30px;color: green;text-decoration: none;">超链接1</a>
- 内部样式(内联样式)(自己使用)
在head标签体中指定style标签,单独书写css代码
弊端
比行内样式好一些,但是不好管理
<style>
body {
background: #76b852;
}
</style>
- 外部方式(外联样式) (推荐方式)
在css文件夹下面单独创建一个以后缀名为css文件
在当前html页面中引入css文件即可
在head标签体中指定link href=“引入css文件” rel=“stylesheet”
<link href="css/index.css" rel="stylesheet" style="text/css"/>
CSS选择器
优先级:id选择器>类选择器>标签选择器
- 标签选择器
选中所有同名的标签
标签名称{
样式代码;
}
div{
font-size:30px;
color: #B3B34D;
}
- class类选择器
可以选中页面标签中所有的同名的class属性
.class属性值{
书写样式代码;
}
.ch1{
font-size: 40px;
color: red;
}
- id选择器
在页面标签中必须指定唯一的id属性值
标签中id属性值不能重复
#id属性值{
css样式代码;
}
#d1{
font-size: 20px;
color: lawngreen;
}
- 并集选择
选择器,选择器2{ //控制多个标签
css样式代码
}
#d1,#d2{
font-size: 35px;
color: blue;
text-decoration: underline;
}
- 交集选择器—后代选择器
选择器1 选择器2{
css样式代码;
}
#d1 span{
font-size: 20px;
color: greenyellow;
}
- 通用选择器
*:通配符
*{
css样式代码
}
*{
color:red ;
}
伪类选择器
- link状态:鼠标没有访问的状态
- hover:鼠标经过标签状态
- active:激活状态 鼠标点击但是没有松开的状态
- visited:已经的访问过的:点击后松开的状态
提示:在 CSS 定义中,a:hover 必须被置于 a:link 和 a:visited 之后,才是有效的。
提示:在 CSS 定义中,a:active 必须被置于 a:hover 之后,才是有效的。
格式
选择器:状态{
css样式代码
}
示例
<style>
/*没有访问过的状态*/
a:link{
font-size: 20px;
color: red;
}
/*访问过的状态,点击了并松开了 visited*/
a:visited{
font-size: 20px;
color: darkgray;
text-decoration: none;
}
/*鼠标经过状态:hover*/
a:hover{
font-size: 25px;
color: green;
/*文本修饰*/
text-decoration: none;
}
/*鼠标激活状态:点击但是没有松开的状态 active*/
a:active{
font-size: 30px;
color: blue;
text-decoration: underline;
}
</style>
效果
link
hover
active
visited
CSS文本属性
body{
/*color:设置文本颜色*/
color: red;
/*direction:设置文本方向:
默认值:ltr*/
direction: ltr;
/*line-height:设置行高 :行间距*/
/* line-height: 5px; */
/*letter-spacing:字符间距*/
letter-spacing: 10px;
/*设置的对齐方式 text-align*/
text-align: center;
/*text-decoration:设置文本修饰
none:默认值
line-through:中划线
overline:上划线
underline:下划线
*/
text-decoration:none;
/* word-spacing:单词间距
系统默认两个字组成一个单词
*/
word-spacing: 10px;
/*font-family :字体系列
是字体库中的有字体
*/
/*
font-family: "楷体";
*/
/*font-size:字体大小*/
/*
font-size: 30px;
*/
/*font-style:字体风格
默认值:normal :正常的字体
*/
/*
font-style: italic;
*/
/* font-style: oblique; */
/*font-weight:设置字体的粗细程度
bold 等价700px 适当加粗
*/
/*
font-weight: bold;
*/
/*
font字体的简写属性:将所有的字体属性在声明中定义
font:font-style font-weight font-size font-family
*/
/*
font:italic bold 40px "楷体";
*/
}
效果
CSS背景属性
body{
/*背景颜色:background-color*/
/*
background-color: darkgray;
*/
/*background-image:设置图像为背景*/
/*
background-image: url(img/高圆圆.jpg);
*/
/*background-repeat:设置图像是否重复以及如何重复
repeat:水平方向/垂直方向重复
repeat-x:水平方向重复
repeat-y:垂直方向重复
no-repeat:不重复
*/
/* background-repeat: no-repeat; */
/*
如果设置图像不重复,默认的位置 top left
backgroun-position:top left
图像的位置
top
center
bottom
图像放置在浏览器的位置
left
center
right
*/
/* background-position: top left; */
/*
背景的简写属性:background
background:background-color background-position
background-repeat background-image
*/
background: red top center no-repeat url(img/高圆圆.jpg);
}
CSS列表属性
ul{
/*list-style-type :设置列表前面的标记图形*/
/* list-style-type: none; */
/*list-style-image:自定义图形*/
/* list-style-image: url(img/start.jpg); */
/*list-style简写属性
list-tyle:list-style-type list-style-image
*/
list-style: none url(img/start.jpg);
}
CSS表格属性
table{
/*border-collapse :设边框和单元之间空隙
指定为collapse
*/
border-collapse: collapse;
}
CSS边框属性
#d1{
/*边框有四个边:给四个边分别设置颜色
注意事项:
1) 某个边框没有设置颜色,宽度或者样式,它会补齐对边的颜色,宽度以及样式
2)默认的方向:边框的颜色/边框的宽度/边框的样式:上 右 下 左
border-top-color
border-left-color
border-right-color
border-bottom-color
*/
/*
border-top-color: #F00;
border-left-color: #00F;
border-right-color: #0F0;
border-bottom-color: #c90;
*
/*边框颜色的简写属性*/
/* border-color: #F00; */
/*
边框的宽度:四个边框的宽度大小
border-top-width
border-left-width
border-right-width
border-bottom-width
*/
/* border-width: 10px; */
/*
border-top-width: 10px;
border-left-width: 20px;
border-right-width: 30px;
border-bottom-width: 40px;
*/
/*
边框的四个边要想显示出来必须设置
边框的样式属性:必须存在
border-top-style
border-left-stylee
border-rigth-style
border-bottom-style
*/
/*
border-top-style: solid;
border-left-style: double;
border-right-style: dotted;
border-bottom-style: dashed;
*/
/*边框样式的简写属性*/
/* border-style: solid; */
/*边框的简写属性:
border:border-width border-style border-color
*/
border:5px solid #000;
width: 100px;
height: 100px;
}
CSS浮动属性
#dl1{
width: 200px;
height: 200px;
border:1px solid #000 ;
background-color: #F00;
/*
浮动属性:float
值:left:左浮动 right:有浮动
一旦元素设置了浮动属性,就会脱离文档流
*/
float:left;
}
#dl2{
width: 200px;
height: 200px;
border:1px solid #000 ;
background-color: #00F;
float:left;
}
#dl3{
width: 200px;
height: 200px;
border:1px solid #000 ;
background-color: #0F0;
}
#clear{
/*清除浮动属性
clear:
left:左边不浮动
right:右边不浮动
both:左右两边浮动
*/
}
#clear{
clear:both;
}
CSS定位属性
position属性:定位
- absolute:绝对定位
- relative:相对定位
CSS盒子模型
盒子模型:万物皆盒子
将任意标签 看成是一个盒子---- 容量大小、厚度、内边距、外边距