web css制作
CSS 制作基础
CSS(层叠样式表)用于控制网页的外观和布局。通过 CSS 可以定义颜色、字体、间距、动画等样式。
基本语法 CSS 规则由选择器和声明块组成:
选择器 {
属性: 值;
属性: 值;
}
选择器类型
- 元素选择器:
p { color: red; } - 类选择器:
.class-name { font-size: 16px; } - ID 选择器:
#id-name { background: blue; } - 属性选择器:
[type="text"] { border: 1px solid; }
常用 CSS 属性
文本样式
p {
font-family: Arial;
font-size: 14px;
color: #333;
line-height: 1.5;
text-align: center;
}
盒模型

div {
width: 200px;
height: 100px;
padding: 10px;
margin: 20px;
border: 1px solid black;
}
背景
body {
background-color: #f5f5f5;
background-image: url("image.jpg");
background-repeat: no-repeat;
background-size: cover;
}
布局技术
Flexbox
.container {
display: flex;
justify-content: center;
align-items: center;
}
.item {
flex: 1;
}
Grid

.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
.grid-item {
grid-column: span 2;
}
响应式设计
媒体查询
@media (max-width: 768px) {
.menu {
display: none;
}
.mobile-menu {
display: block;
}
}
视口单位
.header {
height: 100vh;
width: 100vw;
}
动画效果
过渡
.button {
transition: all 0.3s ease;
}
.button:hover {
transform: scale(1.1);
}
关键帧动画
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}
.box {
animation: slide 2s infinite;
}
CSS 最佳实践
- 使用外部样式表:
<link rel="stylesheet" href="styles.css"> - 遵循 BEM 命名规范:
.block__element--modifier - 使用 CSS 预处理器(Sass/Less)
- 实现 CSS 变量:
:root { --primary-color: #4285f4; } .header { color: var(--primary-color); }
调试工具
- 浏览器开发者工具(F12)
- CSS 验证器:https://jigsaw.w3.org/css-validator/
- 跨浏览器测试工具
以上内容涵盖了 CSS 的核心概念和实用技巧,适用于从基础到进阶的网页样式开发。






