如何为多边形添加圆角?附当前实现代码
给裁剪后的多边形添加圆角
我尝试为多边形添加圆角,想要的效果如下:
以下是我当前的代码:
HTML代码
<div class="sideheader">Exercise ▶▶ Tracker</div> <div class="topheader"> <span>Version 1.9</span> <span>API Front-End</span> <a href="https://codepen.io/bbx/">made by bbx</a> </div> <div class="wrapper"> <input type="checkbox" id="submit1"> <div class="flipper"> <div class="front"> <div class="container"> <div class="content" id="contentFour"> <h1>API Documentation</h1> </div> <svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur"/> <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo"/> <feComposite in="SourceGraphic" in2="goo" operator="atop"/> </filter> </defs> </svg> </div> </div> </div> </div>
CSS代码
@import url("https://fonts.googleapis.com/css?family=K2D"); * { box-sizing: border-box; } body { color: white; background: linear-gradient( 290deg, rgba(228, 37, 141, 1) 0%, rgba(254, 130, 202, 1) 100%) fixed; background-size: cover; min-height: 100vh; font-family: K2D, "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; font-weight: 300; line-height: 1.25; display: flex; justify-content: center; margin: 0 auto; overflow-x: hidden; } b { font-weight: 800; } .sideheader { position: absolute; bottom: 25px; left: 5px; margin: 1em 0 0 0; padding: 0; font-weight: bold; font-size: 2em; writing-mode: vertical-rl; transform: rotate(180deg); } .topheader { position: absolute; width: 100%; top: 0; left: 0; padding: 5px; font-weight: bold; display: flex; justify-content: space-between; } .topheader a { color: white; text-decoration: none; } .wrapper { background-color: transparent; position: relative; margin: 15vmin 20vw 5vmin 20vw; width: 100%; height: 100%; } .flipper { position: relative; width: 100%; height: 100%; text-align: center; transition: transform 0.8s; transform-style: preserve-3d; backface-visibility: hidden; } #submit1:checked+.flipper { transform: rotateY(180deg); } .front, .back { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; } .back { transform: rotateY(180deg); } .container { display: flex; width: 100%; background: rgba(0, 0, 0, 0.25); box-shadow: -0.5em 1em 2em rgba(0, 0, 0, 0.5); } .container>input, .wrapper>input { position: absolute; top: 0; left: -9999px; } .tab { position: absolute; z-index: 1; display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; height: 8vmin; width: 100%; backface-visibility: hidden; } .tab>label { display: flex; justify-content: center; align-items: center; height: 100%; border-right: 1px solid rgba(0, 0, 0, 0.25); cursor: pointer; letter-spacing: 1px; } .tab>label:last-child { border: none; } .content { background: linear-gradient( 45deg, rgba(97, 161, 239, 1) 0%, rgba(135, 92, 207, 1) 29%, rgba(188, 53, 169, 1) 65%, rgba(251, 8, 129, 1) 100%); width: 100%; backface-visibility: hidden; padding: 8vmin 0 5vmin 0; align-items: center; flex-direction: column; } .content.res { padding: 1em; } #contentFour { margin-top: 60px; margin-left: 40px; clip-path: polygon(75% 8vmin, 75% 0, 100% 0%, 100% 100%, 0 100%, 0 8vmin); filter: url('#goo'); }
实现方案
方案一:使用带圆角的clip-path路径
直接通过path()函数定义带圆角的多边形路径,替代原有的polygon(),可以精准控制每个角的圆角大小,同时避免滤镜带来的性能损耗:
#contentFour { margin-top: 60px; margin-left: 40px; /* 定义带10px圆角的路径,可根据需求调整圆角数值 */ clip-path: path('M 0 8vmin Q 0 calc(8vmin - 10px) 10px calc(8vmin - 10px) L 0 calc(100% - 10px) Q 0 100% 10px 100% L calc(100% - 10px) 100% Q 100% 100% 100% calc(100% - 10px) L 100% 10px Q 100% 0 calc(100% - 10px) 0 L calc(75% + 10px) 0 Q 75% 0 75% 10px L 75% calc(8vmin - 10px) Q 75% 8vmin calc(75% - 10px) 8vmin Z'); /* 移除goo滤镜 */ }
方案二:伪元素叠加+border-radius(兼容性更好)
如果path()函数兼容性无法满足需求,可以用伪元素模拟裁剪区域的圆角效果:
#contentFour { margin-top: 60px; margin-left: 40px; clip-path: polygon(75% 8vmin, 75% 0, 100% 0%, 100% 100%, 0 100%, 0 8vmin); border-radius: 0 10px 10px 10px; /* 设置右下角、左下角、右上角的圆角 */ position: relative; overflow: hidden; } #contentFour::before { content: ''; position: absolute; top: 0; right: 0; width: 25%; /* 对应clip-path的75%宽度比例 */ height: 8vmin; background: linear-gradient(45deg, rgba(97, 161, 239, 1) 0%, rgba(135, 92, 207, 1) 29%, rgba(188, 53, 169, 1) 65%, rgba(251, 8, 129, 1) 100%); border-top-right-radius: 10px; /* 匹配右上角圆角 */ }
两种方案都能实现目标效果,方案二的兼容性更优,方案一的视觉精度更高,可根据实际需求选择。
内容的提问来源于stack exchange,提问作者mooaz
相关产品推荐
相关产品推荐

