Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                53

<head>
  <meta charset="utf-8">

<body>
  <div>
    文章地址:<a href="https://juejin.cn/post/6960844183611375630" target="_blank">【CSS】CSS布局解决方案(终结版)</a>
  </div>

  <div>
    <h1>2.1.1 水平垂直居中布局 - 固定宽高 - absolute + 负margin</h1>
    <div class="father211">
      <div class="son211"></div>
    </div>
  </div>

  <div>
    <h1>2.1.2 水平垂直居中布局 - 固定宽高 - absolute + margin auto</h1>
    <div class="father212">
      <div class="son212"></div>
    </div>
  </div>

  <div>
    <h1>2.2.1 水平垂直居中布局 - 不固定宽高 - absolute + transform</h1>
    <div class="father221">
      <div class="son221"></div>
    </div>
  </div>

  <div>
    <h1>2.2.2 水平垂直居中布局 - 不固定宽高 - flex</h1>
    <div class="father222">
      <div class="son222"></div>
    </div>
  </div>

  <div>
    <h1>2.2.3 水平垂直居中布局 - 不固定宽高 - table布局</h1>
    <div class="father223">
      <div class="son223"></div>
    </div>
  </div>

  <div>
    <h1>3.1 单列布局 - 普通布局</h1>
    <div class="container31">
      <div class="header31"></div>
      <div class="content31"></div>
      <div class="footer31"></div>
    </div>
  </div>

  <div>
    <h1>3.2 单列布局 - 内容居中</h1>
    <div class="container32">
      <div class="header32"></div>
      <div class="content32"></div>
      <div class="footer32"></div>
    </div>
  </div>

  <div>
    <h1>4.1 两栏布局 - 左侧定宽、右侧自适应 - float</h1>
    <div class="container">
      <div class="left41"></div>
      <div class="right41"></div>
    </div>
  </div>

  <div>
    <h1>4.2 两栏布局 - 左侧定宽、右侧自适应 - flex</h1>
    <div class="container42">
      <div class="left42"></div>
      <div class="right42"></div>
    </div>
  </div>

  <div>
    <h1>5.1 三栏布局 - 左右固定、中间自适应 - flex布局</h1>
    <div class="box51">
      <div class="center51">中</div>
      <div class="left51">左</div>
      <div class="right51">右</div>
    </div>
  </div>

  <div>
    <h1>5.2 三栏布局 - 左右固定、中间自适应 - 圣杯布局</h1>
    <div class="box52">
      <div class="center52">中哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈中</div>
      <div class="left52">左</div>
      <div class="right52">右</div>
    </div>
  </div>

  <div>
    <h1>5.3 三栏布局 - 左右固定、中间自适应 双飞翼局(TODO 真难弄懂)</h1>
    <p>让你快速掌握float和position - 知乎 https://zhuanlan.zhihu.com/p/142630896</p>
    <div class="box53">
      <div class="center53">
        <div class="content53">
          中哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈中
        </div>
      </div>
      <div class="left53">左</div>
      <div class="right53">右</div>
    </div>
  </div>

  <div>
    <h1>6.1 等高布局 - 正值内边距+负值外边距</h1>
    <div class="container61">
      <div class="left61">左左左</div>
      <div class="right61">
        <p>右右右右</p>
        <p>右右右右</p>
        <p>右右右右</p>
      </div>
    </div>
  </div>

  <div>
    <h1>6.3 等高布局 - flex布局</h1>
    <div class="container63">
      <div class="left63">左左左</div>
      <div class="right63">
        <p>右右右右</p>
        <p>右右右右</p>
        <p>右右右右</p>
      </div>
    </div>
  </div>

</body>
              
            
!

CSS

              
                /* 2.1.1 水平垂直居中布局 - 固定宽高 - absolute + 负margin */
.father211 {
  position: relative;
  width: 500px;
  height: 300px;
  border: 1px solid red;
  background-color: black;
}
.son211 {
  position: absolute;
  /*水平居中*/
  left: 50%;
  /* 向左偏移本身一半宽度 */
  margin-left: -50px;
  /*垂直居中*/
  top: 50%;
  /* 向上偏移本身一半高度 */
  margin-top: -50px;
  width: 100px;
  height: 100px;
  background-color: red;
}

/** 2.1.2 水平垂直居中布局 - 固定宽高 - absolute + margin auto */
.father212 {
  position: relative;

  width: 500px;
  height: 300px;
  border: 1px solid red;
  background-color: black;
}
.son212 {
  position: absolute;
  margin: auto;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;

  width: 100px;
  height: 100px;
  background-color: red;
}

/* 2.2.1 水平垂直居中布局 - 不固定宽高 - absolute + transform */
.father221 {
  position: relative;
  width: 500px;
  height: 300px;
  border: 1px solid red;
  background-color: black;
}
.son221 {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  width: 100px;
  height: 100px;
  background-color: red;
}

