CSS 建设网站 图文教程
HTML 搭建基础网站
- HTML 基础知识
HTML 是现代网站的基础框架,它由一系列标签和样式的组合构成,为了建立一个简单的网站,我们首先需要一个 HTML 文件,它包含 HTML 标签和一些简单的 CSS 样式。
(图片来源网络,侵删)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.">网站教程</title>
</head>
<body>
<!-- 内容区 -->
<div class="container" style="max-width: 1px; margin: 0 auto;">
<!-- 栏目标题 -->
<h1 style="color: #2c3e5; margin-bottom: 2px;">网站教程</h1>
<!-- 日期标签 -->
<div class="date-center" style="margin-bottom: 1px;">
<span id="date" class="date">223年1月15日</span>
</div>
<!-- 作者信息 -->
<div class="author-info" style="margin-bottom: 1px;">
<h3>由小明开发</h3>
<p>www.tencent.com</p>
</div>
</div>
</body>
</html>
- CSS 系统设置
CSS 是网站风格的核心,它能帮助我们美化网站,让内容看起来更美观,以下是常见的 CSS 设置:
- 背景颜色:设置网站的整体背景颜色,可以使用
body标签的background-color属性。 - 字体选择:在
styleheets文件中定义字体,或者在 HTML 标签中使用font属性。 - 排版布局:通过
display属性来控制内容的布局,block、none等。 - 响应式设计:使用
meta viewport标签来确保网站在不同设备上的适应性。 - 图片处理:使用
img标签来插入图片,并使用 CSS 样式处理图片的缩放、旋转等效果。
- CSS 示例
以下是常见的 CSS 示例,帮助我们更好地控制网站的样式:
(图片来源网络,侵删)
- 字体样式:
body {
font-family: Arial, sans-serif;
}
h1 {
color: #3498db;
}
hr {
color: #2c3e5;
}
p {
margin: 0;
line-height: 1.6;
}
- 背景样式:
body {
background-color: #f3f4f6;
}
h1 {
color: #2c3e5;
}
h2 {
color: white;
margin-top: 1px;
}
- 响应式布局:
<style>
@media (max-width: 768px) {
body {
margin: 0;
}
}
</style>
CSS 实现响应式设计
响应式设计是现代网站的重要特点,确保网站在不同设备上的适配性,以下是常见的响应式设计方法:
- 固定布局与缩放
- 定义网站的整体宽度和最大高度。
- 使用
min-height和max-width来控制不同设备上的显示效果。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta viewport="width: 768px; height: 96px;">网站教程</title>
<style>
body {
min-height: 1vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f3f4f6;
color: #3498db;
font-family: Arial, sans-serif;
}
.container {
max-width: 12px;
margin: 0 auto;
padding: 2px;
}
h1 {
text-align: center;
margin-bottom: 3px;
}
h2 {
text-align: center;
margin-bottom: 2px;
}
</style>
</head>
<body>
<div class="container">
<h1 style="color: #2c3e5;">网站教程</h1>
<h2 style="color: white;">欢迎访问我们的网站</h2>
</div>
</body>
</html>
- 动态缩放
使用 CSS 动态缩放来自动调整文本尺寸,确保不同设备上的内容都清晰可读。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta viewport="width: 768px; height: 96px;">网站教程</title>
<style>
body {
min-height: 1vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f3f4f6;
color: #3498db;
font-family: Arial, sans-serif;
}
.container {
max-width: 12px;
margin: 0 auto;
padding: 2px;
}
h1 {
text-align: center;
margin-bottom: 3px;
font-size: 1.5rem;
}
h2 {
text-align: center;
margin-bottom: 2px;
font-size: 0.9rem;
}
</style>
</head>
<body>
<div class="container">
<h1 style="font-size: 1.2rem;">网站教程</h1>
<h2 style="font-size: 0.8rem;">欢迎访问我们的网站</h2>
</div>
</body>
</html>
- 图片缩放
使用 img 标签来插入图片,并使用 CSS 动态缩放来调整图片大小。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta viewport="width: 768px; height: 96px;">网站教程</title>
<style>
body {
min-height: 1vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f3f4f6;
color: #3498db;
font-family: Arial, sans-serif;
}
.container {
max-width: 12px;
margin: 0 auto;
padding: 2px;
}
h1 {
text-align: center;
margin-bottom: 3px;
font-size: 1.5rem;
font-weight: bold;
}
h2 {
text-align: center;
margin-bottom: 2px;
font-size: 0.9rem;
font-weight: bold;
}
</style>
</head>
<body>
<img src="https://via.placeholder.com/8" alt="图片1" style="max-width: 1%; height: auto;">
<img src="https://via.placeholder.com/1" alt="图片2" style="max-width: 8%; height: auto;">
<img src="https://via.placeholder.com/15" alt="图片3" style="max-width: 12%; height: auto;">
</body>
</html>
JavaScript 实现脚本
- 加载
使用 JavaScript 实现网站动态内容的加载,例如loading indicator、动态图片加载等。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta viewport="width: 768px; height: 96px;">网站教程</title>
<style>
body {
min-height: 1vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f3f4f6;
color: #3498db;
font-family: Arial, sans-serif;
}
.container {
max-width: 12px;
margin: 0 auto;
padding: 2px;
}
h1 {
text-align: center;
margin-bottom: 3px;
font-size: 1.5rem;
font-weight: bold;
}
h2 {
text-align: center;
margin-bottom: 2 


