CSS
一、认识CSS
1、CSS简介
(1)CSS全称“层叠样式表(Cascading Style Sheets)”。
(2)作用:它主要是用于定义html内容在浏览器的显示样式,如文字大小,颜色,字体加粗等;
使用CSS样式的一个好处是通过定义某个样式,可以让不同网页位置的字体有着统一的字体、字号或者颜色等。
2、CSS代码语法
(1)CS样式有选择器和声明组成,而声明又由属性和值组成;
(2)选择器:指明网页中药应用样式规则的元素
(3)声明:在英文大括号“{}”中的就是声明,属性和值之间用英文冒号":"分隔,。当有多条声明时,中间可以英文分号";"分隔。
3、CSS注释代码:用/*开始,*/结尾。
代码块
<head>
<title>文档</title>
<style type="text/css">
p{color:#F00;font-size:36px;}
</style>
</head>
<body>
<p>今天天气怎么样</p>
</body>
二、CSS基本知识
1、内联式CSS样式:直接写在现有的html标签中
代码块
<head>
<title>文档</title>
<style type="text/css">
p{color:#F00;font-size:36px;}
</style>
</head>
<body>
<p>今天天气怎么样</p>
<!-------内联式CSS样式----->
<p style="font-size:18px;">
天气晴朗
</div>
</body>
2、嵌入式CSS样式:写在当前的文件中
代码块
<head>
<title>文档</title>
<!-------嵌入式CSS样式----->
<style type="text/css">
p{color:#F00;font-size:36px;}
</style>
</head>
<body>
<p>今天天气怎么样</p>
<p style="font-size:18px;">
天气晴朗
</div>
</body>
3、外部式CSS样式:写在单独的一个文件中,使用link引入
CSS样式文件名称以有意义的英文字母命名,如main,css
rel="stylesheet" type="text/css"是固定写法不可修改
<link>
标签位置一般写在<head>标签之内
test1.html:
<head>
<!-------外部式CSS样式--------->
<link href="main.css" rel="stylesheet" type="text/css" />
<title>文档<:/title>
<!-------嵌入式CSS样式----->
<style type="text/css">
p{color:#F00;font-size:36px;}
</style>
</head>
<body>
<p>今天天气怎么样</p>
<p style="font-size:100px;">
天气晴朗
</div>
</body>
main.css
body{ background-color:#ccc;}
4、优先级
内嵌式>嵌入式>外部式
三、CSS选择器
1、什么是选择器
2、标签选择器
3、类选择器:以点(.)开头
为标签设置class=“class”名称
代码块
<head>
<title>类选择器</title>
<style type="text/css">
body{text-align:center;}
.title{color:#F00;font-family:"楷体";}
.p1{font-family:"宋体";font-size:24px;}
.p2{font-family:"黑体"; font-sizr:36px;}
</head>
<body>
< h1 class="title">春晓</hi>
<p class="p1">春眠不觉晓,处处闻啼鸟。</p>
<p class="p2">夜来风雨声,花落知多少。</p>
</body>
4、ID选择器:以#开头
为标签设置id=“ID”名称 ,而不是class="类名称"
ID选择符的前面是#。而不是英文圆点(.)
代码块
<head>
<title>ID选择器</title>
<style type="text/css">
body{text-align:center;}
#title{color:#F00;font-family:"楷体";}
#p1{font-family:"宋体";font-size:24px;}
#p2{font-family:"黑体"; font-sizr:36px;}
</head>
<body>
< h1 id="title">春晓</hi>
<p id="p1">春眠不觉晓,处处闻啼鸟。</p>
<p id="p2">夜来风雨声,花落知多少。</p>
</body>
5、类和ID选择器的区别
相同点:可以应用于任何元素
不同点:
(1)ID选择器只能在文档中使用一次,与类选择器不同,在一个html文档中,ID选择器只能使用一次,而类选择器 可以使用多次
(2)可以使用类选择器词列表方法为一个元素同时设置多个样式,我们可以为一个元素同时设置多个样式,但只可以用类选择器的方法实现,ID选择器是不可以的(不能使用ID词列表)
(3)ID选择器的优先级高于类选择器
代码块5.1(2)
<head>
<title>ID选择器</title>
<style type="text/css">
body{text-align:center;}
#title{color:#F00;font-family:"楷体";}
.dd1{font-size:50px;font-weight:bold;}
.dd2{color:#F00;}
#p1{font-family:"宋体";font-size:24px;}
#p2{font-family:"黑体"; font-sizr:36px;}
</head>
<body>
< h1 id="title">春晓</hi>
<!----class可以实现多个---->
<p class="dd1 dd2">春眠不觉晓,处处闻啼鸟。</p>
<!----id只能实现一个---->
<p id="p1 p2">夜来风雨声,花落知多少。</p>
</body>
代码块5.2(3)
<head>
<title>ID选择器</title>
<style type="text/css">
body{text-align:center;}
.pp{color:#F00;font-family:"楷体";}
#p1{font-family:"宋体";font-size:24px;}
#p2{font-family:"黑体"; font-sizr:36px;}
</head>
<body>
< h1 id="title">春晓</hi>
<!------ID选择器的优先级高于类选择器------->
<p class="pp" id="p1">春眠不觉晓,处处闻啼鸟。</p>
<p id="p2">夜来风雨声,花落知多少。</p>
</body>
6、子选择器
大于符号(>)用于选择指定标签元素的第一代子元素、
代码块
<head>
<title>子选择器</title>
<style type="text/css">
body{text-align:center;}
div>p{color:F00; font-size:18px;}
<!-----------ul>li{color:F00; font-size:18px;}---------->
</style>
</head>
<body>
<div>
< p>春晓</p>
<p>春眠不觉晓,处处闻啼鸟。</p>
<p>夜来风雨声,花落知多少。</p>
<div>作者</div>
</div>
<div>鹅鹅鹅</div>
<u1>
<li>01 html</li>
<li>02 CSS</li>
<li>03 javascript</li>
</u1>
</body>
7、包含(后代)选择器
加入空格用于选择指定标签元素下的后辈元素
8、通用选择器(了解)
*{background-color:#CCC;}
9、伪类选择符
伪元素选择器
(1)a:link:正常连接的样式
(2)a:hover:鼠标放上去的样式
(3)a:active:选择链接时的样式
(4)a:visited:已经访问过的链接的样式
代码块
<head>
<title>伪类选择符</title>
<style type="text/css">
*<text-align:center;backround-color:#999;>
/* 让li显示在一行*/
.nav li{display:inline;}
.nav a.link{text-decoration:none;font-size:4px;}/*正常链接*/
a{font-size:24px;text-decoration:none; color:#00F}
a:hover{font-size:30px;color:#F00;}/*鼠标放上去的样式*/
a.active{color:#F0F;font-size:30px;}/*选择链接时的样式*/
a.visited{background-color:#000;color:#FFF;}
</style>
</head>
<body>
<ul>
<li><a href="#">首页</li>
<li><a href="#">简介</li>
<li><a href="#">新闻</li>
<li><a href="#">介绍</li>
<li><a href="#">联系</li>
</body>
10、分组选择符
分组设置对应的CSS样式
四、常见属性
1、颜色属性
color后接不同的颜色格式,如green、rgb、#FFFF80等
2、字体属性(API文档)
font-size 字体大小
font-family 字体类型
font-weight 字体加粗
3、背景属性
背景颜色 background-color
背景图片 background-image
背景重复 background-repeat
repeat-x
repeat-y
背景位置 background-position
横向:left/right/center
纵向:top/botton/center
简写方式:
background:背景颜色 url(图像)重复 位置
background:#f60 url(image/bg.jpg) no-repeat top center
4、文本属性
1)text-align 横向排列
2)line-height 文本行高
3)text-index 首行缩进
4)letter-spacing 字符间距
normal默认
length设置具体的数值(可为负)
inherit继承
代码块(图片img/bg.png)
CSS文件:main.css
body{background:url(img/bg.png) repeat-x
}
p{
color:red;
font-size:24px;
font-family:"楷体";
font-weight:bold;
line-height:150%;
text-index:80px;
letter-spacing:10px;
}
.title{
text-align:center;
line-weight:100px;
}
html文件
<head>
<title>引入CSS样式</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
5、边框属性
1)边框风格 border-style
border-botton-style 下拉边框样式
border-top-style 上边边框样式
border-left-style 左边边框样式
border-right-styl 右边边框样式
边框属性值:
none 无边框
solid 直线边框
dashed 虚线边框
dotted 点状边框
double 双线边框
groove 凸槽边框
ridge 垄状边框
inset inset边框
outside inset边框
inherit继承
border-color属性值
2)边框颜色 border-color
3)简写方式
代码块5.1
<head>
<title></title>
<style type="text/css"/>
.dl{
width:500px;
height:400px;
border-style:solid;
background-color:#9FFBDB;
border-botton-color:#F00;
}
<style>
</head>
代码块5.2
<head>
<title></title>
<style type="text/css">
.input{
border:0;
border-bottom:#000 solid 1px;
width:200px;
}
</style>
</head>