【问题标题】:CSS - Rectangle div with cut corners and border colorCSS - 带有切角和边框颜色的矩形 div
【发布时间】:2021-02-06 14:06:45
【问题描述】:

我正在尝试实现如图所示的形状:
让 2 个带切角的矩形 div 和 1 个 div 位于另一个 div 后面。

但角落似乎不正确,我找不到显示形状边框的方法。

.wrapper {
  display: flex;
  justify-content: center;
}

.connect {
  width: 254px;
  height: 50px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  position: absolute;
  bottom: 0;
  z-index: 5;
}

.connect::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: -2px;
  border-top: 52px solid white;
  border-left: 42px solid transparent;
}

.connect::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -2px;
  border-top: 52px solid white;
  border-right: 42px solid transparent;
}

.connect-behind {
  width: 300px;
  height: 44px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  position: absolute;
  bottom: 0;
}

.connect-behind::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: -2px;
  border-top: 46px solid white;
  border-left: 26px solid transparent;
}

.connect-behind::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -2px;
  border-top: 46px solid white;
  border-right: 26px solid transparent;
}
  <div class="wrapper">
    <div class="connect"></div>
    <div class="connect-behind"></div>
  </div>

我参考了其他线程以使用 behindafter 作为解决方案,但它似乎不适用于我的问题。请帮忙,谢谢。

【问题讨论】:

  • 为什么不使用 SVG 之类的东西?

标签: html css css-shapes


【解决方案1】:

你可以使用透视和变换:

可能的例子(对于信息:用网格代替绝对)

.wrapper {
  display: grid;
  justify-content: center;
  align-items: end;
  height: 300px;
  perspective: 50px;
}

.connect,
.connect-behind {
  transform: rotatex(50deg);
  background: red;
  margin: 0 auto;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  grid-row: 1;
  grid-column: 1;
  transform-origin: bottom center;
}

.connect-behind {
  width: 300px;
  height: 44px;
}

.connect {
  width: 254px;
  height: 50px;
  ;
}
<div class="wrapper">
  <div class="connect"></div>
  <div class="connect-behind"></div>
</div>

要在形状周围画一个borderdrop-shadow 可能很有用

.wrapper {
  display: grid;
  justify-content: center;
  align-items: end;
  height: 300px;
  perspective: 50px;
   filter:
   drop-shadow( 1px  0px 0 )
   drop-shadow(-1px  0px 0 )
   drop-shadow( 0px  1px 0 )
   drop-shadow( 0px -1px 0 );
}

.connect,
.connect-behind {
  transform: rotatex(50deg);
  background: red;
  margin: 0 auto;
  background:white;
  grid-row: 1;
  grid-column: 1;
  transform-origin: bottom center; 
  background:#ffa500;
}

.connect-behind {
  width: 254px;
  height: 50px; 
  border-left:solid 2px;
  border-right:solid 2px;
}

.connect {
  background:#ed1c24;
  width: 300px;
  height: 44px;
  ;
}
<div class="wrapper">
  <div class="connect"></div>
  <div class="connect-behind"></div>
</div>

【讨论】:

  • 如何为形状添加边框颜色
  • @hatched 你可以设置元素本身的边框。还是仅在您需要的形状周围的边框?
  • 是形状本身。感谢您的解决方案,效果很好。
【解决方案2】:

你可以使用 clip-path 来处理这样的事情。在(我认为)大多数浏览器中运行良好。但是,有些(例如 ie11 和旧版浏览器)无法正确呈现它,因此您可能需要针对这些情况进行回退。

body {
overflow: hidden;
}

.wrapper {
  display: flex;
  justify-content: center;
}

.connect {
  width: 254px;
  height: 80px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid black;
  position: absolute;
  bottom: 0;
  z-index: 5;
  clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
}

.connect-border-left {
    height: 80px;
    width: 2px;
    background: black;
    left: calc(50% - 131px);
    position: absolute;
    bottom: -12px;
    transform: rotate(34deg) translateX(-50%);
    display: inline-block;
}

.connect-border-right {
    height: 80px;
    width: 2px;
    background: black;
    right: calc(50% - 131px);
    position: absolute;
    bottom: -12px;
    transform: rotate(-34deg) translateX(-50%);
    display: inline-block;
}


.connect-behind {
  width: 300px;
  height: 60px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid black;
  position: absolute;
  bottom: 0;
  clip-path: polygon(14% 0%, 86% 0%, 100% 100%, 0% 100%);
}

.connect-behind-border-right {
    height: 100px;
    width: 2px;
    background: black;
    right: calc(50% - 103px);
    position: absolute;
    bottom: -11px;
    transform: rotate(-32deg) translateX(-50%);
    display: inline-block;
}

.connect-behind-border-left {
    height: 100px;
    width: 2px;
    background: black;
    left: calc(50% - 103px);
    position: absolute;
    bottom: -11px;
    transform: rotate(32deg) translateX(-50%);
    display: inline-block;
}
<div class="wrapper">
    <div class="connect"></div>
    <div class="connect-border-left"></div>
    <div class="connect-border-right"></div>
    <div class="connect-behind"></div>
    <div class="connect-behind-border-left"></div>
    <div class="connect-behind-border-right"></div>
  </div>

【讨论】:

  • 有什么方法可以将黑色边框也添加到侧面?
  • 是的,您可以使用其他 div 并将它们正确定位在项目的任一侧。像那些多边形这样的裁剪项目不允许在边上有边框,因为它们的原始形状是正方形或矩形,所以边框不能正确显示。
  • 非常感谢您的解决方案。工作得很好。
【解决方案3】:

一个带有倾斜变换、剪辑路径和多背景的想法:

.box {
  --b:3px;   /* border width */
  --t:20px; /* top part width */
  --s:30px; /* side part width */
  margin:10px;
  display:inline-block;
  width:250px;
  height:150px;
  position:relative;
}
.box::before,
.box::after {
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  width:50%;
  border-style:solid;
  border-width:var(--b) 0 0 var(--b);
  background:
    linear-gradient(black 0 0) 0 var(--t)/100% var(--b),
    linear-gradient(black 0 0) var(--s) 0/var(--b) 100%,
    linear-gradient(red 0 0)  left/var(--s) 100%, 
    orange;
  background-repeat:no-repeat;
  transform-origin:bottom right;
  transform:skew(-20deg);
  clip-path:polygon(0 calc(var(--t) + var(--b)), calc(var(--s) + var(--b)) calc(var(--t) + var(--b)),calc(var(--s) + var(--b)) 0,60% 0,100% 100%,0 100%);
}
.box::after {
  transform:scale(-1,1) skew(-20deg);
 }
<div class="box"></div>
<div class="box" style="--b:2px;--t:30px;--s:15px;"></div>

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 2022-01-24
    • 1970-01-01
    • 2015-07-29
    • 2012-03-16
    • 2019-11-17
    • 2012-10-26
    • 2017-05-27
    • 1970-01-01
    相关资源
    最近更新 更多