CSS - 学习笔记
<h1 id="css---学习笔记">CSS - 学习笔记</h1><h2 id="一什么是css">一、什么是CSS</h2>
<ul>
<li>Cascading Style Sheet 层叠级联样式表</li>
<li>CSS : 表现(美化网页)
<ul>
<li>字体,颜色,边距,高度,宽度,背景图片……</li>
</ul>
</li>
</ul>
<h2 id="二css-基本语法">二、CSS 基本语法</h2>
<pre><code class="language-html"> <!-- 规范 style type="text/css" 可以编写css的代码
每一个声明使用分号结尾
语法:
选择器 {
声明1;
}
-->
<style type="text/css">
h1{
color: red;
}
</style>
</code></pre>
<p>style.css</p>
<pre><code class="language-css">h1{
color: red;
}
</code></pre>
<p>HTML 引入 CSS 文件</p>
<pre><code class="language-html"><link rel="stylesheet" type="text/css" href="css/style.css">
</code></pre>
<p><strong>CSS 的优势:</strong></p>
<ul>
<li>内容和表现分离</li>
<li>网页结构统一,可以实现复用</li>
<li>样式十分丰富</li>
<li>建议使用独立 html 的 css 文件</li>
<li>利于 SEO,更容易被搜索引擎收录</li>
</ul>
<p><strong>CSS 三种导入方式:</strong></p>
<ul>
<li>
<p>行内样式</p>
<pre><code class="language-html"><h1 style="color:blue">我是标题一</h1>
</code></pre>
</li>
<li>
<p>内部样式</p>
<pre><code class="language-html"><stype>
h1{
color : red;
}
</stype>
</code></pre>
</li>
<li>
<p>外部样式</p>
<pre><code class="language-css">h1{
color: red;
}
</code></pre>
</li>
</ul>
<p><strong>优先级 : 就近原则</strong></p>
<h2 id="三选择器">三、选择器</h2>
<blockquote>
<p>选择器作用:选择页面上的某一种或者某一个元素</p>
</blockquote>
<h3 id="31-基本选择器">3.1 基本选择器</h3>
<ul>
<li>
<p>标签选择器 : 选择一类标签</p>
<pre><code class="language-html"> <style type="text/css">
/* 标签选择器 会选择到这个页面上所有的这个标签的元素 */
h1 {
color: red;
background: wheat;
border-radius: 10px;
}
p{
font-size: 80px;
color: blue;
}
</style>
</code></pre>
</li>
<li>
<p>类选择器 : 选中所有 class属性一致的标签</p>
<pre><code class="language-HTML"> <style type="text/css">
/* 类选择器的格式 : .class的名称{}
有点:可以多个标签归类,是同一个class
*/
.hello{
color: red;
}
.title{
color: wheat;
}
</style>
<h1 class="hello">标题一</h1>
<h1 class="title">标题二</h1>
<h1 class="hello">标题三</h1>
<p class="title"> 我是P标签</p>
</code></pre>
</li>
<li>
<p>ID 选择器 : 全局唯一</p>
<pre><code class="language-HTML"> <style type="text/css">
/* ID选择器 : id必须保证全局唯一
#id名称{}
不遵循就近原则,是固定的
id选择器 > class选择器 > 标签选择器
*/
#title {
color: pink;
}
</style>
<h1 id="title">标题一</h1>
<h1>标题二</h1>
<h1>标题三</h1>
<h1>标题四</h1>
<h1>标题五</h1>
</code></pre>
</li>
</ul>
<p><strong>优先级:id选择器 > class选择器 > 标签选择器</strong></p>
<h3 id="32-层次选择器">3.2 层次选择器</h3>
<ul>
<li>
<p>后代选择器</p>
<pre><code class="language-css">/* 后代选择器 */
body p{
background: red;
}
</code></pre>
</li>
<li>
<p>子选择器</p>
<pre><code class="language-css">/* 子选择器 */
body >p{
background: wheat;
}
</code></pre>
</li>
<li>
<p>相邻兄弟选择器</p>
<pre><code class="language-css">/* 相邻兄弟选择器 : 只有一个,向下 */
.active + p{
background: red;
}
</code></pre>
</li>
<li>
<p>通用选择器</p>
<pre><code class="language-CSS">/* 通用选择器 : 当前选中元素向下的所有兄弟元素 */
.active ~p{
background: green;
}
</code></pre>
</li>
</ul>
<h3 id="33-结构伪类选择器">3.3 结构伪类选择器</h3>
<pre><code class="language-css">/* ul 的第一个子元素 */
ul li:first-child{
color: red;
}
/* ul 的最后一个子元素 */
ul li:last-child{
background: green;
}
</code></pre>
<h3 id="34-属性选择器">3.4 属性选择器</h3>
<pre><code class="language-html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>属性选择器</title>
<style type="text/css">
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: blue;
text-align: center;
color: gainsboro;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/* 存在id属性的元素 属性选择器 a[]{} */
/* a{
background: yellow;
}*/
a{
background: yellow;
}
/*
=号 绝对等于
*= 包含等于
^= 以这个开头
$= 以这个结尾
*/
a{
background: green;
}
/* 选中href中 http开头的元素 */
a{
background: wheat;
}
/* 选中href中 pdf结尾的元素 */
a{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1</a>
<a href="" class="links item active" target="_blank" title="Hello">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>
</code></pre>
<pre><code>= 绝对等于
*= 包含等于
^= 以这个开头
$= 以这个结尾
</code></pre>
<h2 id="四美化网页元素">四、美化网页元素</h2>
<ul>
<li>有效的传递页面信息</li>
<li>美化页面,才能吸引用户</li>
<li>凸显页面的主题</li>
<li>提高用户的体验</li>
</ul>
<p><code>span</code> 标签</p>
<pre><code class="language-HTML"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>span标签</title>
<style type="text/css">
#title1{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习<span id="title1">CSS</span>
</body>
</html>
</code></pre>
<h3 id="41字体样式">4.1字体样式</h3>
<pre><code class="language-HTML"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>字体样式</title>
<!--
font-family: 字体
font-size: 字体大小
font-weight: 字体粗细
color: 字体颜色
-->
<style type="text/css">
body{
font-family: 楷体;
}
h1{
font-size: 50px;
}
.p1{
font-weight: bold;
color: wheat;
}
</style>
</head>
<body>
<h1>百度百科:</h1>
<p class="p1">百度百科是百度公司推出的一部内容开放、自由的网络百科全书。其测试版于2006年4月20日上线,正式版在2008年4月21日发布</p>
<p>截至2020年10月,百度百科已经收录了超2100万个词条,参与词条编辑的网友超过717万人,几乎涵盖了所有已知的知识领域</p>
<p>Great minds have purpose, others have wishes.</p>
</body>
</html>
</code></pre>
<h3 id="42-文本样式">4.2 文本样式</h3>
<ul>
<li>
<p>颜色 : <code>color</code></p>
</li>
<li>
<p>对齐方式 : <code>text-align</code></p>
</li>
<li>
<p>首行缩进 : <code>text-indent</code></p>
</li>
<li>
<p>行高 : <code>line-height</code></p>
</li>
<li>
<p>装饰</p>
<pre><code class="language-css">下划线:text-decoration: underline;
删除线:text-decoration: line-through;
上划线:text-decoration: overline;
去除下划线:text-decoration: none;
</code></pre>
</li>
</ul>
<pre><code class="language-HTML"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文本样式</title>
<!--
颜色:
单次
RGB
RGBA
text-align :排版
text-indent: 2em; 首行缩进
-->
<style>
h1{
color: red;
text-align: center;
}
.p1{
text-indent: 2em;
}
.p2{
background: wheat;
line-height: 300px;
}
/*下划线*/
.l1{
text-decoration: underline;
}
/*中划线*/
.l2{
text-decoration: line-through;
}
/*上划线*/
.l3{
text-decoration: overline;
}
/* a标签去除下划线 */
a{
text-decoration: none;
}
</style>
</head>
<body>
<a href="">123123</a>
<p class="l1">1231231231312</p>
<p class="l2">6666655555555</p>
<p class="l3">78978978978978978</p>
<h1>百度百科</h1>
<p class="p1">百度百科是百度公司推出的一部内容开放、自由的网络百科全书。其测试版于2006年4月20日上线,正式版在2008年4月21日发布</p>
<p class="p2">截至2020年10月,百度百科已经收录了超2100万个词条,参与词条编辑的网友超过717万人,几乎涵盖了所有已知的知识领域</p>
<p>Great minds have purpose, others have wishes.</p>
</body>
</html>
</code></pre>
<h3 id="43-文本阴影和超链接伪类">4.3 文本阴影和超链接伪类</h3>
<p><strong>阴影</strong></p>
<pre><code class="language-css">/* text-shadow : 阴影颜色 水平偏移 垂直偏移 阴影半径 */
#price{
text-shadow: #6e6ea9 10px 10px 10px;
}
</code></pre>
<p><strong>超链接伪类</strong></p>
<pre><code class="language-css">/* 默认的颜色 */
a{
text-decoration: none;
color: black;
}
/* 鼠标悬浮的颜色 */
a:hover{
color: orange;
}
/* 鼠标按住未释放的状态 */
a:active{
color: green;
}
</code></pre>
<h3 id="44-列表">4.4 列表</h3>
<pre><code class="language-CSS">/*
list-style:
none 去除圆点
circle空心圆
decimal 数字
square正方形
*/
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
}
</code></pre>
<h3 id="45-背景">4.5 背景</h3>
<ul>
<li>
<p>背景颜色</p>
</li>
<li>
<p>背景图片</p>
</li>
</ul>
<pre><code class="language-css"> <style>
div{
width: 1000px;
height: 700px;
border: 1px solid red;
/* 默认是平铺 */
background-image: url("images/txc.JPG");
}
.div1{
background-repeat: repeat-x;
}
.div2{
background-repeat: repeat-y;
}
.div3{
background-repeat:no-repeat;
}
</style>
</code></pre>
<h2 id="五盒子模型">五、盒子模型</h2>
<ul>
<li>Margin 外边距</li>
<li>Border 边框</li>
<li>Padding内边距</li>
<li>Content 内容</li>
</ul>
<h3 id="51-边框">5.1 边框</h3>
<pre><code class="language-css"><style>
/*总有一个外边距 margin 默认为 0*/
body{
margin: 0;
padding: 0;
text-decoration: none;
}
/*border: 粗细 样式 颜色*/
#app{
width: 300px;
border: 1px solid red;
}
h2{
font-size: 16px;
background: aqua;
line-height: 30px;
}
form{
background: aquamarine;
}
div:nth-of-type(1) input{
border: 3px solid black;
}
div:nth-of-type(2) input{
border: 3px dashed wheat;
}
div:nth-of-type(2) input{
border: 3px dashed green;
}
</style>
</code></pre>
<h3 id="52-外边框和内边距">5.2 外边框和内边距</h3>
<blockquote>
<p>外边框的妙用:居中元素<br>
margin: 0 设置全部方位都为0<br>
margin: 0 auto; 设置上下边距为0,左右为自动对齐</p>
</blockquote>
<pre><code class="language-HTML"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>外边距</title>
<!--
外边框的妙用:居中元素
margin: 0 设置全部方位都为0
margin: 0 auto; 设置上下边距为0,左右为自动对齐
-->
<style>
/*border: 粗细 样式 颜色*/
#app{
width: 300px;
border: 1px solid red;
margin: 0 auto;
}
h2{
font-size: 16px;
background: aqua;
line-height: 30px;
margin-top: 0;
margin-bottom: 0;
}
form{
background: aquamarine;
}
input{
border: 1px solid black;
}
</style>
</head>
<body>
<div id="app">
<h2>会员登录</h2>
<form action="#">
<div>
<span>用户名:</span>
<input type="text">
</div>
<div>
<span>密码:</span>
<input type="text">
</div>
<div>
<span>邮箱:</span>
<input type="text">
</div>
</form>
</div>
</body>
</html>
</code></pre>
<h3 id="53-圆角边框">5.3 圆角边框</h3>
<pre><code class="language-html"><!--
4个角
顺时针:左上 右上 右下 左下
border-radius
-->
<style>
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 100px;
}
</style>
</code></pre>
<h3 id="54-阴影">5.4 阴影</h3>
<pre><code class="language-CSS">/*
box-shadow: 10px 10px 50px #1d1c1c;
*/
img{
width: 200px;
height: 200px;
border-radius: 562.6px;
box-shadow: 10px 10px 50px #1d1c1c;
margin-bottom: 120px;
}
</code></pre>
<h2 id="六浮动">六、浮动</h2>
<h3 id="61-标准文档流">6.1 标准文档流</h3>
<p>块元素:独占一行</p>
<pre><code class="language-HTML">h1 ~h6 p div ……
</code></pre>
<p>行内元素:不独占一行</p>
<pre><code class="language-html">span a img ……
</code></pre>
<p>行内元素可以被包含在块元素中</p>
<h3 id="62-display">6.2 display</h3>
<p>这个也是一种实现行内元素排列的方式</p>
<pre><code class="language-css"><!--
block 块元素
inline 行内元素
inline-block 是块元素,但是可以内联,在一行
-->
<style>
div{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline;
}
span{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
</style>
</code></pre>
<h3 id="63-float">6.3 float</h3>
<pre><code class="language-css">div{
margin: 10px;
padding: 5px;
}
img{
width: 50px;
height: 130px;
}
#father{
border: 1px solid red;
}
.layer01{
border: 1px solid wheat;
display: inline-block;
float: right;
}
.layer02{
border: 1px solid wheat;
display: inline-block;
float: right;
}
.layer03{
border: 1px solid wheat;
display: inline-block;
float: right;
}
.layer04{
border: 1px solid wheat;
display: inline-block;
font-size: 12px;
line-height: 23px;
float: right;
clear:both;
}
</code></pre>
<h3 id="64-父级边框塌陷的问题">6.4 父级边框塌陷的问题</h3>
<ul>
<li>
<p>clear</p>
<pre><code class="language-css">/*
clear:left 左侧不允许有浮动
clear:right 右侧不允许有浮动
clear:both 两侧不允许有浮动
clear:none
*/
</code></pre>
</li>
</ul>
<p>解决方案:</p>
<ul>
<li>
<p>增加父级元素的高度</p>
<pre><code class="language-css">#father{
border: 1px solid red;
height: 800px;
}
</code></pre>
</li>
<li>
<p>增加一个空 <code>div</code> 标签,清除浮动</p>
<pre><code class="language-html"><div class="clear"></div>
.clear{
clear: both;
margin: 0;
padding: 0;
}
</code></pre>
</li>
<li>
<p>overflow</p>
<blockquote>
<p>在父级元素中增加一个<code>overflow : hidden;</code></p>
</blockquote>
</li>
<li>
<p>父类添加一个伪类:after</p>
<pre><code class="language-css">#father:after{
content: '';
display: block;
clear: both;
}
</code></pre>
</li>
</ul>
<h2 id="七定位">七、定位</h2>
<h3 id="71-相对定位">7.1 相对定位</h3>
<ul>
<li>相对定位 <code>position:relative</code><br>
相对于原来的位置进行偏移</li>
</ul>
<pre><code class="language-css">上:top
下:bottom
左:left
右:right
</code></pre>
<pre><code class="language-HTML"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
相对定位 position
相对于自己原来的位置进行偏移
-->
<style>
body{
margin: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid red;
padding: 0;
}
#first{
border: 1px dashed orange;
position: relative; /* 相对定位,上下左右 */
top: -20px;
}
#second{
border: 1px dashed wheat;
}
#third{
border: 1px dashed green;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
</code></pre>
<h3 id="72-绝对定位">7.2 绝对定位</h3>
<ul>
<li>绝对定位
<ul>
<li>没有父级元素定位的前提下,相对于浏览器定位</li>
<li>父级元素存在定位,相对于父级元素进行偏移</li>
</ul>
</li>
</ul>
<pre><code class="language-html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
<!--
绝对定位 position:absolute
-->
<style>
body{
margin: 20px;
}
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid red;
padding: 0;
position:relative;
}
#first{
border: 1px dashed orange;
}
#second{
border: 1px dashed wheat;
position: absolute;
right: 50px;
top: -10px;
}
#third{
border: 1px dashed green;
}
</style>
</head>
<body>
<div id="father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
</code></pre>
<h3 id="73-固定定位">7.3 固定定位</h3>
<p>固定定位 fixed</p>
<h3 id="73-z-index">7.3 z-index</h3>
<pre><code class="language-CSS">#content{
width: 350px;
padding: 0;
margin: 0;
overflow: hidden;
font-size: 12px;
line-height: 25px;
border: 1px solid black;
}
ul,li{
margin: 0;
padding: 0;
list-style: none;
color:orange;
}
img{
width: 280px;
height: 260px;
}
/* 父级元素相对定位 */
#content ul{
position: relative;
}
.tipText{
position: absolute;
width: 350px;
height: 25px;
top: 190px;
left: 100px;
}
/* zindex:默认是0,最高无限 */
.tipText{
z-index: 999;
}
.tipBg{
position: absolute;
width: 350px;
height: 25px;
background: black;
top: 190px;
opacity: 0.5; /* 背景透明度 */
}
</code></pre>
<p>HTML:</p>
<pre><code class="language-HTML"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Z-index</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="content">
<ul>
<li><img src="images/bg.jpg" alt=""></li>
<li class="tipText">学习前端-CSS</li>
<li class="tipBg"></li>
<li>时间:2022-01-01</li>
<li>地点:ShangHai</li>
</ul>
</div>
</body>
</html>
</code></pre><br><br>
来源:https://www.cnblogs.com/ZunSir/p/16243875.html
頁:
[1]