如何实现带圆角端点的局部边框矩形?附现有代码
实现带圆角端点的局部边框矩形
我想要创建一个带有局部边框且边框端点为圆角的矩形(效果为矩形边框顶部缺失一段,缺失部分的两个端点为圆角)。目前已实现移除部分边框的效果,但无法简便达成圆角端点的最终效果,现有代码如下:
body { position: relative; } div.inner { position: absolute; top: 10px; left: 10px; display: inline-block; width: 420px; height: 320px; background-color: #f80; border-radius: 40px; } div.border { position: absolute; display: inline-block; width: 400px; height: 300px; background-color: transparent; border-radius: 40px; border: 20px solid black; clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0%, 60% 0%, 60% 75%, 25% 75%, 25% 0%); }
<body> <div class="inner"></div> <div class="border"></div> </body>
可以放弃clip-path的裁剪方式,通过伪元素模拟顶部的两段边框,并给端点设置圆角,代码修改如下:
body { position: relative; } div.inner { position: absolute; top: 10px; left: 10px; display: inline-block; width: 420px; height: 320px; background-color: #f80; border-radius: 40px; } div.border { position: absolute; display: inline-block; width: 400px; height: 300px; background-color: transparent; border-radius: 40px; /* 保留左右下三边边框,去掉顶部边框 */ border: 20px solid black; border-top: none; } /* 顶部左侧边框段,右端设置圆角 */ div.border::before { content: ''; position: absolute; top: -20px; left: 0; width: 25%; height: 20px; background-color: black; border-top-left-radius: 40px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; } /* 顶部右侧边框段,左端设置圆角 */ div.border::after { content: ''; position: absolute; top: -20px; right: 0; width: 40%; height: 20px; background-color: black; border-top-right-radius: 40px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; }
<body> <div class="inner"></div> <div class="border"></div> </body>
说明
- 先给
border元素保留左右下三边的黑色边框,去掉顶部边框; - 用
::before伪元素实现顶部左侧的边框段,宽度设为25%,右端点的圆角半径设为10px(等于边框宽度的一半); - 用
::after伪元素实现顶部右侧的边框段,宽度设为40%(100%-60%),左端点的圆角半径同样设为10px; - 伪元素的
top值设为-20px,刚好和原边框的位置对齐。
内容的提问来源于stack exchange,提问作者m-sarabi
相关产品推荐
相关产品推荐

