CSS学习知识点大全(一)

简介: 教程来源 https://app-a7illrp9pngh.appmiaoda.com/ 梳理CSS核心知识:从基础语法、四大引入方式,到选择器(基础/组合/属性/伪类/伪元素)及优先级规则;详解盒模型(标准/怪异)、属性设置与外边距折叠。助力新手构建完整体系,也为开发者提供深度参考。

CSS(层叠样式表)作为 Web 前端开发的三大核心技术之一,负责网页的视觉呈现和布局设计。从简单的文字颜色到复杂的响应式布局,从动画效果到现代 CSS 框架,CSS 已经发展成为一门强大而精妙的设计语言。本文将系统全面地梳理 CSS 的核心知识点,从基础语法到高级特性,帮助初学者建立完整的知识体系,也为有经验的开发者提供深入的技术参考。
4c88a059-1114-4b14-9039-22e0fad015d4.png

一、CSS 基础

1.1 什么是 CSS
CSS(Cascading Style Sheets) 是一种用来描述 HTML 或 XML 文档外观的样式表语言。它定义了元素如何在屏幕上显示,控制布局、颜色、字体、间距等视觉表现。

核心特性:

层叠性:多个样式可以叠加应用

继承性:子元素可以继承父元素的某些样式

优先级:当样式冲突时,根据权重决定应用哪个样式

响应式设计:根据不同设备调整布局

动画与过渡:创建平滑的视觉效果

1.2 CSS 的引入方式

<!DOCTYPE html>
<html>
<head>
    <!-- 1. 内联样式(行内样式) -->
    <div style="color: red; font-size: 16px;">内联样式</div>

    <!-- 2. 内部样式表 -->
    <style>
        .internal {
            color: blue;
            font-size: 18px;
        }
    </style>

    <!-- 3. 外部样式表(推荐) -->
    <link rel="stylesheet" href="styles.css">

    <!-- 4. @import 导入(不推荐,影响性能) -->
    <style>
        @import url('another.css');
    </style>
</head>
<body>
</body>
</html>

1.3 CSS 语法

/* 基本语法结构 */
selector {
    property: value;
    property: value;
}

/* 选择器示例 */
h1 {
    color: #333;
    font-size: 24px;
}

/* 注释 */
/* 这是 CSS 注释,只有这种格式 */

/* 多个选择器 */
h1, h2, h3 {
    font-family: 'Arial', sans-serif;
}

/* 多个属性 */
.card {
    width: 300px;
    height: 200px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

二、选择器

2.1 基础选择器

/* 元素选择器(类型选择器) */
p {
    color: #333;
}

div {
    margin: 0;
}

/* 类选择器(最常用) */
.title {
    font-size: 24px;
    font-weight: bold;
}

.card-container {
    display: flex;
    gap: 20px;
}

/* ID 选择器(唯一标识) */
#header {
    background: #f5f5f5;
    height: 60px;
}

#main-content {
    padding: 20px;
}

/* 通配符选择器 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

2.2 组合选择器

/* 后代选择器(空格) */
.container .item {
    color: red;
}

article p {
    line-height: 1.5;
}

/* 子元素选择器(>) */
.parent > .child {
    margin-left: 20px;
}

ul > li {
    list-style: none;
}

/* 相邻兄弟选择器(+) */
h1 + p {
    margin-top: 0;
}

.item + .item {
    border-top: 1px solid #eee;
}

/* 通用兄弟选择器(~) */
h1 ~ p {
    color: gray;
}

/* 分组选择器(,) */
h1, h2, .heading, #main-title {
    font-family: 'Arial', sans-serif;
}

2.3 属性选择器

/* 存在属性 */
[disabled] {
    opacity: 0.5;
    cursor: not-allowed;
}

/* 精确匹配 */
[type="text"] {
    border: 1px solid #ccc;
}

/* 包含特定值(空格分隔) */
[class~="active"] {
    background: #007bff;
}

/* 属性值开头匹配 */
[href^="https"] {
    color: green;
}

/* 属性值结尾匹配 */
[src$=".jpg"] {
    border-radius: 4px;
}

/* 属性值包含匹配 */
[title*="more"] {
    cursor: help;
}

/* 属性值以特定值开头且后面紧跟连字符 */
[lang|="en"] {
    font-family: 'Times New Roman', serif;
}

/* 示例 */
input[type="text"] {
    padding: 8px 12px;
}

a[href^="mailto:"] {
    background: url('email-icon.png') no-repeat left center;
    padding-left: 20px;
}

img[src$=".svg"] {
    width: 24px;
    height: 24px;
}

