htmlcsssass

How to create a responsive ellipse or semi circle with CSS/SCSS


I need to create a responsive ellipse with SCSS like this ellipse:

The height should be always 72px and also be responsive

This is a mockup of how it should look like:

At the moment my code looks like this

It looks like this current ellipse but it's not as "narrow" and "pointy" as it should be

<div className="landing-bg__ellipse" />
.landing-bg__ellipse {
  width: 100%;
  height: 4.5rem;
  background-color: #95cf9d;
  border-radius: 50% 50% 0 0;
}

Solution

  • To create a half ellipse, you can use border-top-left-radius and border-top-right-radius. You can control the rounding arc on each corner, in your case, you need to create an arc of ellipse.

    You can refer the MDN web docs about border-top-left-radius. It explains how rounding corners works.

    The rounding can be a circle or an ellipse, or if one of the value is 0, no rounding is done and the corner is square.

    An image with three examples that explains how to rounding corner works. Example one, no rounded corner. Example two, rounded using an arc of circle. Example three rounded using an arc of ellipse

    You can read about border-top-right-radius, the explanation is same border-top-left-radius

    Run the code snippet below to see the result, this is the closet to what asked for. Responsive and with fixed height. See it on full page and resize the window.

    *, *:after, *:before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .landing-bg__ellipse {
      width: 100%;
      height: 4.5rem;
      background-color: #95cf9d;
      border-top-left-radius: 50% 100%;
      border-top-right-radius: 50% 100%;
    }
    <div class="landing-bg__ellipse" />

    This is an example uses a container and changes the position and scale of the element.

    *, *:after, *:before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .container {
      height: 4.5rem;
      overflow: hidden;
    }
    
    .landing-bg__ellipse {
      transform: translateY(50%) scale(1.15);
      width: 100%;
      height: 4.5rem;
      background-color: #95cf9d;
      border-top-left-radius: 50% 100%;
      border-top-right-radius: 50% 100%;
    }
    <div class="container">
      <div class="landing-bg__ellipse" />
    </div>