css实现左侧固定宽度,右侧宽度自适应(转载)
转载来源:https://blog.csdn.net/cristina_song/article/details/78374705
1,固定宽度区浮动,自适应区不设宽度而设置 margin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> <html> <head> <style type= "text/css" > #wrap { overflow: hidden; *zoom: 1; } #content ,#sidebar { background-color: #eee; } #sidebar { float : left; width: 300px; } #content { /*float: right;此处不能写浮动,否则会脱离文档流*/ margin-left: 310px; } #footer {background-color: #f00;color:#fff; margin-top: 1em} </style> </head> <body> <div id= "wrap" > <div id= "sidebar" style= "height:340px;" >固定宽度区</div> <div id= "content" style= "height:340px;" >自适应区</div> </div> <div id= "footer" >后面的一个DIV,以确保前面的定位不会导致后面的变形</div> </body> </html> |
无论content和sidebar谁更长,都不会对布局造成影响.
2,固定宽度区使用绝对定位,自适应区照例设置margin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html> <html> <head> <style type= "text/css" > #wrap { *zoom: 1; position: relative; } #content ,#sidebar { background-color: #eee; } #sidebar {position: absolute; left:0; top: 0;width: 300px; } #content { margin-left: 310px; } #footer {background-color: #f00;color:#fff; margin-top: 1em} </style> </head> <body> <div id= "wrap" > <div id= "sidebar" style= "height:340px;" >固定宽度区</div> <div id= "content" style= "height:240px;" >自适应区</div> </div> <div id= "footer" >后面的一个DIV,以确保前面的定位不会导致后面的变形</div> </body> </html> |
footer说——我不给绝对主义者让位!所以要注意footer的设置。
3.标准浏览器的方法
w3c标准早就为我们提供了制作这种自适应宽度的标准方法。那就简单了:把wrap设为display:table并指定宽度100%,然后把content+sidebar设为display:table-cell;然后只给sidebar指定一个宽度,那么content的宽度就变成自适应了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!DOCTYPE html> <html> <head> <style type= "text/css" > #wrap { display:table; width: 100%; } #content,#sidebar { background-color: #eee; display: table-cell; } #sidebar { width: 300px; } #content { margin-left: 10px; display: block; } #footer {background-color: #f00;color:#fff; margin-top: 1em} </style> </head> <body> <div id= "wrap" > <div id= "sidebar" style= "height:240px;" >固定宽度区</div> <div id= "content" style= "height:340px;" >自适应区</div> </div> <div id= "footer" >后面的一个DIV,以确保前面的定位不会导致后面的变形</div> </body> </html> |
如果不考虑ie7及以下版本,则使用标准方法;如果不在意sidebar与content的顺序,则用第一种方法;
树立目标,保持活力,gogogo!
标签:
css
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-12-11 如果再聘请一位会css的美工,那要你还有什么用?
2017-12-11 通过set赋值,与select赋值的区别
2017-12-11 存储过程
2017-12-11 T-sql 编程
2017-12-11 ADO调用分页查询存储过程
2017-12-11 自连接-省市问题
2017-12-11 多条件搜索问题 -sql拼接与参数化查询