css中,在高度已知,写出三栏布局,其中左栏、右栏宽度各位300px,中间自适应...

解决方案主要有五种

首先写入全局样式

<style type="text/css">
      html * {
        margin: 0;
        padding: 0;
      }
      .layout {
        margin-top: 20px;
      }
      .layout article div {
        min-height: 100px;
      }
    </style>

 

1、用浮动解决方案

缺点:清除浮动,脱离文档流

优点:兼容性好

 <section class="layout float">
      <style media="screen">
        .layout.float .left {
          float: left;
          width: 300px;
          background: red;
        }
        .layout.float .right {
          float: right;
          width: 300px;
          background: blue;
        }
        .layout.float .center {
          background: yellow;
          text-align: center;
        }
      </style>
      <article class="left-right-center">
        <div class="left"></div>
        <div class="right"></div>
        <div class="center">
          <h1>浮动解决方案</h1>
          1、这是三栏布局中间部分 2、这是三栏布局中间部分
        </div>
      </article>
    </section>

2、绝对定位解决方案

缺点:布局脱离文档流

优点:快捷

<section class="layout absolute">
      <style media="screen">
        .layout.absolute .left-center-right > div {
          position: absolute;
        }
        .layout.absolute .left {
          background: red;
          width: 300px;
          left: 0;
        }
        .layout.absolute .right {
          background: blue;
          width: 300px;
          right: 0;
        }
        .layout.absolute .center {
          background: yellow;
          left: 300px;
          right: 300px;
          text-align: center;
        }
      </style>
      <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
          <h2>绝对定位解决方案</h2>
          1、这是三栏布局绝对定位中间部分 2、这是三栏布局绝对定位中间部分
        </div>
        <div class="right"></div>
      </article>
    </section>

3、flexbox布局解决方案

优点:为了解决绝对定位和浮动不足,基本比较完美,移动端基本是flexbox

<section class="layout flexbox">
      <style>
        .layout.flexbox {
          margin-top: 150px;
        }
        .layout.flexbox .left-center-right {
          display: flex;
        }
        .layout.flexbox .left {
          width: 300px;
          background: red;
        }
        .layout.flexbox .right {
          width: 300px;
          background: blue;
        }
        .layout.flexbox .center {
          flex: 1;
          background: yellow;
          text-align: center;
        }
      </style>
      <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
          <h2>flexbox解决方案</h2>
          1、这是三栏布局flexbox中间部分 2、这是三栏布局flexbox中间部分
        </div>
        <div class="right"></div>
      </article>
    </section>

4、表格布局解决方案

 

缺点:当某个单元格高度超过表格时,其他单元格也会调整高度

 

优点:表格在很多场景中还是很适用的,兼容性很好,当需要兼容ie8时

 

 <section class="layout table">
      <style>
        .layout.table .left-center-right {
          width: 100%;
          display: table;
          height: 100px;
        }
        .layout.table .left-center-right > div {
          display: table-cell;
        }
        .layout.table .left {
          width: 300px;
          background: red;
        }
        .layout.table .center {
          background: yellow;
          text-align: center;
        }
        .layout.table .right {
          width: 300px;
          background: blue;
        }
      </style>
      <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
          <h2>表格布局解决方案</h2>
          1、这是三栏布表格布局中间部分 2、这是三栏布表格布局中间部分
        </div>
        <div class="right"></div>
      </article>
    </section>

5、grid布局解决方案

 

缺点:

 

优点:代码简化

 

 <section class="layout grid">
      <style media="screen">
        .layout.grid .left-center-right {
          display: grid;
          width: 100%;
          grid-template-rows: 100px;
          grid-template-columns: 300px auto 300px;
        }
        .layout.grid .left {
          background: red;
        }
        .layout.grid .center {
          background: yellow;
          text-align: center;
        }
        .layout.grid .right {
          background: blue;
        }
      </style>
      <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
          <h2>网格布局解决方案</h2>
          1、这是三栏布表网格布局中间部分 2、这是三栏布网格布局中间部分
        </div>
        <div class="right"></div>
      </article>
    </section>

 

转载于:https://www.cnblogs.com/yongwunaci/p/10720824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: <div style="width:100%;"> <div style="float:left;width:30%;">左栏内容</div> <div style="float:left;width:40%;margin-left:2%;">中间内容</div> <div style="float:right;width:30%;">右栏内容</div> </div> ### 回答2: 可以使用HTML和CSS来创建一个三栏布局,其左右两列的宽度固定,而中间列的宽度自适应。 首先,我们需要使用HTML创建一个三列的容器,可以使用`<div>`标签来实现。代码如下: ```html <div class="container"> <div class="left-column"></div> <div class="middle-column"></div> <div class="right-column"></div> </div> ``` 接下来,我们可以使用CSS来设置样式,以实现固定宽度自适应宽度的效果。首先,我们设置三列容器的样式,将其设置为一行布局,并使用`display: flex;`属性,使其自动伸缩。同时,设置容器的宽度100%,以确保占据整个容器的宽度。代码如下: ```css .container { display: flex; width: 100%; } ``` 接下来,我们为左右三列设置样式。设置左右两列的宽度为固定值,例如200像素。代码如下: ```css .left-column { width: 200px; } .right-column { width: 200px; } ``` 为了让中间自适应宽度,我们可以使用`flex-grow`属性,设置中间列的伸缩比例为1,使其自动填充剩余的宽度。代码如下: ```css .middle-column { flex-grow: 1; } ``` 最后,为了使布局更加美观,我们可以为三列容器和内的列添加一些样式,例如背景色和内边距。代码如下: ```css .container { background-color: #f1f1f1; padding: 10px; } .left-column, .middle-column, .right-column { background-color: #e0e0e0; padding: 10px; margin: 5px; } ``` 通过这样设置,我们就可以得到一个具有固定宽度左右列和自适应宽度中间列的三栏布局注意,以上代码只是一个示例,你可以根据自己的需求进行调整和修改。 ### 回答3: 要实现一个左右宽度固定中间自适应三栏布局,可以使用HTML和CSS进行编。 首先,在HTML,我们可以使用```<div>```标签来创建三个元素,分别代表左栏右栏。例如: ``` <div class="left-column">左栏内容</div> <div class="middle-column">内容</div> <div class="right-column">右栏内容</div> ``` 然后,我们需要使用CSS来设置这些元素的样式。首先,设置左栏右栏宽度固定,可以使用```width```属性进行设置,例如: ``` .left-column { width: 200px; } .right-column { width: 200px; } ``` 接下来,设置宽度自适应,可以使用```flexbox```布局来实现。在CSS,将父元素设置为```display: flex;```,然后将设置为```flex-grow: 1;```,这样宽度将会自适应。例如: ``` body { display: flex; } .middle-column { flex-grow: 1; } ``` 最后,可以添加一些样式来美化布局,如设置背景颜色、边框样式等。 完整的HTML和CSS代码如下: ``` <!DOCTYPE html> <html> <head> <style> .left-column { width: 200px; background-color: lightgray; } .middle-column { flex-grow: 1; background-color: white; } .right-column { width: 200px; background-color: lightgray; } </style> </head> <body> <div class="left-column">左栏内容</div> <div class="middle-column">内容</div> <div class="right-column">右栏内容</div> </body> </html> ``` 通过上述HTML和CSS代码,我们实现了一个左右宽度固定中间自适应三栏布局

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值