/* 2.2.2 水平垂直居中布局 - 不固定宽高 - flex */
.father222 {
  display: flex;
  justify-content: center; /*水平居中*/
  align-items: center; /*垂直居中*/

  width: 500px;
  height: 300px;
  border: 1px solid red;
  background-color: black;
}
.son222 {
  width: 100px;
  height: 100px;
  background-color: red;
}

/* 2.2.3 水平垂直居中布局 - 不固定宽高 - flex */
.father223 {
  display: table-cell;
  text-align: center;
  vertical-align: middle;

  width: 500px;
  height: 300px;
  border: 1px solid red;
  background-color: black;
}
.son223 {
  display: inline-block;

  width: 100px;
  height: 100px;
  background-color: red;
}

/** 3.1 单列布局 - 普通布局 **/
.header31 {
  margin: 0 auto;
  max-width: 960px;
  height: 100px;
  background-color: pink;
}
.content31 {
  margin: 0 auto;
  max-width: 960px;
  height: 300px;
  background-color: aquamarine;
}
.footer31 {
  margin: 0 auto;
  max-width: 960px;
  height: 100px;
  background-color: skyblue;
}

/** 3.2 单列布局 - 内容居中 **/
.header32 {
  margin: 0 auto;
  height: 100px;
  background-color: pink;
}
.content32 {
  margin: 0 auto;
  width: 960px;
  height: 300px;
  background-color: aquamarine;
}
.footer32 {
  margin: 0 auto;
  height: 100px;
  background-color: skyblue;
}

/** 4.1 两栏布局 - 左侧定宽、右侧自适应 - float **/
.left41 {
  float: left;
  width: 300px;
  height: 500px;
  background-color: pink;
}
.right41 {
  width: 100%;
  height: 500px;
  background-color: aquamarine;
}

/** 4.2 两栏布局 - 左侧定宽、右侧自适应 - flex **/
.container42 {
  display: flex;
}
.left42 {
  width: 300px;
  height: 500px;
  background-color: pink;
}
.right42 {
  flex: 1;
  width: 100%;
  height: 500px;
  background-color: aquamarine;
}

/** 5.1 三栏布局 - 左右固定、中间自适应 - flex布局 **/
* {
  margin: 0;
  padding: 0;
}
.box51 {
  min-width: 800px;
  height: 600px;
  background: gray;
  display: flex;
}
.left51 {
  order: 0;
  width: 200px;
  height: 600px;
  background: pink;
}
.center51 {
  flex: 1;
  order: 1;
  width: 100%;
  height: 600px;
  background: aquamarine;
}
.right51 {
  order: 2;
  width: 200px;
  height: 600px;
  background: skyblue;
}

/** 5.2 三栏布局 - 左右固定、中间自适应 - 圣杯布局 **/
* {
  margin: 0;
  padding: 0;
}
.box52 {
  min-width: 800px;
  height: 600px;
  /* 4、padding左右盒子的宽度 */
  padding-left: 200px;
  padding-right: 200px;
  background-color: black;
}
.left52 {
  float: left;
  margin-left: -100%;
  position: relative;
  left: -200px;
  width: 200px;
  height: 600px;
  background: pink;
}
.center52 {
  float: left;
  width: 100%;
  height: 600px;
  background: aquamarine;
}
.right52 {
  float: left;
  margin-left: -200px;
  position: relative;
  right: -200px;
  width: 200px;
  height: 600px;
  background: skyblue;
}

/** 5.3 三栏布局 - 左右固定、中间自适应 双飞翼局 **/
* {
  margin: 0;
  padding: 0;
}
.box53 {
  min-width: 800px;
  height: 600px;
}
.left53 {
  float: left;
  width: 200px;
  height: 600px;
  margin-left: -100%;
  background: pink;
}
.content53 {
  margin-left: 200px;
  margin-right: 200px;
  background: yellowgreen;
}
.center53 {
  float: left;
  width: 100%;
  height: 600px;
  background: aquamarine;
}
.right53 {
  float: left;
  width: 200px;
  height: 600px;
  margin-left: -200px;
  background: skyblue;
}

/** 6.1 等高布局 - 正值内边距+负值外边距 **/
* {
  margin: 0;
  padding: 0;
}
.container61 {
  width: 400px;
  margin: 0 auto;
  background-color: black;
  overflow: hidden;
}
.left61,
.right61 {
  float: left;
  width: 50%;
  text-align: center;
  background-color: aquamarine;
  /* 设置正值内边距会把背景颜色拉伸 */
  padding-bottom: 9999px;
  /* 设置负值外边距把边框往里推 */
  margin-bottom: -9999px;
}
.right61 {
  background-color: pink;
}

/** 6.1 6.3 等高布局 - flex布局 **/
* {
  margin: 0;
  padding: 0;
}
.container63 {
  display: flex;
  width: 400px;
  margin: 0 auto;
}
.left63,
.right63 {
  text-align: center;
  background-color: aquamarine;
  flex: 1;
}
.right63 {
  background-color: pink;
}

              
            
!

JS

              
                
              
            
!
999px

Console