查看: 64|回复: 0

css实现两个div并列显示的多种方法

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-3-9
发表于 昨天 22:16 | 显示全部楼层 |阅读模式

 方法一:float浮动,float:left;为左浮动,也可以设置为float:right;右浮动,也可以实现两个div并列一行。

#div1{
    width:50%;
    height:300px;
    background:blue;
    float:left;
}
#div2{
    width:50%;
    height:300px;
    background:green;
    float:left;
}

方法二:display:table-cell

#parent{
    width:100%;
    display:table;
}
#div1{
    width:50%;
    height:300px;
    background:blue;
    display:table-cell;
}
#div2{
    width:50%;
    height:300px;
    background:green;
    display:table-cell;
}

方法三:负margin

#parent{
    display:flex;
    overflow:hidden;
}
#div1{
    width:50%;
    height:300px;
    background:blue;
    padding-bottom:2000px;  
    margin-bottom:-2000px;  
}
#div2{
    width:50%;
    height:300px;
    background:green;
    padding-bottom:2000px;  
    margin-bottom:-2000px;  
}

方法四:绝对定位

*{
    margin:0;
    padding:0;
}
#div1{
    width:50%;
    height:300px;
    background:blue;
    position:absolute;
    left:0;
    top:0;
}
#div2{
    width:50%;
    height:300px;
    background:green;
    position:absolute;
    transform:translate(100%, 0);
}

方法五:flex布局

#parent{
    display:flex;
}
#div1{
    width:50%;
    height:300px;
    background:blue;
    flex:1;
}
#div2{
    width:50%;
    height:300px;
    background:green;
    flex:1;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部