前端问题总结(1):布局

前端问题总结(1):布局

1、盒模型宽度计算

<style type="text/css">
        #div1 {
            width: 100px;
            padding: 10px;
            border: 1px solid #ccc;
            margin: 10px;
            box-sizing: border-box;
        }
    </style>
<body>
    <div id="div1">
         div1   /*请问div1的offsetWidth是多大?*/
    </div>
</body>

宽度=内容+内边距+外边距=122px

2、margin的纵向重叠

style type="text/css">
        p {
            font-size: 16px;
            line-height: 1;
            margin-top: 10px;
            margin-bottom: 20px;
        }
    </style>
<body>
 /*请问A与B之间的距离?*/   
    <p>A</p>
    <p></p>
    <p></p>
    <p>B</p>

</body>

由于空白元素会重叠,上下相邻元素的margin—top和margin—bottom也会重叠,所以答案是20px

3、圣杯布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>圣杯布局</title>
    <style type="text/css">
        body {
            min-width: 550px;
        }
        #header {
            text-align: center;
            background-color: #f1f1f1;
        }

        #container {
            padding-left: 200px;
            padding-right: 150px;/*为两边留白*/
        }
        #container .column {
            float: left;
        }

        #center {
            background-color: #ccc;
            width: 100%;
        }
        #left {
            position: relative;   /*相较于自身移动 不影响其他的*/
            right: 200px;    /*向左移动200*/  
            background-color: yellow;
            width: 200px;
            margin-left: -100%;   /*往左拽 拽了整个中间的宽度*/
            
        }
        #right {
            background-color: red;
            width: 150px;
            margin-right: -150px; /*在外界看来 right 已经没有宽度了*/
        }                         /*所以他可以在上边,而不是浮动下来*/

        #footer {
            text-align: center;
            background-color: #f1f1f1;
        }

        /*  clearfix 清除浮动 */
        .clearfix:after {
            content: '';
            display: table;
            clear: both;
        }
        .clearfix{
            *zoom:1;  /*兼容性 */
        }
    </style>
</head>
<body>
    <div id="header">this is header</div>
    <div id="container" class="clearfix">
        <div id="center" class="column">this is center</div>
        <div id="left" class="column">this is left</div>
        <div id="right" class="column">this is right</div>
    </div>
    <div id="footer">this is footer</div>
</body>
</html>

4、双飞翼布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>双飞翼布局</title>
    <style type="text/css">
        body {
            min-width: 550px;
        }
        .col {
            float: left;
        }

        #main {
            width: 100%;
            height: 200px;
            background-color: #ccc;
        }
        #main-wrap {
            margin: 0 190px 0 190px;   /*通过margin来布局*/
        }

        #left {
            width: 190px;
            height: 200px;
            background-color: #0000FF;
            margin-left: -100%;/*往左拽 拽了整个中间的宽度*/
        }
        #right {
            width: 190px;
            height: 200px;
            background-color: #FF0000;
            margin-left: -190px;    /*向左拖拽190 */
        }
    </style>
</head>
<body>
    <div id="main" class="col">
        <div id="main-wrap">
            this is main
        </div>
    </div>
    <div id="left" class="col">
        this is left
    </div>
    <div id="right" class="col">
        this is right
    </div>
</body>
</html>

总结圣杯布局与双飞翼布局区别:圣杯布局利用padding留白,需要使用position:relative,而双飞翼布局利用margin为两边留白且不需要用到定位。相比而言,双飞翼布局更简单灵活些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值