这是我参与「第五届青训营 」笔记创作活动的第2天。
之前介绍了什么是前端与HTML
今天主要向大家讲述CSS的概念及其选择器的使用等。
课程重点
- 介绍什么是CSS?
- CSS在前端页面中的作用?
- CSS的工作原理?
- CSS在HTML文档中的位置?
- CSS中常见的选择器?
个人学习内容
什么是CSS?
- CSS 指层叠样式表 (Cascading Style Sheets)
- 样式定义如何显示 HTML 元素
- 样式通常存储在样式表中
- 把样式添加到 HTML 4.0 中,是为了解决内容与表现分离的问题
- 外部样式表可以极大提高工作效率
- 外部样式表通常存储在 CSS 文件中
- 多个样式定义可层叠为一个
CSS的工作原理
CSS的位置
<!-- 外链 -->
<link rel="stylesheet" href="/assets/style.css">
<!-- 嵌入 -->
<style>
li {margin:0; list-style:none; }
p {margin:lem 0;}
</style>
<!-- 内联 -->
<p style="margin:len 0">Example Content</p>
CSS选择器
目的:
- 找出页面中的元素,以便给他们设置样式
- 使用多种方式选择元素
分类:
- 通配符选择器
- *:选择所有标签
<h1>This is heading</h1>
<p>this is some paragraph.</p>
<style>
* {
color: red;
font-size: 20px;
}
</style>
- 标签选择器
<h1>This is heading</h1>
<p>this is some paragraph.</p>
<style>
h1 {
color: orange;
}
p {
color: gray;
font-size: 20px;
}
</style>
- id选择器
<h1 id="logo">
<img src="https://assets.codepen.io/59477/h5-logo.svg" alt="HTML5 logo" width="48" />
HTML5 文档
<h1>
<style>
#logo {
font-size: 60px;
font-weight: 200;
}
</style>
- 类选择器
<h2>Todo List</h2>
<ul>
<li class="done">Learn HTML</li>
<li class="done">Learn CSS</li>
<li>Learn JavaScript</li>
</ul>
<style>
.done {
color: gray;
text-decoration: line-through;
}
</style>
- 属性选择器
- [attribute]:选择具有某个属性的所有标签
- [attribute=value]:选择attribute值为value的所有标签
<label>用户名:</label>
<input value="zhao" disabled />
<label>密码:</label>
<input value="123456" type="password" />
<style>
[disabled] {
background: #eee;
color: #999;
}
</style>
<p>
<label>密码:</label>
<input type="password" value="123456" />
</p>
<style>
input[type="password"] {
border-color: red;
color: red;
}
</style>
<p><a href="#top">回到顶部</a></p>
<p><a href="a.jpg">查看图片</a></p>
<style>
a[href^="#"] {
color: #f54767;
background: 0 center/1em url(https://assets.codepen.io/59477/arrow-up.png) no-repeat;
padding-left: 1.1em;
}
a[href$=".jpg"] {
color: deepskyblue;
background: 0 center/1em url(https://assets.codepen.io/59477/image3.png) no-repeat;
padding-left: 1.2em;
}
</style>
CSS中的伪类
- 不基于标签和属性定位元素
- 分类:状态伪类和结构性伪类
链接伪类选择器:
- :link:链接访问前的样式
- :visited:链接访问后的样式
- :hover:鼠标悬停时的样式
- :active:鼠标点击后长按时的样式
- :focus:聚焦后的样式
目标伪类选择器:
- :target:当url指向该元素时生效。
<a href="http://example.com">
example.com
</a>
<label>
用户名:
<input type="text">
</label>
<style>
a:link {
color: black;
}
a:visited {
color: gray;
}
a:hover {
color: orange;
}
a:active {
color: red;
}
:focus {
outline: 2px solid orange;
}
</style>
位置伪类选择器:
- :nth-child(n):选择是其父标签第n个子元素的所有元素。
<ol>
<li>阿凡达</li>
<li>泰坦尼克号</li>
<li>星球大战:原力觉醒</li>
<li>复仇者联盟 3</li>
<li>侏罗纪世界</li>
</ol>
<style>
li {
list-style-position: inside;
border-bottom: 1px solid;
padding: 0.5em
}
li:first-child {
color: coral
}
li:last-child {
border-bottom: none;
}
</style>
<label>
用户名:
<input class="error" value="aa">
</label>
<span class="error">
最少3个字符
</span>
<style>
.error {
color: red;
}
input.error {
border-color: red;
}
</style>
伪元素选择器:
将特定内容当做一个元素,选择这些元素的选择器被称为伪元素选择器。
- ::first-letter:选择第一个字母
- ::first-line:选择第一行
- ::selection:选择已被选中的内容
- ::after:可以在元素后插入内容
- ::before:可以在元素前插入内容
CSS中的组合选择器
| 名称 | 语法 | 说明 | 示例 |
|---|---|---|---|
| 直接组合 | AB | 满足A同时满足B | input:focus |
| 后代组合 | A B | 选中B,如果它是A的子孙 | nav a |
| 亲子组合 | A>B | 选中B,如果它B的子元素 | section>p |
| 兄弟组合 | A~B | 选中B,如果它在A后并且与A同级 | h2~p |
| 相邻选择器 | A+B | 选中B,如果它紧跟在A后边 | h2+p |
<article>
<h1>拉森火山国家公园</h1>
<p>拉森火山国家公园是位于...</p>
<section>
<h2>气候</h2>
<p>因为拉森火山国家公园...</p>
<p>高于这个高度,气候非常寒冷...</p>
</section>
</article>
<style>
article p {
color: black;
}
article > p {
color: blue;
}
h2 + p {
color: red;
}
</style>
样式渲染的优先级
- 权重大小,越具体的选择器权重越大:!important > 行内样式 > ID选择器 > 类与伪类选择器 > 标签选择器 > 通用选择器
- 权重相同时,后面的样式会覆盖前面的样式
- 继承自父元素的权重最低
个人总结
本节可主要学习了CSS的基础概念,明白了CSS在前端页面中的作用,了解了简单的选择器的使用等。