其他分享
首页 > 其他分享> > html+css 三列布局

html+css 三列布局

作者:互联网

前言

在开发过程中,经常会碰到三列布局,就是左右两侧定,中间自适应,如果想看两列布局,前面文章有写两列布局

上代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>

        <style>
            * {
                margin: 0;
                padding: 0;
            }

            body {
                color: #fff;
            }

            .grail-info {
                color: #000;
                margin-bottom: 5px;
            }

            .split {
                height: 10px;
            }

            .g-header, .g-footer {
                height: 100px;
                text-align: center;
                line-height: 100px;
                background: red;
            }

            .g-footer {
                position: fixed;
                bottom: 0;
                clear: both;
                width: 100%;
                background-color: blue;
            }

            /* float + margin */
            .f-content {
                height: 200px;
                line-height: 200px;
                text-align: center;
                overflow: hidden;
            }

            .f-left {
                float: left;
                width: 200px;
                height: 100%;
                background: black;
            }

            .f-center {
                margin-left: 200px;
                margin-right: 300px;
                height: 100%;
                background: green;
            }

            .f-right {
                float: right;
                width: 300px;
                height: 100%;
                background: orange;
            }

            /* 圣杯 */
            .g-content {
                padding: 0 300px 0 200px;
                text-align: center;
                height: 200px;
                line-height: 200px;
                overflow: hidden;
            }

            .g-left {
                position: relative;
                float: left;
                margin-right: -200px;
                right: 200px;
                width: 200px;
                height: 100%;
                background: black;
            }

            .g-center {
                float: left;
                width: 100%;
                height: 100%;
                background: green;
            }

            .g-right {
                float: left;
                width: 300px;
                height: 100%;
                margin-right: -300px;
                background: orange;
            }

            /* 双飞翼 */
            .w-content {
                height: 200px;
                line-height: 200px;
                text-align: center;
                overflow: hidden;
            }

            .w-left {
                float: left;
                width: 200px;
                margin-right: -200px;
                height: 100%;
                background: black;
            }

            .w-center {
                float: left;
                width: 100%;
                height: 100%;
            }

            .center-content {
                margin-left: 200px;
                margin-right: 300px;
                background: green;
            }

            .w-right {
                float: left;
                width: 300px;
                margin-left: -300px;
                height: 100%;
                background: orange;
            }

        </style>
    </head>
    <body>

        <!-- setting -->
        <div class="grail-info">
            <span>左边宽度:</span> <input type="text" id="left-width" name="name" value="200" οnchange="grailChange()">

            <span>右边宽度:</span> <input type="text" id="right-width" name="name" value="300" οnchange="grailChange()">
        </div>

        <!-- 两侧宽度固定,中间宽度自适应 -->
        <!-- header -->
        <header class="g-header">g-header</header>

        <div class="split"></div>

        <!-- float -->
        <div class="f-content">
            <div class="f-left" id="f-left">f-left</div>
            <div class="f-right" id="f-right">f-right</div>
            <div class="f-center" id="f-center">f-center</div>
        </div>

        <div class="split"></div>

        <!-- 圣杯 -->
        <div class="g-content" id="g-content">
            <div class="g-left" id="g-left">g-left</div>
            <div class="g-center">g-center</div>
            <div class="g-right" id="g-right">g-right</div>
        </div>

        <div class="split"></div>


        <!-- 双飞翼 -->
        <div class="w-content">
            <div class="w-left" id="w-left">w-left</div>
            <div class="w-center">
                <div class="center-content" id="center-content">w-center</div>
            </div>
            <div class="w-right" id="w-right">w-right</div>
        </div>

        <!-- footer -->
        <footer class="g-footer">g-footer</footer>


        <script type="text/javascript">
            function grailChange() {
                const $left = document.getElementById('left-width').value
                const $right = document.getElementById('right-width').value

                // margin + float
                const $fLeft = document.getElementById('f-left')
                const $fRight = document.getElementById('f-right')
                const $fCenter = document.getElementById('f-center')

                $fLeft.style.width = $left + 'px'
                $fRight.style.width = $right + 'px'
                $fCenter.style.marginLeft = $left + 'px'
                $fCenter.style.marginRight = $right + 'px'


                // 圣杯
                const $gContent = document.getElementById('g-content')
                const $gLeft = document.getElementById('g-left')
                const $gRight = document.getElementById('g-right')


                $gContent.style.paddingLeft = $left + 'px'
                $gContent.style.paddingRight = $right + 'px'

                $gLeft.style.width = $left + 'px'
                $gLeft.style.marginRight = -$left + 'px'
                $gLeft.style.right = $left + 'px'

                $gRight.style.width = $right + 'px'
                $gRight.style.marginRight = -$right + 'px'

                // 双飞翼
                const $wLeft = document.getElementById('w-left')
                const $cCenter = document.getElementById('center-content')
                const $wRight = document.getElementById('w-right')

                $wLeft.style.width = $left + 'px'
                $wLeft.style.marginRight = -$left + 'px'

                $cCenter.style.marginLeft = $left + 'px'
                $cCenter.style.marginRight = $right + 'px'

                $wRight.style.width = $right + 'px'
                $wRight.style.marginLeft = -$right + 'px'

            }
        </script>
    </body>
</html>

布局方式

标签:style,right,px,三列,height,html,margin,css,left
来源: https://blog.csdn.net/Shaoyouiqng/article/details/114781109