【问题标题】:CSS shape with inset curve and transparent background带有插入曲线和透明背景的 CSS 形状
【发布时间】:2014-09-02 00:42:37
【问题描述】:

我需要创建一个像这张图片一样的 CSS 形状..

请查看我的工作fiddle 我已经创建了类似的东西,但我无法给出曲线。

#shape {     
    border-left: 70px solid transparent;
    border-top: 100px solid red;
    height: 0;
    width: 200px;
} 

谁能帮帮我?

【问题讨论】:

  • 白色部分必须是透明的还是颜色可以?
  • 这个 URL 在不久前帮助了我很多学习和理解如何使用伪元素来制作许多不同的形状。我建议你检查一下:css-tricks.com/examples/ShapesOfCSS
  • 白色部分应该是透明的@NicoO
  • @AlvaroMenéndez 感谢您的网址。

标签: html css svg css-shapes


【解决方案1】:

您可以使用带有边框半径和背景阴影的伪元素来创建曲线并为曲线启用透明背景

输出:

#shape {
  width: 300px; height: 100px;
  position: relative;
  overflow: hidden;
}

#shape:before {
  content: '';
  position: absolute;
  top: 10%; right: 0;
  width: 300%;
  padding-bottom: 300%;
  border-radius: 100%;
  background: none;
  box-shadow: 10px -10px 5px 300px #F15723;
  z-index: -1;
}

body{background:url(https://farm9.staticflickr.com/8461/8048823381_0fbc2d8efb.jpg);background-size:cover;}
<div id="shape"></div>

demo

【讨论】:

  • 嘿,我在页面上添加了,但遇到问题,您使用的是白色,但我需要在上面添加它,所以曲线应该是透明的..你能告诉我怎么做这样做?
  • 抱歉,还需要问一件事 :( 我需要在该 div 中添加一些文本。怎么做?当我添加时它不会显示。
  • 使用z-index属性来显示你的div内容后面的伪元素:jsfiddle.net/webtiki/38GJA/5
  • 我通过将锚标签定位为 abs.. 你能检查stackoverflow.com/questions/24699139/… 你能告诉我有什么问题吗?希望你能解决这个问题.. :)
【解决方案2】:

变体 #01:

CSS3 linear-gradient() 也可以绘制这个背景:

CSS:

div {
  background: linear-gradient(45deg, transparent 50px, tomato 50px);
}

输出图像:

body {
  background: linear-gradient(lightgreen, green);
  min-height: 100vh;
  margin: 0;
}
div {
  background: linear-gradient(45deg, transparent 50px, tomato 50px);
  height: 150px;
  margin: 20px;
  width: 400px;
}
<div>
  
</div>

变体 #02:

我们可以使用:before:after 伪元素并使用css3 转换来制作这个带有圆角的形状。

body {
  background: linear-gradient(lightgreen, green);
  min-height: 100vh;
  margin: 0;
}
div {
  border-radius: 10px;
  position: relative;
  overflow: hidden;
  height: 150px;
  margin: 20px;
  width: 400px;
}
div:before {
  border-radius: 0 0 10px 10px;
  transform-origin: 100% 0;
  transform: skewY(45deg);
  background: tomato;
  position: absolute;
  width: 45px;
  z-index: -1;
  content: '';
  bottom: -5px;
  left: 0;
  top: 0;
}
div:after {
  border-radius: 0 10px 10px 10px;
  background: tomato;
  position: absolute;
  content: '';
  left: 35px;
  bottom: 0;
  right: 0;
  top: 0;
}
<div>

</div>

【讨论】:

  • 我想你一定错过了问题中的这个陈述 - 但我不能给出曲线。。您的答案不会产生曲线。
  • @Harry 是的,你是对的。我会尝试更新我的答案,如果做不到,我会删除它。
  • 仍然不确定这是否是 OP 所追求的。我认为他们正在寻找切割本身是弯曲的,但屏幕截图似乎不同。无论如何,我会把它留在这里,也许 OP 会回应:)
  • @Harry 好的,如果 OP 将提供澄清所需的行为,我会更新我的答案。
猜你喜欢
  • 2021-05-09
  • 2017-03-14
  • 2012-10-15
  • 2018-11-23
  • 2015-11-12
  • 2011-08-02
  • 1970-01-01
  • 2015-06-09
相关资源
最近更新 更多