HTML 和 CSS 基础教程
HTML 和 CSS 是构建网页的两大核心技术,HTML 负责内容结构,CSS 负责样式美化。以下是它们的详细基础知识:
一、HTML 基础回顾
HTML(HyperText Markup Language,超文本标记语言)用标签构建网页结构。
1. 基本结构
<!DOCTYPE html>
<html>
<head>
<title>页面标题</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="页面描述">
<link rel="stylesheet" href="styles.css"> <!-- 引入外部CSS -->
<style>
/* 内部CSS样式 */
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- 页面主要内容 -->
<header>
<nav>
<!-- 导航内容 -->
</nav>
</header>
<main>
<!-- 主体内容 -->
</main>
<footer>
<!-- 页脚内容 -->
</footer>
<script src="script.js"></script> <!-- 引入JavaScript -->
</body>
</html>
2. 常用标签详解
文本标签
<!-- 标题标签 -->
<h1>一级标题</h1>
<h2>二级标题</h2>
<!-- ... -->
<h6>六级标题</h6>
<!-- 段落 -->
<p>这是一个段落,用于展示文本内容。</p>
<!-- 强调文本 -->
<strong>加粗强调</strong>
<em>斜体强调</em>
<!-- 换行 -->
<p>第一行<br>第二行</p>
<!-- 水平线 -->
<hr>
列表标签
<!-- 无序列表 -->
<ul>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
</ul>
<!-- 有序列表 -->
<ol>
<li>第一项</li>
<li>第二项</li>
</ol>
<!-- 定义列表 -->
<dl>
<dt>术语</dt>
<dd>术语的定义</dd>
</dl>
链接和图像
<!-- 绝对路径链接 -->
<a href="https://example.com" target="_blank">外部链接</a>
<!-- 相对路径链接 -->
<a href="about.html">关于我们</a>
<!-- 锚点链接 -->
<a href="#section1">跳转到章节1</a>
<section id="section1">...</section>
<!-- 图片 -->
<img src="image.jpg" alt="图片描述" width="300" height="200">
表格
<table border="1">
<caption>表格标题</caption>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">总计: 1人</td>
</tr>
</tfoot>
</table>
表单
<form action="/submit" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" minlength="6">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email">
<label for="gender">性别:</label>
<select id="gender" name="gender">
<option value="male">男</option>
<option value="female">女</option>
</select>
<label>兴趣:</label>
<input type="checkbox" id="coding" name="interest" value="coding">
<label for="coding">编程</label>
<input type="radio" id="student" name="occupation" value="student" checked>
<label for="student">学生</label>
<label for="bio">个人简介:</label>
<textarea id="bio" name="bio" rows="4"></textarea>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
二、CSS 基础详解
1. CSS 引入方式
内联样式(不推荐)
<p style="color: red; font-size: 16px;">红色文本</p>
内部样式表
<head>
<style>
body {
font-family: 'Arial', sans-serif;
line-height: 1.6;
}
h1 {
color: #333;
}
</style>
</head>
外部样式表(最佳实践)
<head>
<link rel="stylesheet" href="css/styles.css">
</head>
2. CSS 基本语法
/* 注释 */
selector {
property: value;
another-property: value;
}
/* 示例 */
p {
color: #333;
font-size: 16px;
line-height: 1.5;
margin-bottom: 20px;
}
3. CSS 选择器进阶
基本选择器
/* 元素选择器 */
div {
border: 1px solid #ccc;
}
/* 类选择器 */
.error {
color: red;
}
/* ID选择器 */
#main-header {
background: #333;
}
/* 通用选择器 */
* {
box-sizing: border-box;
}
属性选择器
/* 存在属性 */
a[target] {
color: green;
}
/* 属性值匹配 */
input[type="text"] {
border: 1px solid blue;
}
/* 属性值包含 */
a[href*="example"] {
color: purple;
}
/* 属性值开头 */
a[href^="https"] {
font-weight: bold;
}
/* 属性值结尾 */
a[href$=".pdf"] {
background: url(pdf-icon.png);
}
伪类选择器
/* 链接伪类 */
a:link { color: blue; }
a:visited { color: purple; }
a:hover { text-decoration: underline; }
a:active { color: red; }
/* 表单伪类 */
input:focus {
outline: 2px solid orange;
}
input:disabled {
opacity: 0.5;
}
/* 结构伪类 */
li:first-child { font-weight: bold; }
li:nth-child(odd) { background: #f0f0f0; }
li:last-child { border-bottom: none; }
伪元素选择器
/* 首字母 */
p::first-letter {
font-size: 200%;
float: left;
}
/* 首行 */
p::first-line {
font-weight: bold;
}
/* 前后内容 */
.price::before {
content: "¥";
}
/* 选中文本 */
::selection {
background: yellow;
color: black;
}
组合选择器
/* 后代选择器 */
article p {
text-indent: 2em;
}
/* 子元素选择器 */
ul > li {
border-bottom: 1px dotted #ccc;
}
/* 相邻兄弟选择器 */
h2 + p {
margin-top: 0;
}
/* 通用兄弟选择器 */
h3 ~ p {
color: #666;
}
4. 盒模型详解
.box {
/* 内容区 */
width: 300px;
height: 200px;
/* 内边距 */
padding: 20px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 20px;
/* 边框 */
border: 1px solid #000;
border-width: 1px 2px 3px 4px;
border-style: solid dashed dotted double;
border-color: red green blue black;
border-radius: 10px;
/* 外边距 */
margin: 10px;
margin: 10px auto; /* 水平居中 */
/* 盒模型计算方式 */
box-sizing: border-box; /* 包含padding和border */
}
5. 背景与边框
.element {
/* 背景颜色 */
background-color: #f0f0f0;
/* 背景图片 */
background-image: url('bg.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed; /* 固定背景 */
/* 渐变色背景 */
background: linear-gradient(to right, red, yellow);
/* 多重背景 */
background-image: url('bg1.png'), url('bg2.png');
background-position: left top, right bottom;
/* 边框 */
border: 1px solid #333;
border-radius: 10px;
border-top-left-radius: 0;
/* 阴影 */
box-shadow: 2px 2px 5px rgba(0,0,0,0.3);
text-shadow: 1px 1px 1px #000;
}
6. 文本样式
.text {
/* 字体 */
font-family: "Helvetica Neue", Arial, sans-serif;
font-size: 16px;
font-weight: bold;
font-style: italic;
font-variant: small-caps;
/* 文本 */
color: #333;
text-align: justify;
text-decoration: underline;
text-transform: uppercase;
letter-spacing: 1px;
word-spacing: 2px;
line-height: 1.5;
text-indent: 2em;
/* 溢出处理 */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
7. 显示与定位
显示类型
.block {
display: block; /* 块级元素 */
}
.inline {
display: inline; /* 行内元素 */
}
.inline-block {
display: inline-block; /* 行内块元素 */
}
.none {
display: none; /* 隐藏元素 */
}
.flex {
display: flex; /* 弹性布局 */
}
.grid {
display: grid; /* 网格布局 */
}
定位
.static {
position: static; /* 默认 */
}
.relative {
position: relative;
top: 10px;
left: 20px;
}
.absolute {
position: absolute;
right: 0;
bottom: 0;
}
.fixed {
position: fixed;
top: 0;
left: 0;
}
.sticky {
position: sticky;
top: 0;
}
/* 层叠顺序 */
.layer {
z-index: 10;
}
浮动与清除
.float-left {
float: left;
width: 50%;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
8. CSS 布局技术
浮动布局
.container {
width: 960px;
margin: 0 auto;
}
.column {
float: left;
width: 33.333%;
padding: 10px;
}
/* 清除浮动 */
.container::after {
content: "";
display: table;
clear: both;
}
弹性布局(Flexbox)
.flex-container {
display: flex;
flex-direction: row; /* 主轴方向: row | row-reverse | column | column-reverse */
flex-wrap: wrap; /* 换行: nowrap | wrap | wrap-reverse */
justify-content: center; /* 主轴对齐: flex-start | flex-end | center | space-between | space-around | space-evenly */
align-items: center; /* 交叉轴对齐: stretch | flex-start | flex-end | center | baseline */
align-content: stretch; /* 多行对齐 */
gap: 10px; /* 项目间距 */
}
.flex-item {
flex: 1; /* 增长比例 */
align-self: auto; /* 单个项目对齐 */
order: 1; /* 排序 */
}
网格布局(Grid)
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 列定义 */
grid-template-rows: 100px auto 50px; /* 行定义 */
gap: 10px; /* 间距 */
grid-template-areas:
"header header header"
"sidebar main ."
"footer footer footer";
}
.header {
grid-area: header;
}
.main {
grid-area: main;
}
/* 响应式网格 */
@media (max-width: 768px) {
.grid-container {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}
9. 响应式设计
/* 媒体查询 */
@media screen and (max-width: 768px) {
.container {
width: 100%;
padding: 10px;
}
.column {
float: none;
width: 100%;
}
}
/* 视口单位 */
.responsive-text {
font-size: calc(16px + 1vw);
}
/* 图片响应 */
.responsive-img {
max-width: 100%;
height: auto;
}
/* 弹性布局响应 */
.flex-responsive {
flex-direction: column;
}
@media (min-width: 768px) {
.flex-responsive {
flex-direction: row;
}
}
三、实际应用示例
1. 导航栏
<nav class="navbar">
<div class="container">
<a href="#" class="logo">我的网站</a>
<ul class="nav-links">
<li><a href="#">首页</a></li>
<li><a href="#">产品</a></li>
<li><a href="#">关于</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</div>
</nav>
.navbar {
background-color: #333;
color: white;
padding: 1rem 0;
}
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 1.5rem;
font-weight: bold;
text-decoration: none;
color: white;
}
.nav-links {
display: flex;
list-style: none;
}
.nav-links li {
margin-left: 2rem;
}
.nav-links a {
color: white;
text-decoration: none;
transition: color 0.3s;
}
.nav-links a:hover {
color: #f0f0f0;
}
/* 移动端响应 */
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.nav-links {
margin-top: 1rem;
flex-direction: column;
align-items: center;
}
.nav-links li {
margin: 0.5rem 0;
}
}
2. 卡片组件
<div class="card">
<img src="product.jpg" alt="产品图片" class="card-image">
<div class="card-content">
<h3 class="card-title">产品名称</h3>
<p class="card-text">产品描述内容...</p>
<div class="card-footer">
<span class="price">$99.99</span>
<button class="btn">购买</button>
</div>
</div>
</div>
.card {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: transform 0.3s, box-shadow 0.3s;
max-width: 300px;
margin: 1rem;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-content {
padding: 1.5rem;
}
.card-title {
margin: 0 0 0.5rem;
font-size: 1.25rem;
}
.card-text {
color: #666;
margin: 0 0 1rem;
line-height: 1.5;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
}
.price {
font-weight: bold;
color: #e63946;
font-size: 1.2rem;
}
.btn {
background-color: #007bff;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #0056b3;
}
四、现代 CSS 特性
1. CSS 变量
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--text-color: #333;
--spacing-unit: 8px;
}
.element {
color: var(--text-color);
margin: calc(var(--spacing-unit) * 2);
background-color: var(--primary-color);
}
@media (prefers-color-scheme: dark) {
:root {
--text-color: #f0f0f0;
--primary-color: #2980b9;
}
}
2. CSS 过渡与动画
/* 过渡 */
.button {
transition: all 0.3s ease-in-out;
}
.button:hover {
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
/* 关键帧动画 */
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.slide-in {
animation: slideIn 0.5s ease-out forwards;
}
/* 延迟动画 */
.delay-1 {
animation-delay: 0.1s;
}
.delay-2 {
animation-delay: 0.2s;
}
3. CSS 变换
.transform-example {
transform: translate(50px, 20px) rotate(15deg) scale(1.2);
transform-origin: left top;
}
/* 3D 变换 */
.card-3d {
transform-style: preserve-3d;
transition: transform 1s;
}
.card-3d:hover {
transform: rotateY(180deg);
}
五、性能优化与最佳实践
1. 性能优化技巧
/* 避免昂贵的属性 */
.element {
/* 避免 */
box-shadow: 0 0 10px rgba(0,0,0,0.5);
/* 更好 */
box-shadow: 0 0 5px rgba(0,0,0,0.3);
}
/* 减少重绘 */
.animate {
will-change: transform, opacity;
}
/* 硬件加速 */
.accelerate {
transform: translateZ(0);
}
/* 关键CSS */
<link rel="preload" href="critical.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="critical.css"></noscript>
2. 组织CSS代码
/* 按功能组织 */
/* 基础样式 */
body {
font-family: sans-serif;
line-height: 1.6;
}
/* 布局样式 */
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
}
/* 组件样式 */
.button {
display: inline-block;
padding: 0.5em 1em;
}
/* 状态样式 */
.is-active {
background: #007bff;
}
/* 主题样式 */
.theme-dark {
background: #333;
color: #fff;
}
3. CSS 方法论
BEM (Block Element Modifier)
/* 块 */
.card {}
/* 元素 */
.card__title {}
.card__image {}
/* 修饰符 */
.card--featured {}
.card__button--disabled {}
SMACSS (Scalable Modular Architecture)
/* 基础 */
body, h1, p {}
/* 布局 */
.l-header {}
.l-grid {}
/* 模块 */
.btn {}
.media {}
/* 状态 */
.is-hidden {}
.is-active {}
/* 主题 */
.theme-dark {}
六、学习资源与练习建议
1. 推荐学习资源
- MDN Web Docs: 最权威的HTML/CSS文档
- CSS Tricks: 实用的CSS技巧和教程
- CodePen: 在线代码编辑和分享平台
- Frontend Mentor: 真实项目练习平台
- FreeCodeCamp: 免费编程课程
2. 练习项目建议
- 个人简历页面: 练习HTML结构和基本样式
- 产品展示页面: 练习布局和响应式设计
- 博客网站: 练习内容组织和样式复用
- 仪表盘界面: 练习复杂布局和组件设计
- 动画效果: 练习CSS过渡和动画
3. 调试工具
- Chrome DevTools: 检查元素、修改样式、调试布局
- CSS Lint: 检查CSS代码质量
- Can I Use: 检查CSS属性兼容性
- BrowserStack: 跨浏览器测试
七、HTML5 和 CSS3 新特性
HTML5 新元素
<!-- 语义化标签 -->
<header>
<nav>
<ul>
<li><a href="#">首页</a></li>
</ul>
</nav>
</header>
<main>
<article>
<section>
<h1>文章标题</h1>
<p>文章内容</p>
</section>
</article>
<aside>
<h2>相关链接</h2>
</aside>
</main>
<footer>
<p>版权信息</p>
</footer>
<!-- 多媒体 -->
<video controls width="640">
<source src="movie.mp4" type="video/mp4">
</video>
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
</audio>
<!-- 图形 -->
<canvas id="myCanvas" width="200" height="100"></canvas>
<!-- 表单增强 -->
<input type="email" placeholder="输入邮箱">
<input type="date">
<input type="color">
<input type="range" min="0" max="100">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
</datalist>
<input list="browsers">
CSS3 新特性
/* 圆角 */
.rounded {
border-radius: 10px;
}
/* 渐变 */
.gradient {
background: linear-gradient(to right, #ff9966, #ff5e62);
}
/* 阴影 */
.shadow {
box-shadow: 3px 3px 10px rgba(0,0,0,0.2);
}
/* 过渡 */
.transition {
transition: all 0.3s ease;
}
/* 变换 */
.transform {
transform: rotate(10deg) scale(1.1);
}
/* 动画 */
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.animate {
animation: pulse 2s infinite;
}
/* 多列布局 */
.columns {
column-count: 3;
column-gap: 20px;
}
/* 弹性盒 */
.flex {
display: flex;
justify-content: center;
align-items: center;
}
/* 网格布局 */
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
/* 滤镜 */
.filter {
filter: blur(2px) grayscale(50%);
}
/* 混合模式 */
.blend {
background-blend-mode: multiply;
}
八、常见问题与解决方案
1. 居中问题
/* 水平居中 */
.center-h {
margin: 0 auto;
width: 80%;
}
/* 垂直居中 */
.center-v {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/* 完全居中 (方法1) */
.center-1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* 完全居中 (方法2) */
.center-2 {
display: flex;
justify-content: center;
align-items: center;
}
/* 完全居中 (方法3) */
.center-3 {
display: grid;
place-items: center;
}
2. 清除浮动
/* 方法1: 使用clearfix类 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 方法2: 使用overflow */
.container {
overflow: auto;
}
/* 方法3: 使用flex或grid布局 */
.container {
display: flex;
/* 或 */
display: grid;
}
3. 响应式图片
<picture>
<source media="(min-width: 1200px)" srcset="large.jpg">
<source media="(min-width: 768px)" srcset="medium.jpg">
<img src="small.jpg" alt="响应式图片" style="width: 100%;">
</picture>
4. 移动端适配
<meta name="viewport" content="width=device-width, initial-scale=1.0">
/* 使用相对单位 */
html {
font-size: 16px;
}
@media (max-width: 768px) {
html {
font-size: 14px;
}
}
/* 响应式设计 */
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
padding: 0 15px;
}
/* 移动优先 */
.button {
padding: 8px 16px;
font-size: 1rem;
}
@media (min-width: 768px) {
.button {
padding: 12px 24px;
font-size: 1.2rem;
}
}
九、进阶学习路径
- CSS 预处理器: 学习 Sass/Less 提高CSS开发效率
- CSS架构: 学习BEM、SMACSS等架构方法
- CSS-in-JS: 学习styled-components等现代方案
- CSS性能优化: 深入理解渲染性能和优化技巧
- CSS Houdini: 了解下一代CSS API
- CSS规范: 阅读W3C CSS规范文档
掌握这些基础后,你可以进一步学习JavaScript来为网页添加交互功能,或者深入学习前端框架如React、Vue等。