Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

左侧固定右侧自适应两栏布局 #27

Open
xuexueq opened this issue Oct 20, 2018 · 0 comments
Open

左侧固定右侧自适应两栏布局 #27

xuexueq opened this issue Oct 20, 2018 · 0 comments
Labels
css css

Comments

@xuexueq
Copy link
Owner

xuexueq commented Oct 20, 2018

左侧固定右侧自适应两栏布局

以下六种方案中,只有方案二 flex 布局能保证两栏是等高的;其他方案两栏均不等高。

方案一:float + BFC

    #wrapper {
        overflow: hidden; // 清除浮动
    }

    .left {
        float: left;
        width: 200px;
    }

    .right {
        overflow: hidden; // 触发BFC
    }

方案二:flex (同时两栏等高)

    .wrapper {
        display: flex;
    }
    .left {
        width: 100px;
    }
    .right {
        flex: 1;
    }

方案三: float + margin-left

    #wrapper {
        overflow: hidden; // 清除浮动
    }
    .left{
        float: left;
        width: 100px;
    }
    .right {
        margin-left:100px;
    }

方案四: 双 float

    #wrapper {
        overflow: hidden;
    }

    .left, .right {
        float: left;
    }

    .left {
        width: 100px;
    }

    .right {
        width: calc(100% - 100px);
    }

方案五: 双 inline-block

    .wrapper {
        font-size: 0;
    }
    .left, .right {
        display: inline-block;
        font-size: 14px;
        vertical-align: top;
    }
    .left {
        width : 100px;
    }
    .right {
        width: calc(100% - 100px);
    } 

注意: 消除默认的间隙;

方案六: position + margin-left

    .wrapper {
        position: relative;
    }
    .left {
        position: absolute;
        width: 100px;
    }
    .right {
        margin-left: 100px;
    }

问题:绝对定位会脱离文档流,导致父元素不会计算该元素的高度,并且后面兄弟元素会叠加在该元素之上。

@xuexueq xuexueq added the css css label Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css css
Projects
None yet
Development

No branches or pull requests

1 participant