Basic // page layout-two-column layout 2


1. Topic

Give your answer based on the question.
(The width of the right column is fixed, and the left column is adaptive.)
Title: The right width is fixed at 150px, and the left width is adaptive.

Second, my code details

https://codepen.io/janmie-cjm/pen/oNxMPBg

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>页面布局-两栏布局2</title>
  <style>
    html *{
     
     
      margin: 0;
      padding: 0
    }
    html {
     
     
      background: #ccccee
    }
    section {
     
     
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
  <!--  右宽度固定150px,左宽度自适应  -->
  <!-- 1-float + margin (需要浮动的内容放前面) -->
  <section>
    <style>
      .left1 {
     
     
        background: #cff;
        margin-right: 150px;
      }
      .right1 {
     
     
        background: #fcc;
        float: right;
        width: 150px;
        
      }
    </style>
    <article id="wrapper1">
      <div class="right1">
        <h4>右栏</h4>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
      </div>
      <div class="left1">
        <h1>1-float</h1>
        <p>左栏</p>
        <p>左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
      </div>
    </article>
  </section>

  <!-- 2-绝对定位 -->
  <section>
    <style>
      .wrapper2 {
     
     
        position: relative;
      }
      .left2 {
     
     
        background: #cff;
        position: absolute;
        left: 0;
        right: 150px;
      }
      .right2 {
     
     
        background: #fcc;
        width: 150px;
        position: absolute;
        right: 0;
      }
    </style>
    <article id="wrapper2">
      <div class="left2">
        <h1>2-绝对定位</h1>
        <p>左栏</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
      </div>
      <div class="right2">
        <h4>右栏</h4>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
      </div>
    </article>
  </section>

  <!-- 3-flexbox -->
  <section>
    <style>
      #wrapper3 {
     
     
        display: flex;
        margin-top: 300px;
      }
      .left3 {
     
     
        background: #cff;
        flex: 1;
      }
      .right3 {
     
     
        background: #fcc;
        width: 150px;

      }
    </style>
    <article id="wrapper3">
      <div class="left3">
        <h1>3-flexbox</h1>
        <p>左栏</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
      </div>
      <div class="right3">
        <h4>右栏</h4>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
        <p>右宽度固定150px</p>
      </div>
    </article>
  </section>

    <!-- 4-table -->
    <section>
      <style>
        #wrapper4 {
     
     
          display: table;
          width: 100%;
        }
        #wrapper4>div {
     
     
          display: table-cell;
        }
        .left4 {
     
     
          background: #cff;
        }
        .right4 {
     
     
          background: #fcc;
          width: 150px;
        }
      </style>
      <article id="wrapper4">
        <div class="left4">
          <h1>4-table</h1>
          <p>左栏</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        </div>
        <div class="right4">
          <h4>右栏</h4>
          <p>右宽度固定150px</p>
          <p>右宽度固定150px</p>
          <p>右宽度固定150px</p>
        </div>
      </article>
    </section>

    <!-- 5-grid -->
    <section>
      <style>
        #wrapper5 {
     
     
          display: grid;
          grid-template-rows: auto;
          grid-template-columns: auto 150px;
        }
        .left5 {
     
     
          background: #cff;
        }
        .right5 {
     
     
          background: #fcc;
        }
      </style>
      <article id="wrapper5">
        <div class="left5">
          <h1>5-grid</h1>
          <p>左栏</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
          <p>左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应左宽度自适应</p>
        </div>
        <div class="right5">
          <h4>右栏</h4>
          <p>右宽度固定150px</p>
          <p>右宽度固定150px</p>
          <p>右宽度固定150px</p>
        </div>
      </article>
    </section>
</body>
</html>

Three, summary

  1. What are the solutions to this problem?
    float / absolute positioning / flexbox / table layout / grid layout;

  2. The advantages and disadvantages of each program?
    1) Floating
    Excellent: Simple,
    Lack: Break away from the document flow and need to be combined with the BFC application.
    Note: You need to write the floating block level first! ! !
    2) Absolute positioning.
    Excellent: Simple . Shortage : Break
    away from the document flow and need to be combined with BFC applications. There are more codes.
    3) Excellent flexbox
    : Simple and fast, with good compatibility.
    4) Table layout.
    Excellent: Good compatibility . Shortage : The
    height of each column will change at the same time. ; The table needs to be rendered first, and the page generation speed is delayed.
    Note: The block level set to "display: table" needs to set width: 100%.
    5)
    Excellent grid layout : It can achieve complex operations with multiple rows and multiple columns, and has better compatibility. Okay, simplify the code

  3. Are there any compatible solutions?
    flexbox, grid layout


*There are still many shortcomings in the code. We hope to put forward different opinions and communicate with each other. *

Guess you like

Origin blog.csdn.net/weixin_37877794/article/details/108616645