美文网首页
CSS常用布局之——等分等高解决方案

CSS常用布局之——等分等高解决方案

作者: 不懂小彬 | 来源:发表于2017-12-28 14:18 被阅读0次

纵观css盒子(标准or非标准)常用的布局中,我大致分为2类,一种是居中,一种是等分。居中包含了水平居中,垂直居中,水平垂直居中;等分又包含了等分块布局,等分高布局……

等分布局


先看看等分的布局方案

1. float

<div class="parent">
  <div class="column">
    <p>1</p>
  </div>
  <div class="column">
    <p>2</p>
  </div>
  <div class="column">
    <p>3</p>
  </div>
  <div class="column">
    <p>4</p>
  </div>
</div>
<style>
  .parent {
    margin-left: -20px;
  }
  .column {
    float: left;
    width: 25%;
    padding-left: 20px;
    box-sizing: border-box;
  }
</style>

兼容性较好(IE 8以上)

**2. flex **

<div class="parent">
  <div class="column">
    <p>1</p>
  </div>
  <div class="column">
    <p>2</p>
  </div>
  <div class="column">
    <p>3</p>
  </div>
  <div class="column">
    <p>4</p>
  </div>
</div>


<style>
  .parent {
    display: flex;
  }
  .column {
    flex: 1;
  }
  .column+.column { /* 相邻兄弟选择器 */
    margin-left: 20px;
  }
</style>

兼容性较差(flex属于css3)

兼容性:IE8及以上

3. table

<div class='parent-fix'>
  <div class="parent">
    <div class="column">
      <p>1</p>
    </div>
    <div class="column">
      <p>2</p>
    </div>
    <div class="column">
      <p>3</p>
    </div>
    <div class="column">
      <p>4</p>
    </div>
  </div>
</div>

<style>
  .parent-fix {
    margin-left: -20px;
  }
  .parent {
    display: table;
    width: 100%;
    /*可以布局优先,也可以单元格宽度平分在没有设置的情况下*/
    table-layout: fixed;
  }
  .column {
    display: table-cell;
    padding-left: 20px;
  }
</style>

兼容性:可以兼容 IE 8

等高布局


1. table

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: table;
    width: 100%;
    table-layout: fixed;
  }
  .left {
    display: table-cell;
    width: 100px;
  }
  .right {
    display: table-cell
    /*宽度为剩余宽度*/
  }
</style>

利用 table 的特性——每列等宽

2. flex

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: flex;
  }
  .left {
    width: 100px;
    margin-left: 20px;
  }
  .right {
    flex: 1;
  }
</style>

3. float

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    overflow: hidden;
  }
  .left,
  .right {
    padding-bottom: 9999px;
    margin-bottom: -9999px;
  }
  .left {
    float: left;
    width: 100px;
    margin-right: 20px;
  }
  .right {
    overflow: hidden;
  }
</style>

伪等高(只有背景显示高度相等),左右真实的高度其实不相等。 兼容性较好。

相关文章

  • CSS常用布局之——等分等高解决方案

    纵观css盒子(标准or非标准)常用的布局中,我大致分为2类,一种是居中,一种是等分。居中包含了水平居中,垂直居中...

  • 页面架构

    布局解决方案 水平居中布局 垂直居中布局 水平垂直都居中的布局 多列布局 多列等分布局 多列等高布局 在多列布局的...

  • css之等高布局

    1. 在父元素中不设置高度,使用overflow:hidden; 2. 使用很大的正padding-bottom,...

  • css等高布局

    首先页面template如下: 1.tabletable元素中的table-cell元素默认就是等高的 2.fle...

  • css等高布局

    一、CSS等高布局的7种方式https://www.cnblogs.com/xiaohuochai/p/54571...

  • 多列布局(下)

    这篇来说一下多列布局的后两种-等分布局与等高布局。我们所说通常所说的等分布局如下: 也就是说最左边会多出一个20p...

  • CSS 经典三列布局之圣杯布局

    圣杯布局 圣杯布局是典型的 CSS 布局问题,有着众多的解决方案。 圣杯布局是一种非常经典和常用的布局方式,其所指...

  • 几种常见css布局

    单列布局 第一种 给定宽度,margin:auto即可实现 html css 第二种 html css 等高布局 ...

  • css等分布局

    子元素等分父元素 中间子元素有间隔 两边子元素固定紧靠父元素边界 1, float + padding+backg...

  • CSS常用布局实现

    该系列用于整理记录常用的CSS布局实现。 CSS常用布局实现01-水平居中 CSS常用布局实现02-垂直居中 CS...

网友评论

      本文标题:CSS常用布局之——等分等高解决方案

      本文链接:https://www.haomeiwen.com/subject/ztfvgxtx.html