简体   繁体   English

CSS 让一个 div 上下弯曲,但左右两边是直的

[英]CSS Make a div curved at top and bottom but straight at the left and right sides

Hey guys I've been wondering if it was possible to do this, I've tried with border-radius but it only makes curves to the left and right sides apparently...嘿伙计们,我一直想知道是否有可能做到这一点,我已经尝试过使用border-radius ,但它显然只会在左侧和右侧产生曲线......

Here's what I need:这是我需要的:

在此处输入图像描述

This would be the working div :这将是工作div

 #mainbox { width: 115px; height: 24px; background-color: gray; border: 1px solid #000000; text-align: center; }
 <div id="mainbox"> <div id="secondbox">test</div> </div>

Any possible ideas?有什么可能的想法吗?

A few possible ideas:一些可能的想法:

  • You can try using border-top-left-radius: 30%; border-top-right-radius: 30%;您可以尝试使用border-top-left-radius: 30%; border-top-right-radius: 30%; border-top-left-radius: 30%; border-top-right-radius: 30%; . . The problems with this are twofold: You'll see that it does end up curving the corners too when the percentages get higher (which isn't what you want), and it also doesn't support inverted, so you won't be able to use it for the bottom of the div.这样做的问题是双重的:当百分比变高时,你会看到它最终也会弯曲角落(这不是你想要的),而且它也不支持倒置,所以你不会能够将它用于 div 的底部。
  • Or, you can mess around with the clip-path property, like this: clip-path: polygon(0 0, 100% 0, 100% 96%, 0 100%);或者,您可以乱用 clip-path 属性,如下所示: clip-path: polygon(0 0, 100% 0, 100% 96%, 0 100%); This article https://www.viget.com/articles/angled-edges-with-css-masks-and-transforms talks about how he uses that css property to make slanted divs.这篇文章https://www.viget.com/articles/angled-edges-with-css-masks-and-transforms讨论了他如何使用该 css 属性来制作倾斜的 div。
  • Take a look at the methods presented in Invert rounded corner in CSS?看看在 CSS 中反转圆角中介绍的方法? . . The point there was to try to make an inverted rounded corner, and a lot of the solutions presented pretty creative ways to manipulate the borders of a div.重点是尝试制作一个倒圆角,许多解决方案都提供了非常有创意的方法来操纵 div 的边框。

I know this isn't a complete answer, but I hope it helps anyways :)我知道这不是一个完整的答案,但我希望它无论如何都会有所帮助:)

Something like this can be achieved but it's troublesome.这样的事情可以实现,但很麻烦。 SVG will be better for this. SVG 会更好。

Referenced from this question on SO.this question on SO中引用。

 body { width: 100%; height: 100vh; display: flex; align-items: center; justify-content: center; } #mainbox { width: 200px; height: 130px; overflow: hidden; position: relative; display: flex; align-items: flex-end; justify-content: center; } #mainbox::before, #mainbox::after { content: ""; display: block; position: absolute; height: 100px; /* equal to inner curvedbox */ border-left: 5px solid black; bottom: 0; z-index: 1; } #mainbox::before { left: 0; } #mainbox::after { right: 0; } #curvedbox { position: relative; width: 100%; height: 100px; display: flex; align-items: center; justify-content: center; } #curvedbox::before, #curvedbox::after { display: block; content: ""; width: 140%; height: 200%; border: solid 5px #000; border-color: #000 transparent transparent transparent; border-radius: 100%; position: absolute; left: 50%; transform: translateX(-50%); } #curvedbox::before { top: -30%; } #curvedbox::after { top: 69%; } #secondbox { transform: translateY(-140%); }
 <div id="mainbox"> <div id="curvedbox"> <div id="secondbox">test</div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM