惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

博客园 - 廖勇军

关于c++的头文件依赖 增强资源管理器右键功能,含源代码 VC中结构体的内存布局 进程间共享句柄三种方式 SocanCode连接Oracle的方法 SocanCode7之模板编写 不用再纠结反射影响效率了 javascript总结 IIS7.0中使用MVC3,静态页正常,其它404 ashx的使用 一起来灭掉IE6! iis express感觉还不错 关于sqlite使用entity framework的布署问题 - 廖勇军 - 博客园 原来Jquery.load的方法可以一直load下去 错误1067进程意外终止 关于省市联动的问题想法 javac编译多个带package文件 远程服务器返回了错误 NOTFOUND Java程序放到Linux上出现的问题
负margin实现div的左右排版
廖勇军 · 2011-04-08 · via 博客园 - 廖勇军

使用负margin可以使当前的div左边能容纳下面的div浮动上来,因此把右边的div摆在前面,左边的摆后面,右边的使用负margin就能让左边的浮上来,这样就遮住了右边的左半部分,只要右边再内部使用一个div,外边距为左边的宽度就实现了左右的排版。

1、左边固定,右边自适应

    <div>
        
<div style="float: right; margin: 0 0 0 -200px; width: 100%;">
            
<div style="margin: 0 0 0 200px; background: #e4e4e4;">
                这是右边部分
            
</div>
        
</div>
        
<div style="float: left; width: 200px; background: #669999">
            这是左边部分
        
</div>
    
</div>

2、右边固定,左边自适应,跟上一个最大的不同点就是,左右两个div容器的代码的前后位置换了,原因就是"float:rihgt"一定要在"float:left"前面。

    <div>
        
<div style="float: right; width: 200px; background: #669999">
            这是右边部分
        
</div>
        
<div style="float: left; margin: 0 -200px 0 0; width: 100%;">
            
<div style="margin: 0 200px 0 0; background: #e4e4e4;">
                这是左边部分
            
</div>
        
</div>
    
</div>

3、左右各占一定百分比,这就简单了,把上面任意一种将200px改成比例值20%就实现了。

    <div>
        
<div style="float: right; margin: 0 0 0 -20%; width: 100%;">
            
<div style="margin: 0 0 0 20%; background: #e4e4e4;">
                这是右边部分
            
</div>
        
</div>
        
<div style="float: left; width: 20%; background: #669999">
            这是左边部分
        
</div>
    
</div>

4、再加一栏,实现左中右三栏布局。这只要再加一栏,float为right的多空一些位置就行了。这里以按比例为例

    <div>
        
<div style="float: right; margin: 0 0 0 -40%; width: 100%;">
            
<div style="margin: 0 0 0 40%; background: #e4e4e4;">
                这是右边部分
            
</div>
        
</div>
        
<div style="float: left; width: 20%; background: #669999">
            这是左边部分
        
</div>
        
<div style="float: left; width: 20%; background: #663333">
            这是中间部分
        
</div>
    
</div>

请注意:以上代码中都没有设置高度,实际上左右高度是不一致的,如果要左右高度一致,请不要设置左右的高度,由内容自动撑开,然后在页面合适位置用js实现:

window.onload = window.onresize = function ()
{
    var left = document.getElementById("doc-left");
    var right = document.getElementById("doc-right");

    var maxHeight = Math.max(left.clientHeight, right.clientHeight) + "px";
    left.style.height = right.style.height = maxHeight;
}

5、实现框架页的效果(左右可分别出现滚动条,页面无滚动条):只要左右div各加position: absolute; overflow: scroll; height: 100%;右边再加left: 200px;即实现了,这里用了绝对定位,因此float属性可以干掉了

    <div>
        
<div style="margin: 0 0 0 -200px; width: 100%; position: absolute; overflow: scroll;
            height: 100%; left: 200px;"
>
            
<div style="margin: 0 0 0 200px; background: #e4e4e4;">
                这里是右边部分
            
</div>
        
</div>
        
<div style="width: 200px; background: #669999; position: absolute; overflow: scroll;
            height: 100%;"
>
            这是左边部分
        
</div>
    
</div>