此文章是对CSS进行的一些基础的学习。
前言
随着计算机的不断发展,Web前端这门技术也越来越重要,很多人都开启了前端的学习,本篇文章就是简单的写了一下CSS的语法,用来对CSS进行基础的学习。
一、CSS是什么?
CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。
二、CSS进阶
1.1 emmet语法
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
/* font-size: ; */
font-size: 100px;
width: 100px;
height: 100;
background-color: red;
line-height: 100px;
}
</style>
</head>
<body>
<div></div>
<h1></h1>
<!-- 生成div 带类名 -->
<div class="box"></div>
<div class="red"></div>
<div id="one"></div>
<p class="red" id="one"></p>
<p id="box"></p>
<ul>
<li></li>
</ul>
<!-- div 同级 p -->
<div></div>
<p></p>
<!-- 父子 -->
<p></p>
<div></div>
<!-- ul里有三个li -->
<ul>
<li></li>
<li></li>
</ul>
<!-- ul里面有三个li,li里面有文字111 {文字} -->
ul>li*3
</body>
</html>
代码运行结果如下:
1.2 背景色
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
/* background-color: pink; */
/* background-color: #ccc; */
/* 红绿蓝三原色,a是透明度0-1 */
/* background-color: rgba(0,0,0,0.5); */
background-color: rgba(0,0,0,.5);
}
</style>
</head>
<body>
<div>div</div>
</body>
</html>
代码运行结果如下:
1.3 背景图
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./img/04.png);
background-repeat: repeat;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
代码运行结果如下:
1.3 背景图-背景平铺
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./img/04.png);
/* background-repeat: no-repeat; */
/* background-repeat: repeat-y; */
/* background-repeat: repeat-x; */
/* background-repeat: repeat; */
}
</style>
</head>
<body>
<div></div>
</body>
</html>
代码运行结果如下:
1.4 背景位置
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
background-color: pink;
background-image: url(./img/04.png);
/* background-position: right bottom;
background-position: center; */
background-repeat: no-repeat;
/* background-position: 50px 100px; */
background-position: -50px 100px;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
代码运行结果如下:
1.5 复合属性
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 400px;
height: 400px;
/* 不分先后顺序,背景色,背景图,背景图平铺,背景图位置
background:pink url(./img/04.png) no-repeat center bottom
背景图位置如果是英文单词可以颠倒顺序*/
/* background: pink url(./img/04.png) no-repeat center bottom; */
/* 测试背景图位置如果是数值 不要颠倒顺序*/
/*水平50px 垂直100px */
/* background: pink url(./img/04.png) no-repeat 50px 100px; */
background: pink url(./img/04.png) no-repeat 100px 50px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
代码运行结果如下:
1.6 背景图和img的区别
重要的用img,不重要的用背景图
1.7显示模块-块
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 块:独占一行;宽度默认是父级100%;添加宽度都生效 */
div {
width: 300px;
height: 300px;
background-color: pink;
/* 行内块 */
display: inline-block;
}
</style>
</head>
<body>
<div>1222000</div>
<div>12112112121</div>
</body>
</html>
代码运行结果如下:
1.8 显示模块-行内
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 行内:宽高不生效,不换行,尺寸和内容的大小相同 */
span {
width: 300px;
height: 300px;
background-color: pink;
/* 行内块 */
display: inline-block;
/* 块 */
/* display: block; 转换成块级元素 */
/* display: inline-block; 转换成行内块的元素 */
/* display: inline; 转换成行内元素的属性 */
}
</style>
</head>
<body>
<span>span</span>
<span>span</span>
</body>
</html>
代码运行结果如下:
1.9 显示模式-行内块
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<img src="./img/04.png" alt="">
<img src="./img/04.png" alt="">
</body>
</html>
代码运行结果如下:
2.0 拓展-标签嵌套
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- p和h标题不能相互嵌套 -->
<!--
<p>
<h1>一级标题</h1>
</p> -->
<!-- p里面不能包含div -->
<p>
<div>divdiv</div>
</p>
</body>
</html>
代码运行结果如下:
2.1 特性-继承性
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 所有控制文字的都能继承,不是控制文字的都不能继承 */
div {
color: red;
font-size: 30px;
height: 300px;
}
</style>
</head>
<body>
<div>
这是div标签里面的文字
<span>这是div里面的span</span>
</div>
</body>
</html>
2.2 特性-继承性-注意
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
color: red;
font-size: 12px;
}
a {
color: red;
}
</style>
</head>
<body>
<div>
<a href="#">超链接</a>
<h1>一级标题</h1>
</div>
</body>
</html>
代码运行结果如下:
2.3 特性-层叠性
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
color: red;
color: green;
}
.box {
font-size: 30px;
}
</style>
</head>
<body>
<div class="box">文字</div>
</body>
</html>
代码运行结果如下:
三、综合案例
1.1 综合案例-导航栏
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* a显示模式是行内,加宽高默认不生效,转显示模式:行内块 */
a {
text-decoration: none;
width: 100px;
height: 50px;
background-color: red;
display: inline-block;
color:white;
line-height: 50px;
text-align: center;
}
a:hover {
background-color: orange;
}
</style>
</head>
<body>
<!-- alt shift 鼠标左键 多选 选多行加内容或者删除-->
<a href="#">导航1</a>
<a href="#">导航2</a>
<a href="#">导航3</a>
<a href="#">导航4</a>
<a href="#">导航5</a>
</body>
</html>
1.2 综合案例-五彩导航
这个需要五彩导航的图片,我找不到那个图片
代码示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a {
text-decoration: none;
width: 120px;
height: 58px;
background-color: pink;
display: inline-block;
text-align: center;
line-height: 50px;
color: white;
}
/* 每个a的背景图不同,单独找到每个a,设置不同的背景图片 */
.one {
background-image: url(./img/2.png);
}
.two {
background-image: url(./img/2.png);
}
.three {
background-image: url(./img/2.png);
}
.four {
background-image: url(./img/2.png);
}
.one:hover {
/* background-image: url(./img/2.png); */
background: skyblue;
}
.two:hover {
/* background-image: url(./img/2.png); */
background: red;
}
.three:hover {
/* background-image: url(./img/2.png); */
background: greenyellow;
}
.four:hover {
/* background-image: url(./img/2.png); */
background: blueviolet;
}
</style>
</head>
<body>
<a href="#" class="one">五彩导航</a>
<a href="#" class="two">五彩导航</a>
<a href="#" class="three">五彩导航</a>
<a href="#" class="four">五彩导航</a>
</body>
</html>
代码运行结果如下:
总结
以上就是CSS进阶中比较重要的语法,谢谢大家的观看。