【问题标题】:Double curved shape双曲线形状
【发布时间】:2023-03-20 00:41:01
【问题描述】:

我目前正在尝试生成“波浪状的鬼底”形状。此形状包含两条双曲线:

虽然this image的底部我认为用更好的图像来描绘它。


我的代码

我当前尝试生成此形状是使用伪元素和overflow: hidden,尽管这不允许使用渐变背景(需要纯背景):

尝试 1 - 使用隐藏溢出的伪元素

.bottom {
  height: 300px;
  width: 300px;
  background: lightgray;
  position: relative;
  overflow: hidden;
  margin-top:-150px;
  -webkit-transform:rotate(45deg);
  transform:rotate(45deg);
}
.bottom:before, .bottom:after{
  position: absolute;
  content: "";
  background: white; 
}
.bottom:before {
    height: 150%;
  width: 150%; 
  top: 50%;
  border-radius:50%;
  left: -45%;
}

.bottom:after {
    height: 200%;
  width: 100%; 
  bottom: -40%;
  border-radius:50%;
  left: 90%;
}
<div class="bottom"></div>

尝试 2 - 使用具有 's' 形状的伪元素

.bottom {
  background: lightgray;
  width: 300px;
  height: 300px;
  position: relative;
  overflow:hidden;
  color:white;
  border-radius:0 100% 0 100%;
}
.bottom:before{
  content:"S";
  position:absolute;
  height:100%;
  width:100%;
  top:-100%;
  left:-75%;
  font-size:60em;
  font-family: 'arial';
  }

.bottom:after{
  content:"S";
  position:absolute;
  height:100%;
  width:100%;
  top:-150%;
  left:-75%;
  font-size:60em;
  font-family: 'arial';
  }
<div class="bottom"></div>

尝试 3 - 额外元素和盒子阴影

我最近也尝试过使用盒子阴影和额外元素(我可以接受),但即便如此,我也无法正确创建它:

.bottom {
    height:300px;
    width:300px;
    position:relative;
    overflow:hidden;
}
.bottom-left {
    position:absolute;
    top:50%;
    left:-50%;
    height:100%;
    width:100%;
    border-radius:50%;
    box-shadow: inset -35px 35px 0px -24px rgba(50, 50, 50, 1);
    z-index:8;
    background:white;
}
.top {
    position:absolute;
    height:100%;
    top:-35%;
    left:0;
    width:50%;
    border-radius:50%;
    z-index:8;
    background:gray;
    box-shadow:inset 35px -35px 0px -24px rgba(50, 50, 50, 1);
}
.top-right {
    position:absolute;
    top:-80%;
    left:45%;
    height:120%;
    width:100%;
    border-radius:50%;
    box-shadow:inset 35px -35px 0px -24px rgba(50, 50, 50, 1);
    border:20px solid gray;
}
.bigone {
    position:absolute;
    top:0;
    left:-20%;
    height:105%;
    width:100%;
    border-radius:50%;
    box-shadow:inset -35px -35px 0px -24px rgba(50, 50, 50, 1);
    -webkit-transform:rotate(-30deg);
    transform:rotate(-30deg);
    -webkit-transform-origin:center center;
    transform-origin:center center;
    background:gray;
}
<div class="bottom">
    <div class="bottom-left"></div>
    <div class="top"></div>
    <div class="top-right"></div>
    <div class="bigone"></div>
</div>

这些方法似乎都不能轻松生成这种双曲线形状,并且需要“块色背景”

注意:我不愿意求助于 SVG,因为我已经完成了 90% 的“整体形状”只使用纯 css,所以完成这个没有 svg 元素会很好/很好


内部形状将是块颜色,但边框在我的设计中不是强制性/关键的。

this is where I would like to add it to


更新

【问题讨论】:

  • 坦率地说,我怀疑这是否可以使用 CSS(至少在没有多个元素的情况下)...SVG 将是我要走的路。
  • 虽然 CSS 可能不是正确的工具,但当被要求产生“幽灵般的底部”时,我很想看看互联网会出现什么。
  • 查看css shapes这个很棒的链接我不认为那里有你想要的那种形状,但你可以看看制作纯css复杂形状的技术.希望对你有帮助
  • @jbutler483 这不是使用 CSS 制作这种奇怪形状的原因
  • @jbutler483 我称之为“精灵效应”。苹果用这个术语来描述“最小化到停靠”动画,这看起来很相似。

标签: css svg css-shapes


【解决方案1】:

考虑:

  • 所需的代码量
  • 对齐双曲线的麻烦

CSS 似乎不是这里的方式,而 SVG 更合适。为了说明,请看这两个 sn-ps:

SVG

DEMO

/*** FOR THE DEMO **/
svg{
    display:block;
    width:70%;
    margin:0 auto;
    opacity:0.8;
}
body{
    background: url('http://lorempixel.com/output/people-q-g-640-480-7.jpg');
    background-size:cover;
}
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 100 80">
    <path stroke-width="1" stroke="#000" fill="grey" d="M95 5 Q70 20 70 38 T50 65 Q55 50 30 40 T5 5z"/>
</svg>

CSS

DEMO(考虑我只在形状右侧做了一条双曲线)

