css - 边框属性的简写?

标签 css

我们可以缩短吗:

border-top:1px solid black;
border-left:2px solid red;
border-bottom:3px solid green;
border-right:4px solid blue;

类似于:

border:1px solid black 2px solid red 3px solid green 4px solid blue;

注意:我已经这样知道了:

border-width:1px 2px 3px 4px;
border-style:solid;
border-color:black red green blue;

最佳答案

不,你不能让它比你提供的例子更短。

From the docs

Unlike the shorthand ‘margin’ and ‘padding’ properties, the ‘border’ property cannot set different values on the four borders. To do so, one or more of the other border properties must be used.

感谢@Rocket


如果您正在使用预处理器(如 SCSS),您可以尝试使用混入,但我几乎不相信这就是您想要的:

@mixin border_shorthand(
$top_width, $top_color, $top_style,
$left_width, $left_color, $left_style,
$bottom_width, $bottom_color, $bottom_style,
$right_width, $right_color, $right_style) {
  border-top: $top_width $top_color $top_style;
  border-left: $left_width $left_color $left_style;
  border-bottom: $bottom_width $bottom_color $bottom_style;
  border-right: $right_width $right_color $right_style;
}

.element {
  @include border_shorthand(1px, black, solid, 2px, red, solid, 3px, green, solid, 4px, blue, solid);
}

哪些输出:

.element {
  border-top: 1px black solid;
  border-left: 2px red solid;
  border-bottom: 3px green solid;
  border-right: 4px blue solid; }

关于css - 边框属性的简写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11001303/

相关文章:

html - 悬停时可点击图像上的文字

css - 动态重新缩放的 div 网格

html - 自定义悬停效果位置问题

javascript - 如果单击按钮后输入字段为空,则不应提交

javascript - 像 Facebook 一样突出文本区域内的文本

html - 图像不会随浏览器大小调整大小

javascript - 如何更改javascript变量的文本颜色

html - 我的内联图像下方的额外间距

javascript - 如何在 Firebase 响应后同时显示警报和刷新页面?

javascript - 可以在绝对堆叠的元素上触发点击事件吗?