目录
1.什么是CSS
1.1什么是CSS
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。
CSS 能够对网页中元素位置的排版进行像素级精确控制,支持几乎所有的字体字号样式,拥有对网页对象和模型样式编辑的能力。
CSS:表现(美化网页)
字体、颜色、边距、高度、宽度、背景图片、网页定位和浮动
1.2发展史
CSS1.0
CSS20. DIV(块) + CSS、HTML与CSS结构分离的思想,网页 变得简单,SEO
CSS2.1 浮动、定位
CSS3.0 圆角、阴影、动画.....浏览器兼容性~
1.3快速入门
style
<!-- 规范 <style> 可以编写代码,每一个声明使用分号结尾
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<!--连接-->
<link rel="stylesheet" href="style.css">
</head>
<bhody>
<h1>标题</h1>
css的优势
- 内容和表现分离;
- 网页结构表现统一,可以重复使用;
- 样式十分丰富;
- 建议使用独立于HTML的css文件;
- 利用SEO,容易被搜索引擎收录;
1.4 CSS三种导入方式
<!--2. 内部样式-->
<style>
h1{
color: green;
}
</style>
<!--3.外部样式-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--优先级:就近原则谁离标题近就是谁-->
<!--1. 行内样式,在标签元素中,编写一个style属性,编写样式即可-->
<h1 style="color: red">标题</h1>
拓展:外部样式两种写法
- 链接式:
html
<!--3.外部样式 链接式-->
<link rel="stylesheet" href="style.css">
- 导入式:
@import是CSS2.1特有的
2.选择器
作用:选择页面上的某一个或者某一类元素
2.1基本选择器
- 标签选折器 : 选择一类标签 标签{}
<style>
/*标签选择器会选择页面上所有的这个标签的元素*/
h1{
color: #119aec;
}
p{
color: aqua;
font-size: 60px;
}
</style>
</head>
<body>
<h1>java</h1>
<h1>java</h1>
<p>选择器</p>
- 类选择器 class :选择所有class属性一致的标签,跨标签 .类名
<style>
/*类选择器格式 .class的名称{}
好处,可以多个标签归类,是同一个class,可以复用
*/
.aaaa{
color: red;
}
.bbbb{
color: green;
}
</style>
</head>
<body>
<h1 class="aaaa">标题1</h1>
<h1 class="bbbb">标题2</h1>
<h1 class="bbbb">标题3</h1>
<p class="aaaa">P标签</p>
- id选折器:全局唯一 #id名称{}
<style>
/*id选择器 id必须全局唯一
#id名称{}
优先级:
不遵循就近原则,固定的
id选择器 > class选择器 > 标签选择器
*/
#AA{
color: #7914a0;
}
#BB{
color: hotpink;
}
.style{
color: cornflowerblue;
}
h1{
color: cyan;
}
</style>
</head>
<body>
<h1 id="AA">标题1</h1>
<h1 class="style">标题2</h1>
<h1 class="style" id="BB">标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>
优先级:id > class >标签
2.2层次选择器
- 后代选择器:在某个元素的后面 祖爷爷 爷爷 爸爸 你
/*后代选择器*/ body p{ background-color: #7914a0; }
- 子选择器,一代,儿子
/*子选择器*/ body>p{ background-color: cyan; }
- 相邻兄弟选择器 同辈
/*相邻兄弟选择器: 只有一个,相邻向下*/ .active + p{ background-color: aquamarine; }
- 通用选择器
/*通用选择器,当前选中元素的向下的所有兄弟元素*/
.active~p{
background-color: cornflowerblue;
}
2.3伪类选择器
/*ul的第一个子元素*/
ul li:first-child{
color: #119aec;
}
/*ul的最后一个子元素*/
ul li:last-child{
background-color: #119aec;
}
/* 选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效,顺序
*/
p:nth-child(1){
background-color: aquamarine;
}
/* 选中父元素下的p元素的第二个,类型*/
p:nth-of-type(1){
color: yellow;
}
2.4 属性选择器
id+class结合
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background-color: burlywood;
text-align: center;
color: #119aec;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/*属性名, 属性名 = 属性值(正则表达式)
存在id属性的元素 a[]{}
= 是绝对等于
*= 是含有这个元素就行
^=以这个开头
$=以这个结尾
*/
a[id]{
background-color: yellow;
}
a[id=first]{
background-color: #7914a0;
}
/*class中有links的元素*/
a[class*="links"]{
background-color: hotpink;
}
/* 选中href中以http开头的元素*/
a[href^=http]{
background-color: #119aec;
}
a[href$=pdf]{
background-color: #7914a0;
}
</style>
</head>
<body>
<p class="demo">
<a href="https://www.baidu.com" class="links item first" id="first">1</a>
<a href="" class="links item active" target="_blank" title="test">2</a>
<a href="images//123.html" class="links item">3</a>
<a href="images//123.png" class="links item">4</a>
<a href="images//123.jpg" class="links item">5</a>
<a href="abc"class="links item">6</a>
<a href="/a.pdf"class="links item">7</a>
<a href="/abc.pdf"class="links item">8</a>
<a href="abc.doc"class="links item">9</a>
<a href="abcd.doc" class="links item last">10</a>
</p>
</body>
= 是绝对等于
*= 是含有这个元素就行
^=以这个开头
$=以这个结尾