【问题标题】:Susy: Omega and Responsive GridsSusy:欧米茄和响应式网格
【发布时间】:2013-03-29 23:47:47
【问题描述】:

使用 Susy 时,您在行的最后一项上放置一个“omega”标志以删除其 margin-right。例如,假设我们有一堆项目需要在 12 列的网格上布局:

<div class="item">...</div>
<div class="item">...</div>
<div class="item">I'm the last of the row</div>
<div class="item">...</div>

还有 SCSS:

.item{
  @include span-columns(4);
  @include nth-omega(3n);
}

但我们的网格是响应式的,较小的显示器使用 8 列网格。问题是omega 现在需要出现在2n 上,而不是3n

<div class="item">...</div>
<div class="item">I'm the last of the row</div>
<div class="item">...</div>
<div class="item">...</div>

所以我的问题是:对于 Susy,您是否需要为每个断点重新定义列,或者有什么方法可以一劳永逸地定义列宽,让 omega 自然地落在正确的位置?

如果没有,还有其他网格系统提供吗?

【问题讨论】:

  • 请注意,这个问题与 Drupal Omega 主题无关,如果标题误导,请见谅。

标签: css sass compass-sass susy-compass


【解决方案1】:

用 Susy 解决您的问题

Susy 允许覆盖列数。许多 Susy mixin 都允许这样做——每个接受 $context 参数的 mixin。

但覆盖上下文的最佳方法是使用 at-breakpoint 混合:

// Defining the breakpoints
$mobile-to-medium: 400px;
$medium-to-large:  800px;

// Defining columns
$columns-small:    1;
$columns-medium:   8;
$columns-large:    12;

// Starting with a one-column mobile grid
$total-columns: $columns-small;

// Global styles go here

// Mobile-only styles
@include at-breakpoint($total-columns $mobile-to-medium) {
  // ...
}

// Medium-only styles go here
@include at-breakpoint($mobile-to-medium $columns-medium $medium-to-large) {

  .item{
    @include span-columns(3);
    @include nth-omega(2n); } }

// Large-only styles go here
@include at-breakpoint($medium-to-large $columns-large) {

  .item{
    @include span-columns(4);
    @include nth-omega(3n); } }

Omega 假设分层响应:移动样式适用于所有宽度; medium 样式适用于中等和大宽度,大样式适用于大宽度。

上面的方法不是分层的:移动样式只应用于移动宽度等。这样你就不用担心欧米茄应用到不应该去的地方。

要使用 Omega 分层方法,只需删除 at-breakpoint 调用中的第三个元素(最大宽度)。但是你必须申请@include remove-nth-omega()

// Defining the breakpoints
$mobile-to-medium: 400px;
$medium-to-large:  800px;

// Defining columns
$columns-small:    1;
$columns-medium:   8;
$columns-large:    12;

// Starting with a one-column mobile grid
$total-columns: $columns-small;

// Global styles go here

// Medium and large styles go here
@include at-breakpoint($mobile-to-medium $columns-medium) {

  .item{
    @include span-columns(3);
    @include nth-omega(2n); } }

// Large-only styles go here
@include at-breakpoint($medium-to-large $columns-large) {

  .item{
    @include span-columns(4);
    @include remove-nth-omega(2n);
    @include nth-omega(3n); } }

无 omega 方法概述

有些 SASS 网格系统不使用“omega”参数(不要与 Drupal 的 Omega 主题混淆),该参数必须应用于每行中的最后一项。相反,除了列宽之外,您还需要提供每个元素的位置(项目从哪一列开始)。

为了实现这一点,使用了另一个 CSS 定位 approach,称为“隔离”。第一个使用这种方法的框架是Zen Grids

Susy 也通过其isolateisolate-grid mixins 支持此方法。

如果不提及最新和最先进的 SASS 网格框架 Singularity,本概述将是不完整的。它支持这两种定位方法,并且可以扩展以在未来支持更多(例如 flexbox,它最近已成为 Compass 的 added)。

【讨论】:

  • 这里的第一个代码示例演示了上面@Kaishin 建议的内容,以及更具体的媒体查询。第二个示例演示了remove-nth-omega() 选项。隔离也可以,但有类似的警告——你仍然需要适应不同的网格。
【解决方案2】:

在您的情况下,您将不得不重新定义新断点中的总列数(上下文)。至于nth-omega,您可以在显式调用第二个调用之前使用@include remove-nth-omega(3n) 否定第一个调用,但我认为CSS 代码有异味(必须中和属性),因此我建议使用媒体查询拆分来避免这种情况。

另外,我不知道有任何框架可以自动为您做到这一点。

【讨论】:

  • remove-nth-omega(3n) 是适合这项工作的工具,如果您不介意一些覆盖代码。否则@Kaishin 是对的,您可以使用更具体的(最小 + 最大)媒体查询来消除对覆盖的任何需求。
猜你喜欢
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
相关资源
最近更新 更多