div和css制作
div与CSS基础应用
HTML结构搭建
使用<div>标签划分页面区块,为每个div添加类名或ID以便CSS定位:
<div class="header">页眉内容</div>
<div id="main-content">
<div class="sidebar">侧边栏</div>
<div class="article">主体内容</div>
</div>
<div class="footer">页脚信息</div>
CSS样式控制
通过选择器为div添加样式,示例包含布局、颜色和间距:
.header {
background-color: #333;
color: white;
padding: 20px;
text-align: center;
}
#main-content {
display: flex;
margin: 15px 0;
}
.sidebar {
width: 25%;
background: #f4f4f4;
padding: 10px;
}
.article {
width: 75%;
padding: 15px;
border-left: 1px solid #ddd;
}
.footer {
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
响应式布局实现
媒体查询适配
针对不同屏幕尺寸调整div布局:
@media (max-width: 768px) {
#main-content {
flex-direction: column;
}
.sidebar, .article {
width: 100%;
}
.article {
border-left: none;
border-top: 1px solid #ddd;
}
}
弹性盒模型进阶
使用flexbox优化内部元素排列:
.header {
display: flex;
justify-content: space-between;
align-items: center;
}
.footer {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
视觉效果增强
阴影与过渡效果
为div添加交互式视觉效果:
.article {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
.article:hover {
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
transform: translateY(-2px);
}
背景渐变与圆角
提升视觉层次感:
.header {
background: linear-gradient(to right, #333, #555);
border-radius: 0 0 10px 10px;
}
.sidebar {
border-radius: 5px;
}
定位与层级管理
绝对/相对定位
实现特殊布局需求:

.footer {
position: relative;
}
.copyright {
position: absolute;
right: 20px;
bottom: 5px;
}
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100;
}






