CSS3
1.CSS介绍
- HTML+CSS+JS 结构+表现+交互
2.CSS发展史
- Cascading Style Sheet 层叠级联样式表
- 表现层:美化网页(字体、颜色、边距、宽高、网页定位、网页浮动……)
- 1.0/2.0/2.1/3.0
- 2.0:DIV(块)+CSS,提出HTML和CSS结构分离的思想,利于SEO
- 2.1:浮动和定位
- 3.0:圆角边框、阴影、动画……存在浏览器兼容性问题
3.CSS入门
- style
- link外部引入
- 优势:
- 内容和表现分离
- 网页结构表现统一,可以实现复用
- 样式十分丰富
- 利于SEO搜索引擎收录(VUE不利于)
<style>
h1{
color: coral;
}
</style>
<link rel="stylesheet" href="css/style.css">
4.四种CSS导入方式
- 链接式是属于html,导入式必须放在style标签中,是属于CSS2.1
- 优先级遵循就近原则
<style>
h1{
color: aqua;
}
</style>
<link rel="stylesheet" href="css/style.css">
<h1 style="color: coral">我是标题</h1>
5.三种基本选择器
- 作用:选择页面上某一种或者某一个元素
- 基本选择器:
- 标签选择器:选择所有标签 标签{}
- 类选择器class:选中class名一致的标签 .class{}
- id选择器:全局唯一不能重复 #id{}
- 优先级:不遵循就近原则,id选择器 > 类选择器 > 标签选择器
<style>
h1{
color: aqua;
background: coral;
border-radius:24px ;
}
</style>
<style>
.hi{
color: azure;
}
.hello{
color: bisque;
}
</style>
<h1 class="hi">hi</h1>
<h1 class="hello">hi</h1>
<h3 class="hello">hi</h3>
<style>
#hi{
color: azure;
}
</style>
<h1 id="hi">h1</h1>
<h1>h2</h1>
<h1>h3</h1>
<h1>h4</h1>
<h1>h5</h1>
6.层次选择器
- 后代选择器:在某个元素的后面,选择所有后代
- 子选择器:只选择后面一代
- 相邻/兄弟选择器,同辈,只有一个
- 通用选择器
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p {
color: bisque;
}
body p{
background: aqua;
}
body>p{
background: coral;
}
.active+p{
background: aquamarine;
}
.active~p{
background: blue;
}
</style>
</head>
<body>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>
<p>p4</p>
</li>
<li>
<p>p5</p>
</li>
<li>
<p>p6</p>
</li>
</ul>
</body>
7.结构伪类选择器
- 伪类选择器
- 伪类:a:hover
- 结构:定位元素、与结构相关
<style>
ul li:first-child{
background: aquamarine;
}
ul li:last-child{
background: bisque;
}
p:nth-child(1){
background: blueviolet;
}
p:nth-of-type(2){
background: aqua;
}
</style>
8.属性选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float:left;
display: block;
height: 50px;
width: 50px;
border-radius:10px ;
background: coral;
text-align: center;
color: blueviolet;
text-decoration: none;
margin-right: 5px;
font:bold 20px/50px Arial;
}
a[id]{
background: blue;
}
a[class*="links"] {
background: azure;
}
a[href^="http"]{
background: azure;
}
a[href$="pdf"]{
background: blue;
}
</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">6</a>
<a href="/a.pdf">7</a>
<a href="/abc.pdf">8</a>
<a href="abc.doc">9</a>
<a href="abcd.doc"class="links item last">10</a>
</p>
</body>
</html>
9.CSS的作用及字体样式
- 有效传递页面信息
- 吸引注意,美化页面
- 凸显主题
- 提高用户体验
- span标签:重点要突出的字,使用span标签套起来
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title1{
font-size:50px;
}
</style>
</head>
<body>
欢迎学习 <span id="title1">Java</span>
</body>
=========================================================================================
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
font: oblique bolder 16px "幼圆";
}
h1{
font-size: 50px;
color: coral;
}
.p1{
font-weight: bolder;
}
</style>
</head>
<body>
<h1>故事介绍</h1>
<p class="p1">
及爱哦都叫我大家都岛炮击低洼的接待觉得我i巨大的的咯我就低洼的窘境地位i就
</p>
<p>
大家都哈维和带我带我到巨大危机骄傲的我到家都i大家就都到基金我的骄傲滴哦我觉得决定骄傲的骄傲到达口袋击破敌军安排接待评委九点九万九千
</p>
</body>
10.文本样式
- 颜色
- 对齐方式
- 首行缩进
- 行高
- 装饰(下划线……)
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
h1 {
color: rgba(0, 255, 255, 0.1);
text-align: center;
}
.p1 {
text-indent: 2em;
}
.p3 {
background: azure;
height: 300px;
line-height: 300px;
}
.l1 {
text-decoration: underline;
}
.l2 {
text-decoration: line-through;
}
.l3 {
text-decoration: overline;
}
img,span{
vertical-align: middle;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<p>
<img alt="" src="images/a.png">
<br>
<span>djaidjadjoajddpo</span>
</p>
<p class="l1">123123</p>
<p class="l2">123123</p>
<p class="l3">123123</p>
<p>123123</p>
<h1>故事介绍</h1>
<p class="p1">
及爱哦都叫我大家都岛炮击低洼的接待觉得我i巨大的的咯我就低洼的窘境地位i就
</p>
<p class="p3">
大家都哈维和带我带我到巨大危机骄傲的我到家都i大家就都到基金我的骄傲滴哦我觉得决定骄傲的骄傲到达口袋击破敌军安排接待评委九点九万九千
</p>
</body>
11.文本阴影和超链接伪类
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
text-decoration: none;
color: black;
}
a:hover{
color: coral;
font-size: 50px;
}
a:active{
color: blue;
}
a:link{
color: aqua;
}
a:visited{
color: blueviolet;
}
.price{
text-shadow: 10px 10px 5px chartreuse;
}
</style>
</head>
<body>
<a href="#">
<img src="images/a.JPG" alt="">
</a>
<p>
<a href="#">骂出高效</a>
</p>
<p>
<a href="">作者:狂神</a>
</p>
<p id="price">
$99
</p>
</body>
12.列表
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表样式</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="nav">
<h2 class="title">全部商品分类</h2>
<ul>
<li>
<a href="#">图书</a>
<a href="#">音像</a>
<a href="#">数字商品</a>
</li>
<li>
<a href="#">家用电器</a>
<a href="#">手机</a>
<a href="#">数码</a>
</li>
<li>
<a href="#">电脑</a>
<a href="#">办公</a>
</li>
<li>
<a href="#">家居</a>
<a href="#">家装</a>
<a href="#">厨具</a>
</li>
<li>
<a href="#">服饰鞋帽</a>
<a href="#">个性化妆</a>
</li>
<li>
<a href="#">礼品箱包</a>
<a href="#">钟表</a>
<a href="#">珠宝</a>
</li>
<li>
<a href="#">食品饮料</a>
<a href="#">保健食品</a>
</li>
<li>
<a href="#">彩票</a>
<a href="#">旅行</a>
<a href="#">充值</a>
<a href="#">票务</a>
</li>
</ul>
</div>
</body>
</html>
#nav{
width: 400px;
background: azure;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 30px;
background: antiquewhite;
}
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
background: azure;
}
a{
text-decoration: none;
font-size: 14px;
color: rebeccapurple;
}
a:hover{
color: aqua;
text-decoration: underline;
}
13.背景图像应用及渐变
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image: url("images/tx.JPG");
}
.div1{
background-repeat: repeat-x;
}
.div2{
background-repeat: repeat-y;
}
.div3{
background-repeat: no-repeat;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 30px;
background: antiquewhite url("../images/tx.JPG")270px 10px no-repeat;
}
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
background: azure;
background-image: url("../images/tx.JPG");
background-repeat: no-repeat;
background-position: 200px 2px;
}
- 渐变:www.grbient.com
<style>
body{
background-image:linear-gradient(19deg,blue 0%,coral 100%) ;
}
</style>
14.盒子模型及边框的使用
- 盒子模型
- margin外边距
- border边框
- padding内边距
- 边框
<style>
h1,h2,ul,li,a,body{
margin: 0;
padding: 0;
text-decoration: none;
}
#box{
width: 300px;
border: 1px solid red;
}
h2{
font-size: 16px;
background: bisque;
line-height: 50px;
color: blueviolet;
}
form{
background: aqua;
}
div:nth-of-type(1) input{
border: 3px solid black;
}
div:nth-of-type(2) input{
border: 3px dashed chocolate;
}
</style>
15.内外边距及DIV居中
- 盒子的计算方式(元素的大小到底多大):margin+border+padding+内容宽度
<style>
#box {
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
h2 {
font-size: 16px;
background-color: bisque;
line-height: 30px;
color: blueviolet;
margin: 0;
}
form {
background: aqua;
}
input {
border: 1px solid black;
}
div:nth-of-type(1){
padding: 10px;
}
</style>
16.圆角边框及阴影
- 边框有四个角,存在上下左右的概念\
- element
- 飞冰 ice.work
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 100px;
height: 100px;
border: 10px solid black;
border-radius: 0 50px 100px 10px;
}
image{
border-radius: 100px;
}
</style>
</head>
<body>
<div></div>
<img src="images/tx.JPG" alt="">
</body>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 100px;
height: 100px;
border: 5px solid black;
box-shadow: 10px 10px 5px blue;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
17.display和浮动
- 标准文档流:正常排版不会浮动飘出去
- 块级元素:独占一行
h1-h6
p
div
列表....
- 行内元素:不独占一行
span
a
img
strong
- 行内元素可以被包含在块级元素中,反之则不行
- display
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 100px;
height: 100px;
border: 1px solid black;
display: inline;
}
span{
width: 100px;
height: 100px;
border: 1px solid black;
display: inline-block;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
- float-left(right),图片向左(右)浮动,悬浮、独立于原来的背景之上,会自动排版,边框会塌陷,用clear:both可以解决边框塌陷,既可以浮动,又可以有块元素效果
- disply也是实现行内元素排列的方式,但更多的是使用float
18.overflow及父级边框塌陷问题
- ctrl+x 快速删除一行
- clear属性具有四个值
- right:右侧不允许有浮动元素
- left:左侧不允许有浮动元素
- cboth:两侧不允许有浮动元素
- none:可以让其浮动
- 解决父级边框塌陷问题四大解决方案:
- 增加父级元素的高度
- 增加空div标签,class为clear,设置其内外边距为0
- overflow:
- 在父类添加一个伪类after
-
father:after{ content:“ ”;display:block;clear:both; }、
- 对比
- display方向不可控制
- float浮动起来会脱离标准边框流
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#content{
width: 200px;
height: 50px;
overflow: scroll;
}
</style>
</head>
<body>
<div id="content">
<img src="images/2.JPG" alt="">
</div>
<p>
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈。
</p>
</body>
19.相对定位
- 定位分类:
- 相对定位 position:relative (仍然在标准文档流中,原来位置被保留,不会浮动出去)
- 绝对定位
<style>
body{
padding: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #666666;
padding: 0;
}
#first{
background-color: bisque;
border: 1px dashed #7db224;
position: relative;
top:-20px;
left:20px;
}
#second{
background-color: #653b0a;
border: 1px dashed #225edc;
}
#third{
background-color: #624e37;
border: 1px dashed #ce1515;
bottom:-10px;
}
</style>
20.绝对定位和固定定位
- 绝对定位:基于xxx定位进行上下左右的移动
- 没有父级元素定位的话,相对于浏览器定位
- 假设父级元素存在定位,通常会相对于父级元素进行偏移
- 在父级元素范围内移动
- 绝对定位position:absolute不保留原来位置,会浮动出去,相对于父级元素或浏览器进行偏移
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #666666;
padding: 0;
}
#first{
background-color: bisque;
border: 1px dashed #7db224;
}
#second{
background-color: #653b0a;
border: 1px dashed #225edc;
position: absolute;
right: 20px;
}
#third{
background-color: #624e37;
border: 1px dashed #ce1515;
}
</style>
- 固定定位
<style>
body{
height:1000px;
}
div:nth-of-type(1){
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){
width: 50px;
height: 50px;
background: blue;
position: fixed;
right: 0;
bottom: 0;
}
</style>
21.z-index及透明度
- 图层概念
- z-index最低为0,最高无限
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="content">
<ul>
<li><img alt="" src="images/1.JPG"></li>
<li class="tipText">学习微服务,找小狂</li>
<li class="tipBg"></li>
<li>时间2099-01-01</li>
<li>地点:火星基地</li>
</ul>
</div>
</body>
</html>
#content {
width: 120px;
padding: 0;
margin: 0;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border: 1px black solid;
}
ul, li {
padding: 0;
margin: 0;
list-style: none;
}
#content ul {
position: relative;
}
.tipText, .tipBg {
position: absolute;
width: 120px;
height: 25px;
top:25px;
}
.tipText{
color: azure;
}
.tipBg{
background: azure;
opacity: 0.5;
}
22.动画及视野拓展
- 很少使用CSS写,一般用JS
- 找模板
- 卡巴斯基
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)