【问题标题】:Changing column widths更改列宽
【发布时间】:2015-08-14 22:59:49
【问题描述】:

我目前创建了一个并排的两列 div,每个 div 占 50%。我正在试图弄清楚如何使左 div 70% 和右 div 30%。

此外,除了 2 列 div。如果我想创建一个 4 col div,每个 25% 和一个 5 column div,每个 20%。我该怎么做?

.container-2col {
  clear: left;
  float: left;
  width: 100%;
  overflow: hidden;
  background: none;
}

.container-1col {
  float: left;
  width: 100%;
  position: relative;
  right: 50%;
  background: none;
}

.col1 {
  float: left;
  width: 46%;
  position: relative;
  left: 52%;
  overflow: hidden;
}

.col2 {
  float: left;
  width: 46%;
  position: relative;
  left: 56%;
  overflow: hidden;
}
<div class="container-2col">
  <div class="container-1col">
    <div class="col1">
      <p>Delicious and crunchy, apple fruit is one of the most popular and favorite fruits among the health conscious, fitness lovers who firmly believe in the concept of &ldquo;health is wealth.&rdquo; This wonderful fruit is packed with rich phyto-nutrients
        that, in the true sense, indispensable for optimal health. Certain antioxidants in apple have several health promoting and disease prevention properties, and thereby, truly justifying the adage, &ldquo;an apple a day keeps the doctor away.&rdquo;</p>
    </div>
    <div class="col2">
      <p>Vegetables, like fruits, are low in calories and fats but contain good amounts of vitamins and minerals. All the Green-Yellow-Orange vegetables are rich sources of calcium, magnesium, potassium, iron, beta-carotene, vitamin B-complex, vitamin-C,
        vitamin-A, and vitamin K.</p>
    </div>
  </div>
</div>

这是我的 jsfiddle:http://jsfiddle.net/huskydawgs/zhckr47h/3/

【问题讨论】:

  • 不确定我是否遵循,但是将 .col 宽度更改为 64% 并将 .col2 宽度更改为 28% 怎么样?喜欢this?由于您的左右当前各占 46%,因此 64% 和 28% 的总数相同 (92%),但余额为 70:30 而不是 50:50。
  • "如果我想创建一个 4 col div,每个 25% 和一个 5 column div,每个 20%。我该怎么做?" - 这是不可能的,它是宽度的 120%:)
  • 70% 和 30% 在 100% 标签内

标签: html css multiple-columns


【解决方案1】:

有多种方法可以使用 CSS Grid 创建多列布局。

这是一种方法,具有 grid-template-columns 属性和灵活的 (fr) 长度。

article {
  display: grid;
  grid-template-rows: 75px;
  grid-gap: 10px;
}

article:nth-child(1) {
  grid-template-columns: 7fr 3fr;
}

article:nth-child(2) {
  grid-template-columns: 1fr 1fr 1fr 1fr;
}

article:nth-child(3) {
  grid-template-columns: repeat(5, 1fr);
}

/* demo styles only */
article                 { margin-bottom: 2em; }
section:nth-child(odd)  { background-color: lightgreen; }
section:nth-child(even) { background-color: orange; }
<article>
  <section></section>
  <section></section>
</article>

<article>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</article>

<article>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</article>

【讨论】:

    【解决方案2】:

    使用display:inline-block 而不是float。这消除了您对其他divs 的需要。使用margin-right:-4px 删除元素之间的空格。如果您想挖掘它们,还有其他解决此问题的方法。 Float 有它的位置,但我发现它带来的问题比它解决的要多。

     .col1 {
        display:inline-block;  
        width: 30%;
        padding:0;
        margin:0;
        margin-right:-4px;
        vertical-align:top;
        background-color:yellow;
        }
    

    只需将width 相应地更改为col2

    参见 JSfiddle:https://jsfiddle.net/zhckr47h/12/

    【讨论】:

    • 这可以无休止地应用于任意数量的具有任何所需宽度的 div
    • 我使用浮动来解决这些问题的一次是结合浮动上的设置宽度和非浮动元素上的overflow: hidden; width: auto;。这会导致一个意想不到的公式,其中浏览器实际上为您提供了一个完美的、分离的、两列布局(尽管高度不同步),而无需计算百分比数学。
    【解决方案3】:

    无需过多调整代码,这很容易做到。但首先摆脱浮动,而是显示 inline-block 和 vertical-align:top。

    这是fiddle

    .container-2col {
      clear: left;
      float: left;
      width: 100%;
      overflow: hidden;
      background: none;
      margin: 0!important;
      padding: 0!important;
    }
    .col1,
    .col2 {
      display: inline-block;
      vertical-align: top;
      position: relative;
      background: none;
      overflow: hidden;
    }
    .col1 {
      width: 69.5%;
    }
    .col2 {
      width: 30%;
    }
    <div class="container-2col">
      <div class="container-1col">
        <div class="col1">
          <p>Delicious and crunchy, apple fruit is one of the most popular and favorite fruits among the health conscious, fitness lovers who firmly believe in the concept of &ldquo;health is wealth.&rdquo; This wonderful fruit is packed with rich phyto-nutrients
            that, in the true sense, indispensable for optimal health. Certain antioxidants in apple have several health promoting and disease prevention properties, and thereby, truly justifying the adage, &ldquo;an apple a day keeps the doctor away.&rdquo;</p>
        </div>
        <div class="col2">
          <p>Vegetables, like fruits, are low in calories and fats but contain good amounts of vitamins and minerals. All the Green-Yellow-Orange vegetables are rich sources of calcium, magnesium, potassium, iron, beta-carotene, vitamin B-complex, vitamin-C,
            vitamin-A, and vitamin K.</p>
        </div>
      </div>
    </div>

    【讨论】:

    • 我已将宽度设置为 69.5%,因为除非您明确将其删除,否则正文始终有填充
    【解决方案4】:

    我已将leftright 属性更改为margins,以便于计算。因此,70-30 布局的新宽度必须为:70% - 4%(边距)= 66% 和 30% - 4%(边距)= 26%。

    .container-2col {
      clear: left;
      float: left;
      width: 100%;
      overflow: hidden;
      background: none;
    }
    .container-1col {
      float: left;
      width: 100%;
      position: relative;
      background: none;
    }
    .col1 {
      float: left;
      width: 66%;
      margin: 0 2%;
      position: relative;
      overflow: hidden;
    }
    .col2 {
      float: left;
      width: 26%;
      margin: 0 2%;
      position: relative;
      overflow: hidden;
    }
    <div class="container-2col">
      <div class="container-1col">
        <div class="col1">
          <p>Delicious and crunchy, apple fruit is one of the most popular and favorite fruits among the health conscious, fitness lovers who firmly believe in the concept of &ldquo;health is wealth.&rdquo; This wonderful fruit is packed with rich phyto-nutrients
            that, in the true sense, indispensable for optimal health. Certain antioxidants in apple have several health promoting and disease prevention properties, and thereby, truly justifying the adage, &ldquo;an apple a day keeps the doctor away.&rdquo;</p>
        </div>
        <div class="col2">
          <p>Vegetables, like fruits, are low in calories and fats but contain good amounts of vitamins and minerals. All the Green-Yellow-Orange vegetables are rich sources of calcium, magnesium, potassium, iron, beta-carotene, vitamin B-complex, vitamin-C,
            vitamin-A, and vitamin K.</p>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 2017-01-03
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 2014-06-01
      相关资源
      最近更新 更多