【问题标题】:CSS columns (NOT grid) not working as intended in ChromeCSS 列(非网格)在 Chrome 中未按预期工作
【发布时间】:2018-02-19 17:59:19
【问题描述】:

我被卡住了,似乎无法弄清楚。

我正在使用一个我称之为“text-col-”的类,后跟一个使用columns: [number]; CSS 的最多6 个数字。它的工作方式完全符合我在 Firefox 中的意图,但在 Chrome 中却没有。昨天我遇到了完全相反的问题,但我想我没有完全解决它(清楚地)。我将在此处编写特定代码(因此您不必查找它),还可以链接到实时版本,您可以在其中直观地看到我的问题(即在 Chrome 中)。

/* SCSS
@for $i from 1 through 6 {
  .text-col-#{$i} {
    width: 100%;
    columns: $i;
    
    @include clearfix;
    @include mq(tablet) {
      @if $i >= 5 {
        clear: both;
        columns: 2;
      }
    }
    @include mq(phone) {
      @if $i >= 3 {
        columns: 1;
      }
    }
  }
}
See the generated css below */

/* CSS */

.text-col-1 {
  width: 100%;
  -webkit-columns: 1;
  columns: 1; }
  .text-col-1::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }

.text-col-2 {
  width: 100%;
  -webkit-columns: 2;
  columns: 2; }
  .text-col-2::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }

.text-col-3 {
  width: 100%;
  -webkit-columns: 3;
  columns: 3; }
  .text-col-3::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }
  @media only screen and (max-width: 480px) {
    .text-col-3 {
      -webkit-columns: 1;
      columns: 1; } }

.text-col-4 {
  width: 100%;
  -webkit-columns: 4;
  columns: 4; }
  .text-col-4::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }
  @media only screen and (max-width: 480px) {
    .text-col-4 {
      -webkit-columns: 1;
      columns: 1; } }

.text-col-5 {
  width: 100%;
  -webkit-columns: 5;
  columns: 5; }
  .text-col-5::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }
  @media only screen and (min-width: 480px) and (max-width: 767px) {
    .text-col-5 {
      clear: both;
      -webkit-columns: 2;
      columns: 2; } }
  @media only screen and (max-width: 480px) {
    .text-col-5 {
      -webkit-columns: 1;
      columns: 1; } }

.text-col-6 {
  width: 100%;
  -webkit-columns: 6;
  columns: 6; }
  .text-col-6::after {
    display: block;
    clear: both;
    line-height: 0;
    height: 0;
    content: " "; }
  @media only screen and (min-width: 480px) and (max-width: 767px) {
    .text-col-6 {
      clear: both;
      -webkit-columns: 2;
      columns: 2; } }
  @media only screen and (max-width: 480px) {
    .text-col-6 {
      -webkit-columns: 1;
      columns: 1; } }
      
 /* CSS to center the text */
 
 [class*="text-col-"] {
  text-align: center;
 }
<!-- Written in pug-html (Jade), but here's the generated HTML -->

<div class="container">
  <p class="text-col-1">Text Columns</p>
  <p class="text-col-1">.text-col-1</p>
  <p class="text-col-2">.text-col-2<br/>.text-col-2</p>
  <p class="text-col-3">.text-col-3<br/>.text-col-3<br/>.text-col-3</p>
  <p class="text-col-4">.text-col-4<br/>.text-col-4<br/>.text-col-4<br/>.text-col-4</p>
  <p class="text-col-5">.text-col-5<br/>.text-col-5<br/>.text-col-5<br/>.text-col-5<br/>.text-col-5</p>
  <p class="text-col-6">.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6</p>
  <!-- Wrapped-->
</div>
<div class="container">
  <div class="text-col-1">Text Columns</div>
  <div class="text-col-1">.text-col-1</div>
  <div class="text-col-2">.text-col-2<br>.text-col-2</div>
  <div class="text-col-3">.text-col-3<br/>.text-col-3<br/>.text-col-3</div>
  <div class="text-col-4">.text-col-4<br/>.text-col-4<br/>.text-col-4<br/>.text-col-4</div>
  <div class="text-col-5">.text-col-5<br/>.text-col-5<br/>.text-col-5<br/>.text-col-5<br/>.text-col-5</div>
  <div class="text-col-6">.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6<br/>.text-col-6</div>
</div>

这里是一个“实时”版本的链接:https://fexell.github.io/Lib/test/classes.html#text-cols

那么,为什么这在 Chrome 中不起作用?

问题图片: Firefox shows this Chrome shows this

【问题讨论】:

  • 具体是什么没有按预期工作?我们可以查看现场演示,这很有帮助,但需要确切了解哪些地方没有按预期工作。
  • 我在问题末尾添加了屏幕截图,显示了 Chrome 中发生的情况以及 Firefox 中的情况。

标签: html css sass pug


【解决方案1】:

它似乎与中断以及&lt;br&gt; 如何在列中被解释有关。 watch this

【讨论】:

  • 谢谢。这让我明白了很多。只是无法理解为什么他们会让它在 -webkit- 中表现得那样。由于您的链接,我“有点”让它工作了,但它只在文本溢出时才有效。我想这总比没有好。非常感谢(将您的答案标记为解决方案)。
  • 耶,我也这么认为。
  • 我实际上不懂语言,但也许可以将列类更改为行类并将其内容居中?比添加具有所需宽度的子元素(%)。因此第 1 行中的子元素具有 100% 的行宽。第 2 行的孩子 50%,依此类推。
  • 我已经尝试过使用“列宽”,但不幸的是它没有将 % 作为值。我还有一个用于百分比容器的类,它可以执行您所暗示的操作,它也可以正常工作。我不妨将它保留为可选类并按预期使用它,这样它在 Chrome 中看起来并不奇怪。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2021-12-28
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
相关资源
最近更新 更多