【问题标题】:Creating a fluid grid - percentages seem to mess up创建一个流体网格 - 百分比似乎搞砸了
【发布时间】:2012-03-22 12:29:53
【问题描述】:

我正在查看 Skeleton 框架 (getskeleton.com),但我想要一个流体网格系统。我也对这些网格的内部运作感兴趣,所以我决定自己动手。

但是,我遇到了two.columns 百分比的问题。所有其他列宽都可以正常工作。首先,这是发生了什么:

第二排明显有问题,我实在想不通。

我的这个布局代码如下:

   $max-width: 600px;

   .container {
     @include container($max-width);

     .column, .columns {
       @include columns($max-width);
     }

     .one.column, .one.columns  { width: perc(30px, $max-width); }
     .two.columns               { width: perc(80px, $max-width); }
     .three.columns             { width: perc(130px, $max-width); }
     .four.columns              { width: perc(180px, $max-width); }
     .five.columns              { width: perc(230px, $max-width); }
     .six.columns               { width: perc(280px, $max-width); }
     .seven.columns             { width: perc(330px, $max-width); }
     .eight.columns             { width: perc(380px, $max-width); }
     .nine.columns              { width: perc(430px, $max-width); }
     .ten.columns               { width: perc(480px, $max-width); }
     .eleven.columns            { width: perc(530px, $max-width); }
     .twelve.columns            { width: perc(580px, $max-width); }

以下是我使用的 mixins/函数,不确定是否重要:

@function perc($width, $container-width) {
  @return percentage($width / $container-width);
}

@mixin container($width)  {
  position: relative; 
  width: $width; 
  margin: 0 auto; 
  padding: 0;
}

@mixin columns($max-width)  {
  float: left;
  display: inline;
  margin: 0 10px;
}

HTML文件中的代码就是:

<% 6.times do %>
  <div class="two columns box"></div>
<% end %>

我想如果百分比的计算有问题,所有行都会以某种方式搞砸。不过,这只是第二个。。如果有人看到这里发生了什么,请赐教?

我把处理后的 CSS 放到 pastebin 上:http://pastebin.com/bxq1a5Ge

提前致谢。

【问题讨论】:

    标签: css grid sass fluid


    【解决方案1】:

    我认为您还需要将利润率转换为百分比。每当一个 div 跨越超过 1 列时,20px 的装订线宽度就会转换为一个百分比,作为块宽度的一部分。这最终会在某个时候出错。

    另一种选择是四舍五入问题,因为浏览器无法将百分比值整齐地转换为像素值。

    自从 jsfiddle 支持 SCSS 以来,我已经设置了一个基本的 jsfiddle of the issue。该示例显示选项 2 是一个不错的候选者,但我建议将所有宽度更改为百分比。

    【讨论】:

    • 我刚刚在 Firefox 中进行了测试,看起来还不错。它不在 Chrome 中。您设置的 JSFiddle 在 Chrome 中看起来也很糟糕,就像在我的屏幕截图中一样。这是否意味着我应该放弃流动性?现在有点困惑:(
    【解决方案2】:

    显然

    perc(80px, 600px) 给出13.333%

    当我使用width: 13.3333333% 时,问题得到了解决。

    【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多