首页 > 解决方案 > 如何使用 CSS 创建类似紧身胸衣的效果?

问题描述

我想用CSS创建如下效果,最初,我想合并两个带圆角的矩形,但我不能使用这种方式,因为它是一个锐角,而不是圆角,看看圆中的区域,这个是最难的部分。

在此处输入图像描述

所以我这样做:
1. 创建一个圆角矩形作为轮廓。边框颜色为灰色。
2. 在矩形左侧的中心创建一个灰色三角形。
3. 创建另一个白色三角形覆盖步骤 2 中的灰色三角形,稍微偏移,使其显示为矩形的边框。

这是效果:

在此处输入图像描述

我的问题是:
1.如何添加正确的部分?看来我只能在 CSS 选择器中同时绘制一个形状:before 或 :after。
2.如果我想添加边框阴影效果怎么办?如果我添加矩形的阴影,会出现白色三角形,这很难看。
3. 或者有什么其他的方法可以产生这种效果?

这是代码:

.triangle_left {
  width: 600px;
  height: 600px;
  position: relative;
  margin: 20px auto;
  border: 2px solid #cccccc;
  /* box-shadow: 0px 0px 8px 1px darkgrey; */
  border-radius: 20px;
}

.triangle_left:before {
  content: '';
  width: 0;
  height: 0;
  border: 20px solid transparent;
  border-left-color: #cccccc;
  position: absolute;
  left: 0;
  top: 50%;
  margin-top: -20px;
}

.triangle_left:after {
  content: '';
  width: 0;
  height: 0;
  border: 18px solid transparent;
  border-left-color: white;
  position: absolute;
  left: -2px;
  top: 50%;
  margin-top: -18px;
}
<div class="triangle_left">

</div>

在 JSFiddle 中尝试一下

标签: htmlcsscss-shapes

解决方案


一个想法是使用渐变构建形状并依靠drop-shadow过滤器

.box {
  margin-top:50px;
  border-radius:10px;
  width:200px;
  height:200px;
  background:
  /*the middle line */
    repeating-linear-gradient(to right,gold 0px,gold 5px,transparent 5px,transparent 10px) center/calc(100% - 40px) 2px,
    /*The background*/
    linear-gradient(to bottom right,#fff 49%,transparent 50%) 100% calc(50% - 10px) / 20px 20px,
    linear-gradient(to top    right,#fff 49%,transparent 50%) 100% calc(50% + 10px) / 20px 20px,
    linear-gradient(to bottom left ,#fff 49%,transparent 50%) 0    calc(50% - 10px) / 20px 20px,
    linear-gradient(to top    left ,#fff 49%,transparent 50%) 0    calc(50% + 10px) / 20px 20px,
    
    linear-gradient(#fff,#fff) top    right / 20px calc(50% - 20px),
    linear-gradient(#fff,#fff) bottom right / 20px calc(50% - 20px),
    
    linear-gradient(#fff,#fff) top    left / 20px calc(50% - 20px),
    linear-gradient(#fff,#fff) bottom left / 20px calc(50% - 20px),
    
    linear-gradient(#fff,#fff) center/calc(100% - 40px) 100%;
   background-repeat:no-repeat;
   filter:drop-shadow(0 0 5px #000); 
}

body {
  background:pink;
}
<div class="box">
</div>

CSS透明三角形带阴影

您也可以这样做,clip-path但您需要一个额外的包装器来应用drop-shadow过滤器

.box {
  margin-top: 50px;
  border-radius: 10px;
  width: 200px;
  height: 200px;
  background: 
   repeating-linear-gradient(to right, gold 0px, gold 5px, transparent 5px, transparent 10px) center/100% 2px no-repeat,
   #fff;
  clip-path:polygon(
  0 0,100% 0,
  100% calc(50% - 20px),calc(100% - 20px) 50%,100% calc(50% + 20px),
  100% 100%, 0 100%,
  0 calc(50% + 20px),20px 50%,0 calc(50% - 20px)
  );
}

.drop {
  filter: drop-shadow(0 0 5px #000);
}

body {
  background: pink;
}
<div class="drop">
  <div class="box">
  </div>
</div>


推荐阅读