CSS常用布局方案

2020-05-29  本文已影响0人  前端_Fn

上中下一栏式布局

上中下各一栏

<style>
body {
    margin: 0;
}
#header {
    height: 64px;
    background-color: firebrick;
}
#content{
    height: 600px;
    background-color: moccasin;
}
#footer {
    height: 80px;
    background-color: grey;
}
</style>
<body>
    <header id="header">header</header>
    <section id="content">content</section>
    <footer id="footer">footer</footer>
</body>

左右两栏式布局

左侧固定,右侧自适应

<style>
body {
    margin: 0
}
.left {
    float: left;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    float: left;
    width: calc(100% - 200px);
    height: 600px;
    background-color: pink;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    <div>
</body>
<style>
body {
    margin: 0
}
.left {
    float: left;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    width: 200px;
    height: 600px;
    background-color: pink;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
</body>
<style>
body {
    margin: 0;
}
.main {
    display: flex;
}
.left {
    flex: 0 0 200px;
    height: 600px;
    background-color: orange;
}
.right {
    flex: 1;
    height: 600px;
    background-color: pink;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
</body>
<style>
body {
    margin: 0
}
.left {
    position: absolute;
    left: 0;
    top: 0;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    margin-left: 200px;
    height: 600px;
    background-color: pink;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
</body>

左右三栏式布局

左右固定,中间自适应

<style>
body {
    margin: 0;
}
.main {
    display: flex;
}
.left {
    width: 200px;
    background-color: orange;
}
.center{
    flex: 1 0 auto;
    background-color: gray;
}
.right {
    width: 200px;
    background-color: firebrick;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="center">中间</div>
        <div class="right">右侧</div>
    </div>
</body>
<style>
body {
    margin: 0;
}
.left {
    position: absolute;
    left: 0;
    width: 300px;
    height: 100px;
    background-color: orange;
}
.center {
    position: absolute;
    left: 300px;
    right: 300px;
    background-color: seagreen;
}
.right {
    position: absolute;
    right: 0;
    width: 300px;
    height: 100px;
    background-color: brown;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="center">中间</div>
        <div class="right">右侧</div>
    </div>
</body>
<style>
body {
    margin: 0;
}
.left {
    float: left;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    float: right;
    width: 200px;
    height: 600px;
    background-color: seagreen;
}
.center {
    margin-left: 200px;
    margin-right: 200px;
    height: 600px;
    background-color: gray;
}
.center::after {
    content: '';
    display: block;
    clear: both;
}
</style>
<body>
    <div class="main">
        <div class="left">左侧</div>
        <div class="right">右侧</div>
        <div class="center">中间</div>
    </div>
</body>
<style>
body {
    margin: 0
}
.layout-grid .main {
    display: grid;
    width: 100%;
    grid-template-columns: 300px auto 300px;
    grid-template-rows: 600px;
}
.layout-grid .left {
    background: orange;
}
.layout-grid .center {
    background: gray;
}
.layout-grid .right {
    background: pink;
}
</style>
<body>
    <div class="layout-grid">
        <div class="main">
            <div class="left">左侧</div>
            <div class="center">中间</div>
            <div class="right">右侧</div>
        </div>
    </div>
</body>

两边固定宽度,中间自适应,唯一区别是 dom 结构必须是先写中间列部分,这样实现中间列可以优先加载。

<style>
body {
    margin: 0;
}
.main {
    padding-left: 200px;
    padding-right: 200px;
}
.center {
    float: left;
    width: 100%;
    height: 600px;
    background-color: gray;
}
.left {
    float: left;
    margin-left: -100%;
    position: relative;
    left: -200px;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    float: left;
    margin-left: -200px;
    position: relative;
    right: -200px;
    width: 200px;
    height: 600px;
    background-color: firebrick;
}
</style>
<body>
    <div class="main">
        <div class="center">中间</div>
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
</body>

同样也是三栏布局,在圣杯布局基础上进一步优化,解决了圣杯布局错乱问题,实现了内容与布局的分离。而且任何一栏都可以是最高栏,不会出问题。

<style>
body {
    margin: 0;
}
.main {
    min-width: 400px;
}
.center {
    float: left;
    width: 100%;
    height: 600px;
    background-color: gray;
}
.inner {
    margin: 0 200px;
}
.left {
    float: left;
    margin-left: -100%;
    width: 200px;
    height: 600px;
    background-color: orange;
}
.right {
    float: left;
    margin-left: -200px;
    width: 200px;
    height: 600px;
    background-color: firebrick;
}
</style>
<body>
    <div class="main">
        <div class="center">
            <div class="inner">中间</div>
        </div>
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
</body>


上一篇 下一篇

猜你喜欢

热点阅读