如何在三角形内部制作边框?

问题描述 投票:3回答:2

我正在用箭头和边框看起来像Desired Result

我试过这个。

* {
    box-sizing: border-box;
}
.block-arr {
    background: purple;
    margin: 20px;
    margin-right: 100px;
    position: relative;
}

.block-arr .inner {
    min-height: 100px;
    display: flex;
    padding: 20px;
    align-items: center;
    position: relative;
}

.block-arr .inner:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}

.block-arr:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}
<div class="block-arr">
    <div class="inner">
        <strong>Main Heading</strong>
        <span>Sub Heading</span>
    </div>
</div>

我怎么能像图像一样制作块?我们可以使这个箭头高度响应吗?

html css css3
2个回答
4
投票

我会考虑倾斜变换,插入框阴影和一些线性渐变的混合:

* {
  box-sizing: border-box;
}

.block-arr {
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  background: linear-gradient(#fff, #fff)2px 0/2px 100% no-repeat, purple;
  border-left: 2px solid purple;
  z-index: 0;
}

.block-arr:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 50%;
  left: 0;
  right: 0;
  background: purple;
  border: 5px solid purple;
  border-bottom: none;
  border-left: none;
  box-shadow: -2px 2px 0px #fff inset;
  transform: skew(25deg);
  transform-origin: top left;
  z-index: -1;
}

.block-arr:after {
  content: "";
  position: absolute;
  top: 50%;
  bottom: 0;
  left: 0;
  right: 0;
  background: purple;
  border: 5px solid purple;
  border-top: none;
  border-left: none;
  box-shadow: -2px -2px 0px #fff inset;
  transform: skew(-25deg);
  transform-origin: bottom left;
  z-index: -1;
}
<div class="block-arr">
  <strong>Main Heading</strong>
  <span>Sub Heading</span>
</div>
<div class="block-arr">
  <strong>Main Heading</strong><br/>
  <span>Sub Heading</span>
</div>
<div class="block-arr">
</div>

这是一个更加压缩的版本,带有一些CSS变量,可以轻松处理颜色。您也可以这样做来处理其他变量:

* {
  box-sizing: border-box;
}

.block-arr {
  --c1:purple;
  --c2:#fff;
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  background: linear-gradient(var(--c2), var(--c2))2px 0/2px 100% no-repeat, var(--c1);
  border-left: 2px solid var(--c1);
  z-index: 0;
}

.block-arr:before,
.block-arr:after {
  left: 0;
  right: 0;
  content: "";
  position: absolute;
  background: var(--c1);
  border: 5px solid var(--c1);
  border-left: none;
  z-index: -1;
}

.block-arr:before {
  top: 0;
  bottom: 50%;
  border-bottom: none;
  box-shadow: -2px 2px 0px var(--c2) inset;
  transform: skew(25deg);
  transform-origin: top left;
}

.block-arr:after {
  top: 50%;
  bottom: 0;
  border-top: none;
  box-shadow: -2px -2px 0px var(--c2) inset;
  transform: skew(-25deg);
  transform-origin: bottom left;
}
<div class="block-arr">
</div>

<div class="block-arr" style="--c1:red;--c2:yellow">
  <strong>Main Heading</strong>
  <span>Sub Heading</span>
  <p>And yes it is reponsive and grow when height grow</p>
</div>

奖金

另一种只有线性渐变的奇特而复杂的方式:

* {
  box-sizing: border-box;
}

.block-arr {
  --c1:purple;
  --c2:#fff;
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  border:1px solid;
  background:
  linear-gradient(to top left,transparent calc(50% + 4px),var(--c2) calc(50% + 4px),var(--c2) calc(50% + 6px),transparent 0) 100% 100%/50px 50% no-repeat,
  linear-gradient(to bottom left,transparent calc(50% + 4px),var(--c2) calc(50% + 4px),var(--c2) calc(50% + 6px),transparent 0) 100% 0/50px 50% no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px calc(100% -  4px)/calc(100% - 58px) 2px no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px 4px/calc(100% - 58px) 2px no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px 4px/2px calc(100% - 8px) no-repeat,
  linear-gradient(to top left ,transparent 50%,var(--c1) 50%) 100% 100%/50px 50% no-repeat,
  linear-gradient(to bottom left,transparent 50%,var(--c1) 50%) 100% 0/50px 50% no-repeat,
  linear-gradient(var(--c1),var(--c1)) 0 0/calc(100% - 50px) 100%  no-repeat;
}
<div class="block-arr">
</div>

1
投票

使用:after:before伪元素,我做了这个设计。

希望它符合您的要求。

谢谢

CSS和HTML:

* {
    box-sizing: border-box;
}
p { margin:0; }
.block-arr {
    background: purple;
    margin: 20px;
    margin-right: 100px;
    position: relative;
}

.block-arr .inner {
    min-height: 100px;
    /*display: flex;*/
    padding: 20px;
    align-items: center;
    position: relative;
}

.block-arr .inner:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}

.block-arr:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}
.bordered { position:relative; border:1px solid #fff; border-right:none; display: flex; align-items: center; padding:20px; }
.bordered:before, .bordered:after {
    content: "";
    position: absolute;
    height: 72%;
    width: 1px;
    background: #fff;
    top: 0;
    right: 0;
    z-index: 4;
}
.bordered:before {
    transform: rotate(45deg);
    top: auto;
    right: -3.3%;
    bottom: -11%;
}
.bordered:after {
    transform: rotate(-45deg);
    top: -12%;
    right: -3.3%;
}
<div class="block-arr">
    <div class="inner"><div class="bordered">
        <p><strong>Main Heading</strong>
        <span>Sub Heading</span></p>
        
    </div></div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.