2.4 伪类选择器

/* 链接伪类 */
a:link { color: blue; }        /* 未访问链接 */
a:visited { color: purple; }   /* 已访问链接 */
a:hover { color: red; }        /* 鼠标悬停 */
a:active { color: orange; }    /* 激活状态 */

/* 表单伪类 */
input:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
}

input:checked {
    background-color: #007bff;
}

input:disabled {
    background: #f5f5f5;
    color: #999;
}

input:read-only {
    background: #f9f9f9;
}

/* 结构性伪类 */
/* :first-child - 第一个子元素 */
li:first-child {
    font-weight: bold;
}

/* :last-child - 最后一个子元素 */
li:last-child {
    border-bottom: none;
}

/* :nth-child(n) - 第 n 个子元素 */
li:nth-child(odd) { background: #f9f9f9; }      /* 奇数 */
li:nth-child(even) { background: #fff; }        /* 偶数 */
li:nth-child(3n) { color: red; }                /* 3的倍数 */
li:nth-child(3n+1) { background: #eee; }        /* 1,4,7... */

/* :nth-last-child(n) - 倒数第 n 个 */
li:nth-last-child(1) { border-bottom: none; }

/* :only-child - 唯一子元素 */
div:only-child {
    margin: 0;
}

/* :empty - 空元素 */
div:empty {
    display: none;
}

/* :not() - 否定伪类 */
:not(.active) {
    opacity: 0.5;
}

/* 目标伪类 */
:target {
    background: yellow;
    animation: highlight 1s;
}

/* 语言伪类 */
:lang(zh) {
    font-family: 'Microsoft YaHei', sans-serif;
}

2.5 伪元素选择器

/* ::before - 在元素内容前插入 */
.element::before {
    content: "★";
    color: gold;
    margin-right: 4px;
}

/* ::after - 在元素内容后插入 */
.link::after {
    content: " →";
    font-size: 12px;
}

/* ::first-letter - 首字母 */
p::first-letter {
    font-size: 2em;
    font-weight: bold;
    color: #007bff;
}

/* ::first-line - 首行 */
p::first-line {
    font-weight: bold;
    color: #333;
}

/* ::selection - 选中文本样式 */
::selection {
    background: #007bff;
    color: white;
}

/* 实用示例 */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

.custom-checkbox::before {
    content: "";
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 1px solid #ccc;
    border-radius: 3px;
    margin-right: 8px;
}

.custom-checkbox:checked::before {
    background: #007bff;
    content: "✓";
    color: white;
    text-align: center;
    line-height: 16px;
}

2.6 选择器优先级

/* 优先级计算规则(权重) */
/* 内联样式:1000 */
<div style="color: red"></div>

/* ID 选择器:100 */
#header { color: blue; }

/* 类选择器、属性选择器、伪类:10 */
.title { color: green; }
[type="text"] { color: yellow; }
:hover { color: orange; }

/* 元素选择器、伪元素:1 */
div { color: black; }
::before { content: ""; }

/* 通配符、组合器、否定伪类:0 */
* { margin: 0; }

/* 示例计算 */
#header .title h1 { }           /* 100 + 10 + 1 = 111 */
.nav ul li a:hover { }          /* 10 + 1 + 1 + 1 + 10 = 23 */
[type="text"]:focus { }         /* 10 + 10 = 20 */
div p span { }                  /* 1 + 1 + 1 = 3 */

/* !important(最高优先级,慎用) */
.element {
    color: red !important;
}

三、盒模型

3.1 标准盒模型 vs 怪异盒模型

/* 标准盒模型(content-box) */
.element {
    box-sizing: content-box;
    width: 200px;
    padding: 20px;
    border: 1px solid #000;
    margin: 10px;
    /* 实际宽度 = 200 + 40 + 2 = 242px */
}

/* 怪异盒模型(border-box) */
.element {
    box-sizing: border-box;
    width: 200px;
    padding: 20px;
    border: 1px solid #000;
    /* 实际宽度 = 200px(padding和border包含在内) */
}

/* 全局重置 */
*,
*::before,
*::after {
    box-sizing: border-box;
}

3.2 盒模型属性

.box {
    /* 宽高 */
    width: 300px;
    height: 200px;
    min-width: 100px;
    max-width: 500px;
    min-height: 50px;
    max-height: 400px;

    /* 内边距 */
    padding: 20px;                    /* 四周 */
    padding: 10px 20px;               /* 上下 左右 */
    padding: 10px 20px 30px;          /* 上 左右 下 */
    padding: 10px 20px 30px 40px;     /* 上 右 下 左 */
    padding-top: 10px;
    padding-right: 20px;
    padding-bottom: 30px;
    padding-left: 40px;

    /* 边框 */
    border: 1px solid #ccc;           /* 宽度 样式 颜色 */
    border-width: 1px 2px 3px 4px;
    border-style: solid dashed dotted double;
    border-color: red green blue yellow;

    border-top: 2px solid #333;
    border-right: 1px dashed #666;
    border-bottom: 3px dotted #999;
    border-left: none;

    border-radius: 8px;               /* 圆角 */
    border-radius: 10px 20px 30px 40px;
    border-radius: 50%;               /* 圆形 */

    /* 外边距 */
    margin: 20px;
    margin: 10px auto;                /* 水平居中 */
    margin-top: 10px;
    margin-right: auto;
    margin-bottom: 20px;
    margin-left: auto;
}

3.3 外边距折叠

/* 兄弟元素外边距折叠 */
.item {
    margin-bottom: 20px;
}
.item + .item {
    margin-top: 30px;
    /* 实际间距为 max(20px, 30px) = 30px */
}

/* 父子元素外边距折叠 */
.parent {
    margin-top: 20px;
}
.child {
    margin-top: 30px;
    /* 父元素和子元素的上外边距会合并 */
}

/* 解决方法 */
.parent {
    overflow: hidden;           /* 创建 BFC */
    padding-top: 1px;           /* 添加内边距 */
    border-top: 1px solid transparent;  /* 添加透明边框 */
    display: flex;              /* 使用 flex 布局 */
    display: grid;              /* 使用 grid 布局 */
}

来源:
https://app-a7illrp9pngh.appmiaoda.com/

相关文章
|
3月前
|
人工智能 Linux API
零基础玩转OpenClaw:阿里云计算巢+本地部署攻略(含Skills集成+避坑指南)
在AI智能体快速普及的2026年,OpenClaw(原Clawdbot,业内昵称“龙虾”)凭借强大的任务自动化执行、多模型兼容、跨场景适配能力,成为个人与轻量团队提升工作效率的核心工具。它区别于传统聊天AI,真正实现“能动手、会执行”,可通过自然语言指令完成文件管理、浏览器自动化、代码编写、数据处理等真实任务,而其模块化的Skills(技能)系统,更让AI智能体的功能得以无限延伸,适配不同领域的个性化需求。
1258 1
|
缓存 前端开发
前端代码整洁与规范之CSS篇
【4月更文挑战第2天】 前端代码整洁与规范之CSS篇
542 4
|
Android开发
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
2222 1
|
5月前
|
编译器 开发工具 C语言
【2026最新】VSCode下载、安装和使用保姆级教程(附安装包+图文步骤)
Visual Studio Code(VSCode)是微软开发的免费开源跨平台代码编辑器,轻量高效,支持50多种编程语言。通过丰富插件可扩展功能,兼具简洁与强大,广泛应用于Web开发、数据科学等领域,是全球开发者首选工具之一。
12240 7
|
10月前
|
机器学习/深度学习 传感器 人工智能
火灾火焰识别数据集(2200张图片已划分、已标注)|适用于YOLO系列深度学习分类检测任务【数据集分享】
在人工智能和计算机视觉的快速发展中,火灾检测与火焰识别逐渐成为智慧城市、公共安全和智能监控的重要研究方向。一个高质量的数据集往往是推动相关研究的核心基础。本文将详细介绍一个火灾火焰识别数据集,该数据集共包含 2200 张图片,并已按照 训练集(train)、验证集(val)、测试集(test) 划分,同时配有对应的标注文件,方便研究者快速上手模型训练与评估。
3878 10
火灾火焰识别数据集(2200张图片已划分、已标注)|适用于YOLO系列深度学习分类检测任务【数据集分享】
|
前端开发
CSS——@layer规则
CSS——@layer规则
379 2
CSS——@layer规则
|
前端开发 Windows
【前端web入门第一天】02 HTML图片标签 超链接标签 音频标签 视频标签
本文档详细介绍了HTML中的图片、超链接、音频和视频标签的使用方法。首先讲解了`&lt;img&gt;`标签的基本用法及其属性,包括如何使用相对路径和绝对路径。接着介绍了`&lt;a&gt;`标签,用于创建超链接,并展示了如何设置目标页面打开方式。最后,文档还涵盖了如何在网页中嵌入音频和视频文件,包括简化写法及常用属性。
533 13
|
SQL JavaScript 关系型数据库
connection.query()和 connection.execute()
connection.query()和 connection.execute()

热门文章

最新文章