如何使用 CSS 创建多色边框?

新手上路,请多包涵

如何创建如下图所示的多色边框?

在此处输入图像描述

原文由 Hamed mayahian 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 361
2 个回答

您可以使用 :after:before 伪元素和 css linear-gradient 来完成,如下所示:

 body {
  background: #ccc;
}

.box {
  text-align: center;
  position: relative;
  line-height: 100px;
  background: #fff;
  height: 100px;
  width: 300px;
}

.box:after {
  background: linear-gradient(to right, #bcbcbc 25%,#ffcd02 25%, #ffcd02 50%, #e84f47 50%, #e84f47 75%, #65c1ac 75%);
  position: absolute;
  content: '';
  height: 4px;
  right: 0;
  left: 0;
  top: 0;
}
 <div class="box">Div</div>

原文由 Mohammad Usman 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以在没有 pseudo-elements 的情况下做到这一点,只需 border-image: linear-gradient

 .fancy-border {
  width: 150px;
  height: 150px;
  text-align:center;
  border-top: 5px solid;
  border-image:   linear-gradient(to right, grey 25%, yellow 25%, yellow 50%,red 50%, red 75%, teal 75%) 5;
}
 <div class="fancy-border">
  my content
</div>

原文由 Theodore K. 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