圣杯布局及类似布局的多种实现思路

效果如图所示,为左(left)右(right)固定宽度,中间(center)自适应宽度的布局


 方法1:浮动+margin负值+相对定位(圣杯布局)

1. 首先搭建html结构,用一个container的盒子包裹center、left、right这三个盒子,要先渲染center盒子,故结构中center处在最前。container中三个盒子浮动,故需要清除浮动。

<body>
    <header>header</header>
    <div class="container clearfix">
        <div class="center">center</div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <footer>footer</footer>
</body>

2. 分别设置CSS样式。三个盒子都浮动,给container的padding-left为left的宽度,padding-right为right的宽度。center的宽度为100%。

<style>
    body {
        min-width: 550px;
        margin: 0;
        padding: 0;
    }
    
    div {
        box-sizing: border-box;
    }
    
    .clearfix::after,
    .clearfix::before {
        content: "";
        display: table;
    }
    
    .clearfix::after {
        clear: both;
    }
    
    .clearfix {
        *zoom: 1;
    }
    
    header,
    footer {
        display: block;
        height: 100px;
        background-color: #ccc;
    }
    
    .container {
        padding: 0 150px 0 200px;
    }
    
    .center,
    .left,
    .right {
        float: left;
        height: 200px;
    }
    
    .center {
        width: 100%;
        background-color: skyblue;
    }
    
    .left {
        width: 200px;
        background-color: antiquewhite;
    }
    
    .right {
        width: 150px;
        background-color: pink;
    }
</style>

3. 效果如图所示,由于三个盒子浮动,center宽度为100%,故left和right盒子只能换行显示

 4. 要将left盒子移到center左边,首先给一个margin-left: -100%,再用相对定位移到左边的空白处,CSS代码及效果图如下

<style>
    .left {
    position: relative;
    left: -200px;
    margin-left: -100%;
    width: 200px;
    background-color: antiquewhite;
    }
</style>

5. 将right盒子用margin-right: -150px移动到center右边空白处。

<style>
    .right {
    margin-right: -150px;
    width: 150px;
    background-color: pink;
    }
</style>


 方法二:浮动+margin负值(双飞翼布局)

1. 搭建html结构如下,center、left、right盒子浮动,故box需要清除浮动。同样是先渲染center,故结构中center在最前。

<body>
    <header>header</header>
    <div class="box clearfix">
        <div class="center">
            <div class="content">center</div>
        </div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <footer>footer</footer>
</body>

2. center盒子宽度为父级100%,其中的content盒子给左右margin分别为left和right的宽度。CSS代码及效果图如下:

<style>
    body {
        min-width: 500px;
        margin: 0;
        padding: 0;
    }
    
    div {
        box-sizing: border-box;
    }
    
    .clearfix::after,
    .clearfix::before {
        content: "";
        display: table;
    }
    
    .clearfix::after {
        clear: both;
    }
    
    .clearfix {
        *zoom: 1;
    }
    
    header,
    footer {
        display: block;
        height: 100px;
        background-color: #ccc;
    }
    
    .center,
    .left,
    .right {
        float: left;
        height: 200px;
    }
    
    .center {
        width: 100%;
    }
    
    .content {
        height: 200px;
        margin: 0 150px 0 200px;
        background-color: skyblue;
    }
    
    .left {
        width: 200px;
        background-color: antiquewhite;
    }
    
    .right {
        width: 150px;
        background-color: pink;
    }
</style>

3. left盒子用margin-left: -100%移到center左边空白处。代码及效果图如下:

<style>
    .left {
        margin-left: -100%;
        width: 200px;
        background-color: antiquewhite;
    }
</style>

4. 将right盒子用margin-left: -150px移到center右边空白处。代码及效果图如下:

<style>
    .right {
        margin-left: -150px;
        width: 150px;
        background-color: pink;
    }
</style>


 方法三:浮动+calc()函数

1. 搭建html结构如下,left、center、right都需要浮动。

<body>
    <header>header</header>
    <div class="container clearfix">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
    <footer>footer</footer>
</body>

2. left、right给固定宽度,center宽度用函数calc(100% - 350px)表示,350px即为left和right宽度之和。CSS代码及效果图如下:

<style>
    body {
        min-width: 500px;
        margin: 0;
        padding: 0;
    }
    
    div {
        box-sizing: border-box;
    }
    
    .clearfix::after,
    .clearfix::before {
        content: "";
        display: table;
    }
    
    .clearfix::after {
        clear: both;
    }
    
    .clearfix {
        *zoom: 1;
    }
    
    header,
    footer {
        display: block;
        height: 100px;
        background-color: #ccc;
    }
    
    .center,
    .left,
    .right {
        float: left;
        height: 200px;
    }
    
    .left {
        width: 200px;
        background-color: antiquewhite;
    }
    
    .center {
        width: calc(100% - 350px);
        background-color: skyblue;
    }
    
    .right {
        width: 150px;
        background-color: pink;
    }
</style>


 方法四:绝对定位+margin

1. html结构如下:

<body>
    <header>header</header>
    <div class="container">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
    <footer>footer</footer>
</body>

2. left、right给固定宽度,并且使用绝对定位,分别定到左侧及右侧,center盒子不给宽度(默认占满一行)给左右的margin值分别为left和right的宽度。CSS代码及效果图如下:

<style>
    body {
        min-width: 500px;
        margin: 0;
        padding: 0;
    }
    
    div {
        box-sizing: border-box;
    }
    
    header,
    footer {
        display: block;
        height: 100px;
        background-color: #ccc;
    }
    
    .container {
        position: relative;
    }
    
    .center {
        height: 200px;
        margin: 0 150px 0 200px;
        background-color: skyblue;
    }
    
    .left,
    .right {
        position: absolute;
        top: 0;
        width: 200px;
        height: 200px;
        background-color: antiquewhite;
    }
    
    .left {
        left: 0;
    }
    
    .right {
        right: 0;
        width: 150px;
        background-color: pink;
    }
</style>


 方法五:flex布局

1. html结构如下:

<body>
    <header>header</header>
    <div class="container">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
    <footer>footer</footer>
</body>

2. container采用flex弹性布局,给left和right设置固定宽度,center设置flex: 1。CSS代码及效果图如下:

<style>
    body {
        min-width: 500px;
        margin: 0;
        padding: 0;
    }
    
    div {
        box-sizing: border-box;
    }
    
    header,
    footer {
        display: block;
        height: 100px;
        background-color: #ccc;
    }
    
    .container {
        display: flex;
        height: 200px;
    }
    
    .left {
        width: 200px;
        background-color: antiquewhite;
    }
    
    .center {
        flex: 1;
        background-color: skyblue;
    }
    
    .right {
        width: 150px;
        background-color: pink;
    }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值