You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为类椭圆矩形实现双色偏移重叠边框?

需求:为椭圆矩形混合形状实现双色偏移边框

我需要创建一个与背景重叠时呈现两种不同颜色的偏移边框,之前在矩形或圆形上实现过该效果,但本次需求是顶部为椭圆、底部为矩形的混合形状。

第一次尝试代码

CSS

.square {
  height: 300px;
  width: 200px;
  background-color: black;
  border-radius: 500px 500px 0px 0px;
  border-color: black white white black;
  border: 0px solid transparent;
  content:"";
}

.square::after {
  content: " ";
  position: absolute;
  transform: translate3d(10px, 10px, 0);
  height: 300px;
  width: 200px;
  border: 3px solid;
  /* border-color: white black black white;*/
  box-shadow: white 3px 3px 0px inset;
  box-sizing: border-box;
  border-radius: 500px 500px 0px 0px;
}

HTML

<div class="square"></div>

遇到的问题

尝试过渐变和box-shadow方案均未成功,查阅W3C规则后仍需优化,左下部分的边框效果不符合预期。我需要实现的是:左下边框和右上边框呈现不同颜色,且边框宽度保持一致(效果如参考图片所示)。

其他尝试代码

CSS

*,
::before {
  box-sizing: inherit;
  margin: 0;
}

html {
  font: 100 1.25em/1.25 handlee, z003, segoe script, comic sans ms, cursive;
}
@media (max-width: 360px) {
  html {
    font-size: 1em;
  }
}
@media (max-width: 240px) {
  html {
    font-size: 0.75em;
  }
}

body {
  box-sizing: border-box;
  display: grid;
  grid-gap: 1.25em;
  grid-template-columns: repeat(auto-fit, minmax(14em, 1fr));
  overflow-x: hidden;
  padding: 0.5em;
  min-height: 100vh;
}
@media (min-width: 37.8125em) {
  body {
  }
}

article {
  --c: #ffa940;
  --s0: calc(1.25em + 6px);
  --s1: calc(1.25em + 6px + 0.5em);
  place-self: center;
  position: relative;
  width: 12em;
  height: 14em;
  border: solid 4px var(--s0);
  border-width: 0 var(--s0) var(--s0) 0;
  padding: var(--s1) 0.5em 0.5em var(--s1);
  background: var(--c) padding-box;   
}

article {
  border-top-right-radius: 50%;
  border-top-left-radius: 50%;
}
article::before {
  position: absolute;
  top: -40px;
  left: -60px;
  border: inherit;
  border-width: 1.25em 0 0 1.25em;
  width: 12em;
  height: 14em;
  box-shadow: inset 0 0 0 6px var(--c);
  mix-blend-mode: difference;
  pointer-events: none;
  content: "";
  border-top-right-radius: 50%;
  border-top-left-radius: 50%;
}

h3 {
  font-size: 1.5em;
  font-weight: 900;
  line-height: 1.75;
}

.container {
  width: 200px;
  height: 300px;
  background: #ffa940;
  border-top-left-radius: 400px;
  border-top-right-radius: 400px;
  transform: translateY(10px);
}
.container:after {
  width:12px;
  height: 3px;
  background-color: black;
  position:absolute;
  content:"";
  top:284px;
  left:-13px
}

.container:before {
  content: "";
  border: 4px solid;
  border-bottom-color: white;
  border-left-color: black;
  border-top-left-color: black;
  border-right-color: white;
  /* border-top-color: white;*/
  border-top-left-radius: 400px;
  border-top-right-radius: 400px;
  width: 200px;
  height: 300px;
  position: absolute;
  left: -13px;
  top: -13px;
}
.white-grad {
  --b:5px;  /* border width*/

  color: #313149;
  display: inline-block;
  margin: 10px;
  width: 200px;
  height:250px;
 /* aspect-ratio: 1;*/
  position: relative;
  z-index: 0;
  left:500px;
  background: red;
   border-radius: var(--r,50%);
}

.white-grad:before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  background: var(--c,linear-gradient(to right, #9c20aa, #fb3570));
  padding: var(--b);
  border-radius: var(--r,50%);
  -webkit-mask: 
     linear-gradient(#fff 0 0) content-box, 
     linear-gradient(#fff 0 0);
         mask: 
     linear-gradient(#fff 0 0) content-box, 
     linear-gradient(#fff 0 0);
 -webkit-mask-composite: xor;
 mask-composite: exclude;
  left: -20px;
  margin-top: -20px;
  margin-bottom: 20px;
  margin-right: 20px;
}

HTML

<article></article>

<div class="container"></div>

<div class="white-grad"  style="--b:4px;--r:5em 5em 0 0;--c:linear-gradient(-56.03deg,white 49.5%,black 0)"></div>

内容的提问来源于stack exchange,提问作者Urano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:15:43