温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS三栏布局怎么弄

发布时间:2020-10-16 16:19:48 来源:亿速云 阅读:220 作者:小新 栏目:web开发

这篇文章给大家分享的是有关CSS三栏布局怎么弄的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

对于前端来说,布局也是必须掌握的,一个好的布局可以让页面看起来更美观。提到布局,那就不得不说CSS三栏布局。这是前端面试经常会问到的一个问题,算是基础题。所谓的三栏布局,一般是指左右两边固定中间自适应,或者是中间固定左右两边自适应。

左右两边固定中间自适应

圣杯布局

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right(注意,main在写在前面,这样在页面渲染时会先加载中间,针对面试题优先加载中间部分)

style样式设置

1、父元素设置高度
 2、三个元素均设置浮动
 3、中间main部分定宽100%:width: 100%,左右两边按产品需求设置宽高
 4、左边设置margin-left: -100%;右边设置margin-right: -右盒子宽
 5、父元素设置padding-left: 左盒子宽;padding-right: 右盒子宽
 6、左右盒子相对定位

<div class="container">
  <div class="main f">go aheadgo aheadvgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo ahead</div>
  <div class="left f"></div>
  <div class="right f"></div>
</div>
<style>
  body {
    min-width: 700px;
  }
  .container {
     height: 300px;
     padding: 0 200px 0 200px;
  }
  .f {
     float: left;
  }
  .main {
     width: 100%;
     height: 300px;
     background-color: cornflowerblue;
  }
  .left {
     width: 200px;
     height: 300px;
     background-color: indianred;
     margin-left: -100%;
     position: relative;
     left: -200px;
  }
  .right {
     width: 200px;
     height: 300px;
     background-color: lightgreen;
     margin-left: -200px;
     position: relative;
     right: -200px;
  }
</style>

该布局受内部元素影响而破坏布局的概率低,但是当浏览器屏幕缩小的一定程度时,左右两侧的内容会掉下来,或发生重叠现象。解决方案,给body加一个最小宽度(起码大于左右两侧宽度之和)

双飞翼布局

与圣杯布局的思路是一致的,只是有一些细微的差别。

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right(注意,main在写在前面,这样在页面渲染时会先加载中间,针对面试题优先加载中间部分)

style样式设置

1、父元素设置高度
 2、三个元素均设置浮动
 3、中间main部分定宽100%:width: 100%,左右两边按产品需求设置宽高
 4、中间main部分再加一个盒子inner,放置内容(与圣杯布局的不同点)
 5、左边设置margin-left: -100%;右边设置margin-right: -右盒子宽
 6、新添加盒子,inner,设置左右padding或margin

<div class="container">
   <div class="main f">
      <div class=inner>go aheadgo aheadvgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo aheadgo ahead</div>
   </div>
   <div class="left f"></div>
   <div class="right f"></div>
</div>
<style>
  .container {
     height: 300px;
  }
  .f {
     float: left;
  }
  .main {
     width: 100%;
     height: 300px;
     background-color: cornflowerblue;
  }
  .left {
     width: 200px;
     height: 300px;
     background-color: indianred;
     margin-left: -100%;
  }
  .right {
     width: 200px;
     height: 300px;
     background-color: lightgreen;
     margin-left: -200px;
  }
  .inner {
    padding: 0 200px 0 200px;
  }
</style>
自身浮动

HTML结构设置

新建三个元素:left、right、main(注意,main写在后面)

style样式设置

1、左盒子左浮动,右盒子右浮动
 2、中间部分设置margin或padding值

<div class="left"></div>
<div class="right"></div>
<div class="main">我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容</div>
<style>
    .main {
        margin: 0 200px 0 200px;
        background-color: red;
        height: 200px;
    }
    .left {
        float: left;
        width: 200px;
        background-color: blue;
        height: 200px;
    }
    .right {
        float: right;
        width: 200px;
        background-color: pink;
        height: 200px;
    }
</style>
CSS3新特性:flex

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right(注意,main写在中间)

style样式设置

1、父元素设置宽度为100%,display: flex;
 2、左右两则按产品需求设置宽高
 3、中间部分设置flex: 1;

<div class="container">
  <div class="left"></div>
  <div class="main">我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容我是中间内容</div>
  <div class="right"></div>
</div>
<style>
    .container {
       width: 100%;
       height: 200px;
       display: flex;
   }
   .main {
       flex: 1;
       background-color: red;
       height: 200px;
   }
   .left {
       width: 200px;
       background-color: blue;
       height: 200px;
   }
   .right {
       width: 200px;
       background-color: pink;
       height: 200px;
   }
</style>

还有其他的写法,这里就不一一赘述,只是列举了一些比较常用的,以及面试可能会问到的情况。CSS3还有很多好玩的特性,在工作和学习的过程中值得深入研究。

中间固定左右两边自适应

浮动 + 负边距 (圣杯布局)

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right(注意,main写在中间)

style样式设置

1、左右两边各占50%的宽度
 2、左边负边距 margin-left 占中间p宽度的一半
 3、右边负边距 margin-right 也占中间p宽度的一半

 <div class="container">
   <div class="left"></div>
   <div class="main">我是中间内容</div>
   <div class="right"></div>
 </div>
 <style>
    .main {
        width: 100px;
        text-align: center;
        float: left;
        background-color: lightgreen;
        height: 300px;
    }
    .left {
        height: 300px;
        float: left;
        width: 50%;
        margin-left: -50px;
        background-color: pink;
    }
    .right {
        height: 300px;
        float: right;
        width: 50%;
        margin-right: -50px;
        background-color: cornflowerblue;
    }
 </style>
CSS3新特性:flex

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right

style样式设置

1、父元素设置display: flex;flex-direction: row;
 2、左右设置flex-grow: 1,平分剩余空间

 <div class="container">
   <div class="left"></div>
   <div class="main">我是中间内容</div>
   <div class="right"></div>
 </div>
 <style>
    .container {
        display: flex;
        flex-direction : row;
    }
    .main {
        width: 200px;
        height: 300px;
        text-align: center;
        background-color: lightgreen;
    }
    .left {
        height: 300px;
        flex-grow: 1;
        background-color: pink;
    }
    .right {
        height: 300px;
        flex-grow: 1;
        background-color: cornflowerblue;
    }
 </style>
CSS3特性 calc(四则运算)

用于动态计算长度值。需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 50px)。

HTML结构设置

新建一个父元素,包含三个子元素:left、main、right

style样式设置

1、父元素设置100%宽;
 2、左右设置width: calc(50%, - 中间宽/2)

 <div class="container">
   <div class="left"></div>
   <div class="main">我是中间内容</div>
   <div class="right"></div>
 </div>
 .container {
     width: 100%;
     height: 300px;
 }
 .f {
     float: left;
 }
 .main {
     width: 100px;
     text-align: center;
     background-color: lightgreen;
     height: 300px;
 }
 .left {
     height: 300px;
     background-color: pink;
     width: calc(50% - 50px);  /*平分中间部分的宽度*/
 }
 .right {
     height: 300px;
     background-color: cornflowerblue;
     width: calc(50% - 50px);  /*平分中间部分的宽度*/
 }

感谢各位的阅读!关于CSS三栏布局怎么弄就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI