简单网页制作css
基础CSS结构
CSS(层叠样式表)用于控制网页的样式和布局。一个基础的CSS文件通常包含选择器和属性声明块。例如:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
常用选择器
- 元素选择器:直接针对HTML标签(如
p、h1)。p { color: #333; line-height: 1.6; } - 类选择器:通过
.前缀匹配HTML的class属性。.header { background: #35424a; color: white; padding: 20px; } - ID选择器:通过
#前缀匹配HTML的id属性(通常用于唯一元素)。#main-nav { display: flex; justify-content: space-between; }
盒模型控制
CSS盒模型包括margin、border、padding和content:
.box {
width: 300px;
padding: 20px;
border: 1px solid #ddd;
margin: 10px auto;
}
响应式布局
使用媒体查询(@media)适配不同设备屏幕:

@media (max-width: 768px) {
.box {
width: 100%;
padding: 10px;
}
}
弹性布局(Flexbox)
Flexbox适合一维布局(如导航栏):
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
}
网格布局(Grid)
Grid适合二维复杂布局:

.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
动画效果
通过@keyframes和transition实现动态效果:
.button {
transition: background 0.3s ease;
}
.button:hover {
background: #0056b3;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fade-in {
animation: fadeIn 1s;
}
字体与图标
引入外部字体(如Google Fonts)和图标库(如Font Awesome):
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
body {
font-family: 'Roboto', sans-serif;
}
变量与复用
CSS变量提升可维护性:
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
}
.button {
background-color: var(--primary-color);
}






