制作网页用css
基础CSS引入方式
在HTML文件中通过<style>标签内嵌CSS代码:
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
</style>
通过外部文件引入CSS(推荐方式):
<link rel="stylesheet" href="styles.css">
常用选择器类型
元素选择器直接作用于HTML标签:
p {
color: #333;
line-height: 1.6;
}
类选择器通过class属性定位:
.button {
background-color: #4CAF50;
padding: 10px 15px;
}
ID选择器针对唯一元素:
#header {
height: 80px;
background: linear-gradient(to right, #ff8a00, #da1b60);
}
盒模型控制
设置元素尺寸与边距:

.box {
width: 300px;
height: 200px;
padding: 20px;
border: 1px solid #ddd;
margin: 15px auto;
}
Flex布局实现水平排列:
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
响应式设计要点
媒体查询适配不同设备:
@media (max-width: 768px) {
.menu {
flex-direction: column;
}
.hero-section {
padding: 40px 20px;
}
}
相对单位确保可伸缩性:

.article {
font-size: 1.2rem;
max-width: 90vw;
margin: 0 auto;
}
视觉增强技巧
CSS变量实现主题切换:
:root {
--primary-color: #6200ee;
--secondary-color: #03dac6;
}
.button {
background-color: var(--primary-color);
}
过渡动画效果:
.card {
transition: transform 0.3s ease, box-shadow 0.2s;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
现代布局技术
Grid布局创建复杂结构:
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
定位与层叠控制:
.modal {
position: fixed;
z-index: 1000;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}






