使用position设置经典的网站前端结构
能脱离文档流的设置:
- float:left/right
- position:absolute; 绝对定位
- position:fixed; 固定定位
//搞清楚position的属性值的意思就容易明白
使用position布局能使得标签脱离文档流(但是属性值为relative除外),这个脱离文档流是不会受到父级标签约束的,除非:
若某个绝对定位元素的容器也被定位过,那么该元素指定的top和left值将不会基于文档的根元素html(也就是浏览器窗口的左上角)计算,而是会基于其容器的左上角计算这个偏移量。换句话说,也就是:被定位过的容器将扮演其中所有元素绝对定位起始点的角色。
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title></title> | |
<style type="text/css"> | |
#div1 { | |
width: 200px; | |
position: absolute; | |
height: 600px; | |
background: darksalmon; | |
top: 10px; | |
left: 5px; | |
} | |
#div2 { | |
width: 700px; | |
height: 1200px; | |
background: yellowgreen; | |
position: absolute; | |
left: 220px; | |
top: 10px; | |
} | |
#div3 { | |
width: 300px; | |
height: 600px; | |
background: hotpink; | |
position: absolute; | |
left: 930px; | |
top: 10px; | |
} | |
#div5 { | |
background: chartreuse; | |
position:relative; | |
height: 620px; | |
width: 1240px; | |
top: 0px; | |
left: 0px;; | |
} | |
#head { | |
width: 1240px; | |
height: 150px; | |
background: coral;; | |
} | |
#footer { | |
width: 1240px; | |
height: 150px; | |
float: left; | |
background: darkturquoise; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="head">head</div> | |
<!--div5的position设置为relative,意思就是:div5还是在流当中,遵从由上到下排列,只是设了top和left后,就往右或下方挪--> | |
<!--div5的position设置为absolute,那么div5就脱离了文档流,可以不理head的位置,随意按top和left来相对浏览器摆放,除非div5又被一个设了position的div包裹--> | |
<div id="div5"> | |
<!--div1,div2,div3设了position: absolute,那么就脱离了文档流,如果div5不设置position,那么div123的top和left就会相对于浏览器显示的地方--> | |
<!--如果div5设置了position,无论position设置了什么,那么div1,2,3的top和left就相对于div5--> | |
<div id="div1">B</div> | |
<div id="div2">A</div> | |
<div id="div3">C</div> | |
</div> | |
<div id="footer"></div> | |
</body> | |
</html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现