一、CSS的引入
XML与HTML的引入:
调入外部样式:通过引用外部css文件来定义xml文件的表现形式
xml:
<?xml-stylesheet type="文件类型" herf="css文件的URL" ?>
html:
<link rel="stylesheet" type="text/css" href="style.css" />
内部调入样式:将css样式直接嵌入xml文档内部调用了HTML的<style>标记
xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css">
<persons xmlns:Html="http://www.w3.org/Profiles/XHTML-transitional">
<HTML:style>
person{
display:block;
font-size:25px;
}
</HTML:style>
</persons>
html:
book {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333;
}
chapter {
font-size: 18px;
font-weight: bold;
margin-top: 20px;
margin-bottom: 10px;
}
行内式:提升效率
xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css">
<student>
<name style="display:block;font-size:18px";font-weigth">
<sex style="display:line;font-size:12px">女</sex>
</name>
</student>
html:
<p style="display:block;font-size:18px">这是一个标签</p>
二:Selecter选择器:
可以同时指定多个元素,也可以是元素的class属性和ID属性
通配符选择器:
可以简化代码,实现全部代码
*{
margin:0px;
padding:0px;
}
定义样式:
选择器{
属性1:属性值1;
属性2:属性值2;
属性3:属性值3;
......
属性n:属性值n;
}
1、类型选择符(Type Selecter):
以元素、对象的名称作为选择符。
.name{
font-family:"微软雅黑";
font-size:22px;
font-weigth:bold;
}
2、类选择符(Class Selector):
可以以一种独立于文档元素的方式来指定,可以单独使用也可以与其他元素结合使用。
.anthorname{
color:pink;
font-size:20px;
}
<name class="anthorname">张三</name>
3、ID选择符:(ID Selector)
允许以一种独立于文档元素的方式来指定;前面必须有一个#;只能在文档中使用一次,
#intro {
font-weight:bold;
}
<p id="intro">这是一个标题</p>
4、成组选择器:
可以把一组属性用于多个元素,用逗号将个元素隔开。
并集:
teacher,student{
display:block;
margin:10px;
padding:5px;
}
交集:即是title元素,又是class=c1的元素
title.c1{
display:block;
margin:10px;
}
5、后代选择器(包含选择器/上下文选择器):
包含关系:
A B
{
}
例:message里面包含的name元素里字体都会变为20px,颜色为粉色
message name{
font-size:20px;
color:pink;
}
<message>
<name>
<b>张三</b>
</name>
<sex>男</sex>
<name>李四 </name>
<sex>男</sex>
</message>
子元素选择器 :
A的子元素是B才可以被选中:
A>B{
}
例:message里面只有包含name元素里字体才会变为20px,颜色为粉色
message > name{
font-size:20px;
color:pink;
}
<message>
<name>
<b>张三</b>
</name>
<sex>男</sex>
<name>李四 </name>
<sex>男</sex>
</message>
<message>
<name>王丽 </name>
</message>
兄弟元素:
相邻的才会被选中:
A+B{
}
后续的全部选中:
A~B{
}
6伪元素(pseude-element):
用来添加一些选择器的特殊效果,将文档中不能作为独立元素看待的部分作为元素看待
(1)首行伪元素 :first-line[只能用于块级元素]
p:first-line{
color:#00ff00;
font-weight:bold; //字体的粗细程度
}
(2)首字符伪元素 :first-letter[用于向文本的首字母设置特殊格式]
p:first-letter
{
color:#ffff00;
font-weight:bold; //字体的粗细程度
}
(3)伪类(:link,:visited,:hover,:active,:focus,:first-child)
选择未访问链接样式
a:link
{
background-color:yellow;
}
选择访问过得链接样式
a:visited
{
background-color:yellow;
}
选择鼠标移到链接上的样式:
a:hover
{
background-color:yellow;
}
选择激活的链接样式:
a:active
{
background-color:yellow;
}
一个输入字段获得焦点时选择的样式:
input:focus
{
background-color:yellow;
}
用来指定元素的第一个子元素
p:first-child
{
background-color:yellow;
}
三、常用css属性
1、显示属性(display)
行内元素:span a
块级元素:div p
行内块级元素 img
1.1 块方式 display:block
使文本在一个块区域内显示
name
{
display:block
//可通过设置width和heigth设置块区域的宽和高
width:200px;
heigth:300px;
}
同级别元素按先后顺序靠左对齐;子元素指定的块区域将被包含在为父元素指定的块区域中
position:absolute 改变区域默认的左对齐方式,但必须同时设置left、top、width、height
1.2 行方式: display:line(inline)使文本与行的方式显示
name
{
display:line
}
同级别元素按先后顺序首位相接;子元素指定的块区域将被包含在为父元素指定的块区域中以行的方式显示
position:absolute 改变区域默认的先后顺序尾随在另一行首位之后,但必须同时设置left、top
1.3列表方式: display:list-item使文本按列表方式在浏览器中显示
name
{
display:list-iten;
}
list-iten-type属性有:
disc (缺省值,黑圆点)、circle (空心圆点)square (小黑方块)、decimal (数字排序)、lower-roman (小写罗马字排序)、upper-roman (大写罗马字排序)、lower-alpha (小写字母排序)、upper-alpha (大写字母排序)、none (无列表项标记)
name{
display:none; //不显示文本,不占用空间
visibility:hidden //不显示文本,但是占用空间
}
2、字体属性(font)
name{
font-family:Arial; //字体类型
font-style:italic; //字体样式 italic斜体,normal正常显示、oblique倾斜
font-variant:small-caps; //英文字体打印时的大小写状态 normal(大小写无变化)small-caps大写字母代替小写
font-weight:600px; //粗细程度 bold加粗;normal默认标准字体
font-size:20px; //字体大小
}
3、颜色属性(color、background-color)和背景属性(background-xxx)
color:置元素的文本以及文本装饰的前景色颜色值
background-color:设置不同元素的背景色
表示颜色的三种属性方法:
name{
background-color:rgb(255,255,255)
color:#00ff00;
coloe:pink;
background-img:(URL("a1.jpg")
}
4、文本属性(text-xxx)文本的对齐方式
name{
text-align:center; //居中
text-align:right; //右对齐
text-align:justify; //两端对齐
text-index:17px; //首行缩进
//指定在一个文本中的大写和小写字母。
text-transform:uppercase; //全部大写
text-transform:lowercase; //全部小写
text-transform:capitalize; //首字母大小
//向文本添加修饰
text-decoration: none; //不加任何线
text-decoration:overline; //加下划线
text-decoration:line-through;//加删除线
text-decoration:underline // 加上划线
line-height:1.5; //设置文本的行间距
}
5、边框(border-xxx)
border-xxx-width属性:设置边框的宽度 xxpx;
border-xxx-color属性:设置边框颜色
border-xxx-style属性:设置元素的边框
四、定位和浮动
1、定位:指定元素的显示位置(position:static、absolut、
relative)
偏移量用left和top来决定
1、1绝对定位(absolut):以设置了position的左上角为原点,偏移后原来的位置不在占用空间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相对定位</title>
<style type="text/css">
div{
width: 200px;
height: 300px;
}
.box1{
background-color: red;
}
.box2{
position: relative;
top: 30px;
left: 40px;
background-color: green;
}
.box3{
background-color: blue;
}
</style>
</head>
<body>
<div class="box1">盒子1</div>
<div class="box2">盒子2</div>
<div class="box3">盒子3</div>
</body>
</html>
1、2相对定位(relative)相对于自己原来位置的偏移,原来的位置还要占用空间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
<style type="text/css">
div{
width: 200px;
height: 300px;
border: 3px solid #ff6700;
}
.box1{
position: absolute;
left: 50px;
background-color: red;
}
.box2{
background-color: green;
}
.box3{
background-color: blue;
}
</style>
</head>
<body>
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
</body>
</html>
2浮动(float)
浮动的原则:上方元素不浮动则浮动元素上不去;脱离原来的文本流;下方元素会占据浮动元素的位置;对后续元素有影响,要清除浮动
2.1清除浮动
2.1.1、父盒子设置固定高度
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浮动元素的破坏性</title>
<style type="text/css">
.father{
/*父盒子设置了固定高度*/
height: 200px;
border: 1px solid red;
}
.child{
width: 200px;
height: 200px;
float: left;
background-color: green;
}
</style>
</head>
<body>
<div class="father">
<div class="child">儿子</div>
</div>
</body>
</html>
缺点:使用不灵活,后期不易维护
2.1.2内墙法:在浮动元素的后面加一个空的块级元素(通常是div),并且该元素设置clear:both;
属性。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浮动元素的破坏性</title>
<style type="text/css">
.father{
border: 1px solid red;
}
.child{
width: 200px;
height: 200px;
float: left;
background-color: green;
}
.clearfix{
clear: both;
}
</style>
</head>
<body>
<div class="father">
<div class="child">儿子</div>
<div class="clearfix"></div>
</div>
</body>
</html>
缺点:结构冗余
2.1.3伪元素清除法
p::after{
/*p::after{}一定要有content。表示在p元素内部的最后面的添加内容*/
content:'...'
}
<style>
.clearfix:after{
content:'.';//给.clearfix元素内部最后添加一个内容,该内容为行内元素。
display: block;//设置该元素为块级元素,符合内墙法的需求
clear: both;//清除浮动的方法
overflow: hidden;//表示隐藏元素
height: 0;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浮动元素的破坏性</title>
<style type="text/css">
.father{
border: 1px solid red;
}
.child{
width: 200px;
height: 200px;
float: left;
background-color: green;
}
.cleafix:after{
content:'.';
display: block;
clear: both;
overflow: hidden;
height: 0;
}
</style>
</head>
<body>
<div class="father clearfix">
<div class="child">儿子</div>
</div>
</body>
</html>
2.1.4overflow:hidden(定义一个元素的内容太大而无法适应盒子的时候该做什么)
visible默认值。内容不会被修剪,会呈现在元素框之外
hidden内容会被修剪,并且其余内容不可见
scroll内容会被修剪,浏览器会显示滚动条以便查看其余内容auto由浏览器定夺,如果内容被修剪,就会显示滚动条
inherit规定从父元素继承
overflow属性的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow属性的用法</title>
<style type="text/css">
.box{
width: 300px;
height: 300px;
border: 1px solid red;
overflow: hidden;
}
</style>
</head>
<body>
<div class="box">
这是一个文本
</div>
</body>
</html>