Search code examples
htmlcsscurve

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...

Here's what I need:

enter image description here

This would be the working 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?


Solution

  • Something like this can be achieved but it's troublesome. SVG will be better for this.

    Referenced from 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>