多列布局:定宽+自适应,不定宽+自适应,等宽布局,等高布局

多列布局:定宽+自适应,不定宽+自适应,等宽布局,等高布局

1、 两列布局

(1) 1列定宽+1列自适应

示例:

这里写图片描述

html代码:

	<div class="parent">
        <div class="left">
            <p>left定宽</p>
        </div>
        <div class="right">
            <p>right1自适应</p>
            <p>right2</p>
        </div>
    </div>

css样式:

		/* 方法一:  float + margin )  */
        .left{
            float:left;
            width:300px;
            background-color: #5f5;
        }
        .right{
            margin-left:310px;
            background-color: #c5c;
        }
        

        /* 方法二 : float + overflow */
        .left{
            float: left;
            width:300px;
            margin-right:10px;
            background-color: #5f5;
        }
        .right{
            overflow: hidden;
            background-color: #c5c;
        }  


        /* 方法三: table */
        .parent{
            display: table;
            width:100%;
            table-layout: fixed;
        }
        .left{
             display: table-cell;
             width: 300px;
             padding-right: 20px;
             background: #5f5;
        }
        .right{
            display: table-cell;
            background: #c5c;
        }


        /* 方法四: flex */
        .parent{
            display: flex;
        }
        .left{
            width: 300px;
            margin-right: 10px;
            background: #5f5;
        }
        .right{
            flex: 1;
             background: #c5c;
        }
		 
		 /* 方法五:float + margin + position */

		html代码:

		<div class="parent">
        	<div class="left">
            	<p>left定宽</p>
        	</div>
        	<div class="right-fix">
            	<div class="right">
                	<p>right1自适应</p>
                	<p>right2</p> 
            	</div>
        	</div>
    	</div>


		css代码:

        .left{
            float:left;
            width:100px;
            position: relative;
            background-color: #5f5;
        }
        .right-fix{
            float: right;
            width:100%;
            margin-left:-100px;
        }
        .right{
            margin: -20px 0 0 110px;
            background-color: #c5c;
        }
(2) 1列不定宽+1列自适应

示例:

这里写图片描述

html代码:

	<div class="parent">
        <div class="left">
            <p>left</p>
            <p>left不定宽</p>
        </div>
        <div class="right">
            <p>right</p>
            <p>自适应</p>
        </div>
    </div>

css样式:

		 /* 方法一:float + overflow */
        .left{
            float: left;
            margin-right:10px;
            background-color: #5f5;
        }
        .right{
            overflow: hidden;
            background-color: #c5c;
        }


        /* 方法二: table */
        .parent{
            display: table;
            width: 100%;
        }
        .left{
            width: 0.1%;
            display: table-cell;
            background-color: #5f5;
        }
        .left p{
            width:100px;
        }
        .right{
            display: table-cell;
            background-color: #c5c;
        }


        /* 方法三: flex */
        .parent{
            display: flex;
        }
        .left{
            margin-right: 10px;
            background-color: #5f5;
        }
        .right{
            flex:1;
            background-color: #c5c;
        }

2、 多列布局

(1) 多列定宽+1列自适应

示例:

这里写图片描述

html代码:

	.left,.center{
        float: left;
        width: 200px;
        margin-right: 10px;
        background-color: #5f5;
    }
    .right{
        overflow: hidden;
        background-color: #c5c;
    }

css样式:

	.left,.center{
        float: left;
        width: 200px;
        margin-right: 10px;
        background-color: #5f5;
    }
    .right{
        overflow: hidden;
        background-color: #c5c;
    }
(2) 多列不定宽+1列自适应

示例:

这里写图片描述

html代码:

	<div class="parent">
        <div class="left">
            <p>left</p>
            <p>定宽</p>
        </div>
        <div class="center">
            <p>center</p>
            <p>定宽</p>
        </div>
        <div class="right">
            <p>right</p>
            <p>自适应</p>
        </div>
    </div>

css样式:

	.left,.center{
        float: left;
        margin-right: 10px;
        background-color: #5f5;
    }
    .right{
        overflow: hidden;
        background-color: #c5c;
    }

3、 等分布局

示例:

这里写图片描述

html代码:

	<div class="parent">
        <div class="one">
            <p>one</p>
        </div>
        <div class="two">
            <p>two</p>
        </div>
        <div class="three">
            <p>three</p>
        </div>
        <div class="four">
            <p>four</p>
        </div>
    </div>

css样式:

		/* 方法一 : float */
        .parent{
            margin-right:10px;
        }
        .one,.two,.three,.four{
            float: left;
            width: 24%;
            box-sizing: border-box;
            margin-left: 10px;
            background: #c5c;
        } 


         /* 方法二: flex */
        .parent{
            display: flex;
        }
        .one,.two,.three,.four{
           flex: 1;
           margin-left: 10px;
           background: #c5c;
        }


        /* 方法三 : table */
        .parent{
            display: table;
            width: 100%;
            table-layout: fixed;
        }
        .one,.two,.three,.four{
           display: table-cell;
           border: 1px solid #c5c;
        }

4、 等高布局

示例:

这里写图片描述

html代码:

	<div class="parent">
        <div class="left">
            <p>left</p>
            <p>等高布局</p>
        </div>
        <div class="right">
            <p>right</p>
            <p>等高布局</p>
        </div>
    </div>

css样式:

		/* 方法一: float */
        .parent{
            overflow: hidden;
        }
        .left,.right{
            padding-bottom:9999px;
            margin-bottom:-9999px;
        }
        .left{
            float: left;
            width: 200px;
            margin-right: 10px;
            background-color: #5f5;
        }
        .right{
            overflow: hidden;
            background-color: #c5c;
        } 

        /* 方法二: flex */
        .parent{
            display: flex;
        }
        .left{
            width: 200px;
            margin-right: 10px;
            background-color: #5f5;
        }
        .right{
            flex: 1;
            background-color: #c5c;
        }


        /*方法三: table*/
        .parent{
            display: table;
            width: 100%;
            table-layout: fixed;
        }
        .left,.right{
            display: table-cell;
        }
        .left{
            width: 200px;
            padding-right: 10px;
            background-color: #5f5;
        }
        .right{
            background-color: #c5c;
        }

方法一的实现原理:
垂直方向margin无法改变元素的内部尺寸,但却能改变外部尺寸,设置了margin-bottom:-9999px意味着元素的外部尺寸在垂直方向上小了9999px。默认情况下,垂直方向块级元素上下距离是0,一旦margin-bottom:-9999px就意味着后面所有元素和上面元素的空间距离变成了-9999px,也就是后面元素都往上移动了9999px。此时,通过padding-bottom:9999px增加元素高度,这正负一抵消,对布局层并无影响,但却带来了我们需要的东西—视觉层多了9999px高度的可使用的背景色。但是,9999px太大了,所以需要配合父级overflow:hidden把多出来的色块背景隐藏掉,于是实现了视觉上的等高布局效果。—— 《CSS世界》

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

英子的搬砖日志

您的鼓励是我创作的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值