.ghost {
  position: relative;
  width: 400px;
  height: 300px;
  margin: 0 auto;
  overflow: hidden;
}
.ghost:before,
.ghost:after {
  content: '';
  position: absolute;
}
.ghost:before {
  bottom: 0;
  right: 50%;
  width: 70%;
  height: 30%;
  transform-origin: 100% 100%;
  transform: skewY(30deg) rotate(20deg);
  box-shadow: -100px -100px 0px 99px #656565;
  border-top-right-radius: 30% 100%;
}
.ghost:after {
  top: 0;
  right: 0;
  transform-origin: 100% 0;
  transform: skewX(-10deg) rotate(-20deg);
  box-shadow: none;
  height: 107px;
  width: 173px;
  border-top-left-radius: 90% 100%;
  box-shadow: -30px -30px 0px 29px #656565, 60px -110px 0px 109px #656565;
}
<div class="ghost">
</div>

请注意,我没有列出在这种情况下使用 svg 的优势(响应速度、输出质量、曲线控制、边框、边框颜色/不透明度、填充颜色/不透明度、透明度、可维护性、数量构建形状的时间......)

【讨论】:

  • 是的,这就是我希望提问者使用的! jbutler483 应该使用 svg!太棒了!
  • 建议 svg 解决方案的道具。在这种情况下,它确实是一个更好的选择。
【解决方案2】:

您应该使用 boxshadows 和溢出来制作该形状。

body {background:url('http://whofortedblog.com/wp-content/uploads/2013/01/33c9f33218a6cab6054375fb76129a80.jpeg');
background-size: cover;}
div {
  height: 100px;
  width: 200px;
  overflow: hidden;
  position: relative;
  -webkit-transform: scale(1,1.1);
  -moz-transform: scale(1,1.1);
  -ms-transform: scale(1,1.1);
  -o-transform: scale(1,1.1);
  transform: scale(1,1.1);
}
div:before {
  height: 80px;
  width: 100px;
  border-radius: 50% / 50%;
  box-shadow: 40px -11px 0 -20px white, 42px -22px 0 -10px white, 50px -28px 0 -8px white, 36px -95px 0 20px white;
  content: "";
  position: absolute;
  -webkit-transform: scale(0.9,1.1);
  -moz-transform: scale(0.9,1.1);
  -ms-transform: scale(0.9,1.1);
  -o-transform: scale(0.9,1.1);
  transform: scale(0.9,1.1);
  top: 50%;
  left: -10px;
}
div:after {
  height: 70px;
  width: 120px;
  border-radius: 50%;
  -webkit-transform: rotate(-35deg);
  -moz-transform: rotate(-35deg);
  -ms-transform: rotate(-35deg);
  -o-transform: rotate(-35deg);
  transform: rotate(-35deg);
  box-shadow: ;
  content: "";
  position: absolute;
  top: -1%;
  box-shadow: -1px -28px 0 5px white;
  right: -35px;
}
&lt;div&gt;&lt;/div&gt;

您当然可以使用好的位置值来改进这个版本! 无论如何,您几乎不应该使用此解决方案。我认为最好的选择是 png 图像或 SVG。

工作:

div {
  height: 100px;
  width: 200px;
  overflow: hidden;
  position: relative;
  border: 1px solid black;
}
div:before {
  height: 80px;
  width: 100px;
  border-radius: 50% / 50%;
  background-color: red;
  box-shadow: 40px -9px 0 -20px blue, 42px -20px 0 -10px pink, 50px -25px 0 -8px plum, 37px -95px 0 20px green;
  content: "";
  position: absolute;
  top: 50%;
  left: -10px;
}
div:after {
  height: 70px;
  width: 120px;
  border-radius: 50%;
  background-color: rgba(255, 215, 0, 0.6);
  -webkot-transform: rotate(-35deg);
  -moz-transform: rotate(-35deg);
  -o-transform: rotate(-35deg);
  -ms-transform: rotate(-35deg);
  transform: rotate(-35deg);
  box-shadow: ;
  content: "";
  position: absolute;
  top: -1%;
  box-shadow: -4px -27px 0 5px rgba(0, 255, 215, 0.6);
  right: -44px;
}
&lt;div&gt;&lt;/div&gt;

【讨论】:

  • 发生了什么?早上这个答案不一样,现在我看起来像个尖叫者
  • @ViROscar 它仍在工作,它只是有一个背景。形状在左上角。我会说 SVG 可能是其他人提到的方式。请注意,CSS 版本有时看起来还可以,但当我滚动它时,它在我的浏览器 (Firefox) 中可能会出现问题。 SVG 版本显示完美,没有任何问题。
  • 看起来不可怕吗? :) @Goblinlord 你如何滚动/它不是大形状,它是角落里的一个小形状,所以滚动条不会出现。但你是对的 svg 是这项工作的核心工具。
  • @TimKrul 我的意思是当我滚动浏览此页面时,它实际上显示在此处。
  • @Goblinlord 很抱歉,我想知道您是如何滚动的。 :) 还有很多弓箭手不支持转换 css:w3schools.com/css/css3_2dtransforms.asp
猜你喜欢
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多