CSS层叠样式表

来源:互联网 发布:数控铣床编程实例详解 编辑:程序博客网 时间:2024/06/05 11:34
CSS层叠样式表Cascading Style Sheet


理解样式表对控制页面元素外观的作用

掌握样式规则的语法

理解各种选择器

理解样式代码在什么位置编写及他们的作用范围

掌握常用的样式属性


解决了网页内容与表现分离的问题


一、CSS样式规则声明


选择器,属性和值构成


选择器:是某个HTML标签的名称或自定义的名称


属性:你期望控制的样式的某个方面。例如,字体,颜色,边框,背景等


selector {propety:value;propety:value;……}


样式规则一般放在网页头部的style标签中


<style type="text/css">


body {
<!--背景图片-->
background-image: url("imgs/a.jpg");
}
p {
<!--首行缩进:通常是字体大小的两倍-->
text-indent: 32px;
<!--字体大小-->
font-size: 16px;
}

</style>


html选择器

h1{
text-align: center;
font-size: 18pt;
}

input {
border: 1px solid green;
background-color: #fedcba;
color: blue; 
}
a {
text-decoration: none;
}


text-align: center;设置文本的水平对齐方式

border: 1px solid gray; 设置边框的线宽、线型和颜色
 
text-decoration: none;设置文本的修饰方式


类选择器


应用场景:有时候,要求页面中同一种标签完全以相同的外观显示并不可行时,可以使用类选择器


.txt{
border: 1px solid green;
background-color:  #fedcba;
color: blue;
height: 20px;
}

.btn{
border: 1px solid blue;
background-color: blue;
color: red;
font-weight: bold;
}


用户名:<input /><br/>
密码:<input type="password" class="txt"/> <br/>
<input type="submit" value="登录" class="btn" />
<input type="reset" />


ID选择器


应用场景:为页面特定的某个标签定制外观


注意:ID选择器样式规则在一个页面中只被使用一次。


定义ID选择器以#开头,然后是id名字 


#txt1{
background-color: orange;
text-align: center;
font-size: 16px;
font-style: italic;
}
 用户名:<input id="txt1" /><br/>


伪类选择器


应用场景:用于向其他选择器指定适用范围内的特定元素或特定状态制定样式。锚伪类


a {
text-decoration: none;
font-size: 14px;
}
a:link {
color: blue;
}
a:VISITED {
color: gray;
}

a:HOVER{
color: red;
font-size: 16px;
text-decoration: underline;
}

a:ACTIVE{
font-style: italic;
}


link :未被访问时的状态


hover:鼠标在超链接上面时的状态


active:按下时的状态


visited:被访问过时的状态


注意:必须按这个顺序编写




伪元素选择器


应用场景:文本块的首行或首字符


p{font-size: 14px;}
p:FIRST-LINE {
font-size: 16px;
text-decoration: line-through;
}
h1{
font-size: 18pt;
color: black;
}
h1:FIRST-LETTER {
font-size: 24pt;
color: red;
border: 1px solid blue;
padding: 3px;

}


p:first-line  设置第一行


h1:first-letter 设置首字母


上下文选择器


ol li{
list-style-type: upper-latin;
font-size: 14px;
}

ul li {
list-style-image: url("imgs/reg3.gif");
font-weight: bold;
}






<ol>
<li>HTML语言总结</li>
<li>JavaScript总结</li>
<li>JAVA基础总结</li>
<li>JSP语法知识总结</li>
</ol>
<ul>
<li>Struts1.0用法总结</li>
<li>Struts2.0总结</li>
<li>Hibernate用法总结</li>
<li>Spring用法总结</li>
</ul>


除了父子标签可以形成上下文关系之外,类选择器与标签及ID选择器与标签也可形成上下文关系




群组联合选择器


多个样式规则的定义相同,但选择器名称不同。可以将这些样式规则合并成为一个样式规则,选择器名称之间使用逗号分隔


h1,h2,h3{
color:black;
}


h1{
font-size:24px;
}
h2{
font-size:18px;
}




二、样式规则声明写在何处


在head内的称为内部样式表


在标签中使用style属性中


外部文件style.css
<head>
<link rel="Stylesheet" type="text/css" href="/css/style.css" />
</head>




三、常用的CSS样式属性


背景


background-color: green;背景颜色
background-image: url("");背景图片
background-repeat: repeat;沿水平和垂直两个方法重复平铺
background-repeat: repeat-x;仅沿水平方向重复平铺
background-repeat: repeat-y;垂直平铺
background-repeat: no-repeat;不重复
background-attachment: scroll;背景图片随着容器一起滚动
background-attachment: fixed;背景图片在容器滚动时固定不动
background-attachment: inherit;继承
background-position: center|left|rigth center|top|bottom;位置
background-position: 50px 100px;

background: black url("") repeat scroll center top;




边框


border-top-width:thin|medium|thick|22px顶边框的宽度


border-right-width:


border-bottom-width:


border-left-width:


border-width:简写属性:为各个边框设置宽度。


border-top-style:solid实线|dashed虚线|dotted原点虚线|double双重线条|none无 顶边框的线型
  
border-right-style:


border-bottom-style:


border-left-style:


border-style:简写属性:为各个边框设置线型


border-top-color:顶边框颜色


border-right-color:


border-bottom-color:


border-left-color:


border-color:简写属性:为各个边框设置颜色


border-top:宽度 线型 颜色;


border-right:


border-bottom:


border-left:


border:在一个声明中用来设置四个边框的所有属性 宽度、线型、颜色




文本


color  设置文本颜色


letter-spacing 设置字符间距


text-align : left|right|center|justify 元素中文本的水平对齐方式


text-decoration:none无修饰|underline下划线|overline上划线|line-through删除线 向文本添加修饰


text-indent:缩进元素中文本的首行


text-transform:none|capitalize单词首字母大写|uppercase大写|lowercase小写。控制字符的大小写转换


word-spacing: 设置字间距




字体


font-style:normal正常|italic斜体|oblique倾斜 设置字体样式


font-variant:normal|small-caps 以小型大写字体或者正常字体显示文本


font-weight:normal=400|bold=700|bolder|lighter设置字体的粗细


font-size:设置字体尺寸


font-family:宋体,黑体 字体类型名称或者字体组列表


font:字体样式 显示文本 字体粗细 字体尺寸 字体类型名称




列表


list-style-type:列表项标记的类型


list-style-position:inside列表项标志插入到列表框开始处,就像是第一个字符|outside(默认)位于列表框外面。设置列表中列表项标记被放置的位置


list-style-image:将图像设置为列表项标记


list-style :简写属性 依次设置上面三个属性




小结


CSS层叠样式表对页面元素外观风格可实施精准控制,CSS使得网页结构与表现分离


选择器包含了HTML标签选择器、类选择器、ID选择器、伪类选择器 伪元素选择器、上下文选择器、群组选择器


样式代码可以放置在当前网页的头部,也可以放置网页之外的独立的样式文件中,还可以在内联的标签style属性中

0 0
原创粉丝点击