【问题标题】:Is there a shorthand property for positioning? [duplicate]是否有定位的简写属性? [复制]
【发布时间】:2018-05-02 01:53:14
【问题描述】:

类似于你的写作方式:

margin: 12px 8px 10px 8px;
//or
padding: 12px 8px 10px 8px;

有没有办法简写位置属性?

//from    
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;

//to
position: absolute 0 0 0 0;

还是什么?

MDN 似乎没有任何东西,但它可以被称为其他东西或完全不同的方法。

【问题讨论】:

  • 不...抱歉...可能在未来的规范中。也许你可以用 scss 做一个 mixin。

标签: css css-position shorthand


【解决方案1】:

不,您不能将 position 简写为 top 等其他属性,因为它们是完全独立的属性,不像 background 具有图像和颜色例子。如果您想节省编写 CSS 的时间,可以查看 CSS 预处理器(如 Sass 和 LESS)中的 mixins。

Sass 中的 mixin 示例:

@mixin border-radius($radius) {
  -webkit-border-radius: $radius;
     -moz-border-radius: $radius;
      -ms-border-radius: $radius;
          border-radius: $radius;
}

.box { @include border-radius(10px); }

以上摘自SassMixin Reference

显然,如果您不想使用预处理器,则只需在每次要使用这些属性时手动将其写出来。

【讨论】:

    【解决方案2】:

    不,没有。

    这是 CSS 定位模块级别 3 的当前定义:https://www.w3.org/TR/css-position-3/ - 该文档没有为 positionleftrighttopdown 定义任何简写形式。

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 1970-01-01
      • 2020-12-11
      • 2021-10-31
      • 2018-06-25
      • 2020-03-19
      • 2011-12-19
      • 2017-03-25
      • 2017-06-27
      相关资源
      最近更新 更多