学习如何使用CSS美化网页
CSS(层叠样式表)用于定义HTML元素的样式:
/* 基本CSS语法 */
selector {
property: value;
}
/* 示例 */
h1 {
color: blue;
font-size: 24px;
}
CSS选择器用于选择要设置样式的HTML元素:
p {
color: red;
}
.highlight {
background: yellow;
}
#header {
background: #35424a;
}
CSS盒模型描述了元素的内容、内边距、边框和外边距:
.box {
width: 200px;
height: 100px;
padding: 20px;
margin: 20px;
border: 2px solid #e8491d;
background: #f8f9fa;
}
CSS提供了多种布局方式:
.flex-container {
display: flex;
gap: 1rem;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
CSS可以创建动画和过渡效果:
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.animated {
animation: bounce 2s infinite;
}
.transition {
transition: all 0.3s ease;
}
.transition:hover {
transform: scale(1.2);
}
使用媒体查询创建响应式布局:
/* 响应式布局示例 */
@media (max-width: 768px) {
.container {
width: 95%;
}
.grid {
grid-template-columns: 1fr;
}
}
卡片内容描述