1.CSS的简单介绍
推荐网站:
- https://www.runoob.com 菜鸟教程
重点:CSS选择器
CSS Cascading Style Sheet 层叠级联样式表
作用:美化网页
2.四种CSS导入方式
方式1:内部样式表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范 <style> 可以编写CSS的代码
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
color: antiquewhite;
}
</style>
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
方式2:外部样式表(推荐)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="CSS/style.css"> 调用css
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
CSS/style.css
h1{
color: antiquewhite;
}
Markdown加粗快捷键:Ctrl + B
方式3:行内样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--行内样式:在标签元素中,编写style属性,样式即可-->
<h1 style="color: antiquewhite">我是标题</h1>
</body>
</html>
方式4:导入样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
@import "CSS/style.css";
</style>
</head>
<body>
<h1>说java</h1>
</body>
</html>
h1{
color: antiquewhite;
}
3.选择器
作用:选择页面上的某一个或者某一类元素
3.1 基本选择器(重要)
1.标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*
标签选择器:会选择到页面上所有的这个标签的元素
*/
h1{
color:#dadd;
}
p{
color:#143245;
background: aquamarine;
font-size:80px;
}
</style>
</head>
<body>
<h1>学Java</h1>
<h1>学Java</h1>
<p>OKK</p>
</body>
</html>
2.类选择器:选择所有class属性一致的标签,跨标签 .类名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*
类选择器的格式: .Class的名称{}
好处:可以多个标签归类,是同一个Class
*/
.xiao{
color:#143245;
}
.chao{
color: antiquewhite;
}
.Li{
color: bisque;
}
</style>
</head>
<body>
<!--尽量见名知意-->
<h1 class="xiao">标题1</h1>
<h1 class="chao">标题2</h1>
<h1 class="Li">标题3</h1>
<p class="Li">6666</p>
</body>
</html>
3.Id选择器:全局唯一! #id名称{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*
id必须保证全局唯一
id选择器:
#id的名称{}
*/
#xiao{
color: antiquewhite;
}
</style>
</head>
<body>
<h1 id="xiao">标题1</h1>
<h1>标题2</h1>
<h1>标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>
</body>
</html>
**优先级:**id选择器>class选择器>标签选择器
3.2 层次选择器
以祖爷爷为基准
-
后代选择器
在某个元素后面 祖爷爷 爷爷 爸爸 我
/*后代选择器*/ body p{ background: antiquewhite; }
-
子选择器
只有一代 祖爷爷 爷爷 爸爸 我
/*子选择器*/ body>p{ background: #143245; }
-
相邻兄弟(弟弟)选择器
同辈 相邻的下一个 祖一爷爷 祖爷爷 祖二爷 爷爷 爸爸 我
/*相邻兄弟选择器*/ .active + p{ background: chartreuse; }
-
通用(所有弟弟)选择器
同辈 下面若干 祖一爷爷 祖爷爷 祖二爷 祖三爷 祖四爷 爷爷 爸爸 我
/*通用兄弟选择器*/ .active~p{ background: darkblue; }
3.3 结构伪类选择器
伪类:带**:**
/*ul的第一个子元素*/
ul li:first-child{
background: antiquewhite;
}
/*ul的最后子元素*/
ul li:last-child{
background: #0c370b;
}
/*选中p1: 定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效 不分类型
*/
p:nth-child(1){
background: chartreuse;
}
/*选中父元素,下的p元素的第二个,类型 分类型*/
p:nth-of-type(2){
background: darkblue;
}
3.4 属性选择器(重要 重要)
<!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: #2700ff;
text-align: center;
color: gainsboro;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/*存在id属性的元素
a[]{}
= 绝对等于
*= 包含这个元素
^= 以这个元素开头
$= 以这个元素结尾
*/
/*a[id]{*/
/* background: yellow;*/
/*}*/
/*a[id=first]{*/
/* background: blue;*/
/*}*/
/* id等于first的元素 */
/* a[id=first]{*/
/* background: yellow;*/
/* }*/
/*class中有links的元素*/
/*a[class*=links]{*/
/* background: antiquewhite;*/
/*}*/
/* 选中href中以http开头的元素*/
/* a[href^=http]{*/
/* background: yellow;*/
/* }*/
a[href$=pdf]{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1</a>
<a href="http://blog.kuangstudy.com" 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>
</html>
a[]{}
= 绝对等于
*= 包含这个元素
^= 以这个元素开头
$= 以这个元素结尾
4.美化网页元素
span标签:重点要突出的字,使用span套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title{
font-size: 50px;
}
</style>
</head>
<body>
欢迎进入<span id="title">Java</span>
</body>
</html>
4.1字体样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*
font-family:字体
font-size:字体大小
font-weight:字体的粗细
color:字体颜色
*/
body{
font-family:楷体,monospace;
}
h1{
font-size: 50px;
}
.p1{
font-weight: bold;
}
</style>
</head>
<body>
<h1>简介:</h1>
<p class="p1">故事从主人公漩涡鸣人的孤儿生活开始,他的父母为了保护村子,将攻击村子九尾妖狐封印到了他体内,鸣人因此受尽了村人</p>
<p>的冷落,只是拼命用各种恶作剧试图吸引大家的注意力。好在还是有依鲁卡老师的关心,鸣人的性格才没有变得扭曲,他总是干劲十足、非常乐观。 为了让更多的人认可自己,鸣人的目标是成为火影。整个故事就围绕鸣人的奋斗、成长,鸣人的同伴们的故事,以及这个忍者世界的各种争斗和阴谋展开。</p>
<p>About CCTV.com CCTV.com is constantly updated with top news from China and around the world. Offering news reports, live and on-demand video content and searchable archives, CCTV.com is</p>
</body>
</html>
4.2 文本样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
颜色(color): 单词 十六进制(RGB) rgb(a,b,c)/rgba(a,b,c,d)
text-align: 排版居中
text-indent: 2px 首行缩进
text-decoration: 文本修饰
line-height: 行高
-->
<style>
h1{
color: yellow;
text-align: center;
}
.p1{
text-indent: 2px;
text-decoration: underline;
}
</style>
</head>
<body>
<h1>简介:</h1>
<p class="p1">故事从主人公漩涡鸣人的孤儿生活开始,他的父母为了保护村子,将攻击村子九尾妖狐封印到了他体内,鸣人因此受尽了村人</p>
<p id="l2">的冷落,只是拼命用各种恶作剧试图吸引大家的注意力。好在还是有依鲁卡老师的关心,鸣人的性格才没有变得扭曲,他总是干劲十足、非常乐观。 为了让更多的人认可自己,鸣人的目标是成为火影。整个故事就围绕鸣人的奋斗、成长,鸣人的同伴们的故事,以及这个忍者世界的各种争斗和阴谋展开。</p>
<p>About CCTV.com CCTV.com is constantly updated with top news from China and around the world. Offering news reports, live and on-demand video content and searchable archives, CCTV.com is</p>
</body>
</html>
<!--
颜色(color): 单词 十六进制(RGB) rgb(a,b,c)/rgba(a,b,c,d)
text-align: 排版居中
text-indent: 2px 首行缩进
text-decoration: 文本修饰
line-height: 行高
-->
4.3文本阴影与超链接伪类
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
text-decoration: none;
}
/*鼠标悬停时的状态*/
p:hover {
color:coral;
font-size: 30px;
}
/*鼠标按下时未释放的状态*/
p:active{
color:darkslategrey;
}
/* 阴影颜色 水平偏移 垂直偏移 阴影半径*/
#yin{
text-shadow: #2365a7 10px 10px 2px;
} </style>
</head>
<body>
<a href="">
<img src="img/stone.png" alt="" width="600" height="600">
</a>
<p>纸盒人</p>
<p>灰暗系列</p>
<p id="yin">阴郁</p></body>
/*鼠标悬停时的状态*/
p:hover {
color:coral;
font-size: 30px;
}
/*鼠标按下时未释放的状态*/
p:active{
color:darkslategrey;
}
/* 阴影颜色 水平偏移 垂直偏移 阴影半径*/
#yin{
text-shadow: #2365a7 10px 10px 2px;
}
4.4 列表样式标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="div">
<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>
#div{
width:300px;
background: aliceblue;
}
.title{
font-size:30px;
text-indent:1em;
line-height: 30px;
font-weight: bold;
background: red;
}
/*ul li
none:去掉远点
circle 空心圆
decimal 有序数字
square 正方形
*/
ul{
}
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
}
a{
text-decoration: none;
font-size: 14px;
color: #000;
}
a:hover{
color: orange;
text-decoration: underline;
}
4.5 背景
-
背景颜色
-
背景图片
<style>
div{
width:1000px;
height: 700px;
border: 1px solid red;
background-image: url("images/1.png");
/*默认平铺 repeat*/
}
.div1{
background-repeat: repeat-x; /*x轴平铺*/
}
#div2{
background-repeat: repeat-y; /*y轴平铺*/
}
.div3{
background-repeat:no-repeat; /*不平铺*/
}
</style>
<body>
<div class="div1"></div>
<div id="div2"></div>
<div class="div3"></div>
</body>
/* 颜色 图片 图片位置 平铺方式 */
background:red url("./images/d.gif") 270px 10px no-repeat;
background-image:url("../images/r.gif");
background-repeat:no-respeat;
background-position:150px 20px
渐变素材网站:https://www.grabient.com/
background-color: #4158D0;
background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
5.盒子模型
5.1 什么是盒子模型
CSS盒模型包括:边距,边框,填充,和实际内容
不同部分的说明:
- Margin - 清除边框区域。Margin没有背景颜色,它是完全透明
- Border - 边框周围的填充和内容。边框是受到盒子的背景颜色影响
- Padding - 清除内容周围的区域。会受到框中填充的背景颜色影响
- Content - 盒子的内容,显示文本和图像
重要的是:当您指定一个CSS元素的宽度和高度属性时,你只是设置内容区域的宽度和高度。要知道,完全大小的元素,你还必须添加填充,边框和边距。
总元素的高度=高度+顶部填充+底部填充+上边框+下边框+上边距+下边距
总元素的宽度=宽度+左填充+右填充+左边框+右边框+左边距+右边距
/*设置总宽度为250像素的元素*/
width:220;
padding:10px;
border:5px solid gray;
margin:0px;
5.1.1 边框(border)
边框样式属性指定要显示什么样的边界
/* 粗细 样式 颜色 */
/* 合 */
border:1px solid red;
/* 分 */
border-width
border-style
border-color
-
边框的粗细
border-width 属性为边框指定宽度
- 指定长度值:2px 或 0.1em
- 3个关键字:thin 、medium(默认值) 和 thick
-
边框的样式
- 边框颜色
border-color属性用于设置边框的颜色:
- name - 指定颜色的名称,如 “red”
- RGB - 指定 RGB 值, 如 “rgb(255,0,0)”
- Hex - 指定16进制值, 如 “#ff0000”
5.1.2 外边距(margin)
margin:25px 50px 75px 100px; /* 顺时针 */
- 上边距:25px
- 右边距:50px
- 下边距:75px
- 左边距:100px
5.1.5 填充(padding)
padding:25px 50px 75px 100px; /* 顺时针 */
- 上填充:25px
- 右填充:50px
- 下填充:75px
- 左填充:100px
5.2 圆角边框
border-radius属性被用于创建圆角
<style>
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 50px;
}
</style>
5.3 阴影
5.3.1 文本阴影
text-shadow属性适用于文本阴影。
指定了水平阴影,垂直阴影,模糊的距离,以及阴影的颜色*:
h1
{
text-shadow: 5px 5px 5px #FF0000;
}
5.3.2 盒阴影
box-shadow属性被用来添加阴影:
div
{
box-shadow: 10px 10px 5px #888888;
}
5.4 Display and Visibility
隐藏一个元素可以通过display属性设置为"none"或"hidden"属性的可见性。
visibility:hidden可以让您隐藏某个元素,但它仍然需要像以前一样的空间。该元素将被隐藏,但仍然会影响布局
h1.hidden {visibility:hidden;}
display:隐藏的元素,它不会占用任何空间。
h1.hidden {display:none;}
5.5 Float(浮动)
Float(浮动):元素可以围绕其他元素向左或向右被推动
img
{
float:right;
}
Idea删除一行代码快捷键:Ctrl + X
6.定位
6.1 相对定位
#first{
position:relative; /* 相对于原来的自己的位置*/
top:-20px;
left:20px;
bottom:20px;
right:15px;
}
6.2 绝对定位
- 没有父级元素定位的前提下,相对于浏览器定位
- 假设父级元素存在定位,通常会相对于父级进行定位
- 在父级元素范围内移动
#first{
position:absolute;
top:-20px;
left:20px;
bottom:20px;
right:15px;
}
6.3 固定定位
#first{
position:fixed;
}