Search code examples
csstransformcss-shapes

Rounded skewed top shape in css


I need to make a div that looks like this:

skewed top with rounded corners

With text in the middle in css. I tried looking at transforms and other 3d stuff, but I couldn't figure it out, especially without ruining the text.


Solution

  • You can use a skewed Y pseudo element as a background of the element. This won't affect the content :

    div {
      position: relative;
      display: inline-block;
      padding: 20px 50px;
      overflow: hidden;
    }
    div:before {
      content: '';
      position: absolute;
      top: 0; left: 0;
      width: 100%; height: 100%;
      background: #FFD505;
      -webkit-transform-origin: 0 0;
      -ms-transform-origin: 0 0;
      transform-origin: 0 0;
      -webkit-transform: skewY(5deg);
      -ms-transform: skewY(5deg);
      transform: skewY(5deg);
      z-index: -1;
      border-radius: 10px 10px 0 0;
    }
    <div>Some text</div>

    • The div has overflow:hidden so that the overflowing parts of the pseudo-element are hidden.
    • The pseudo element has a negative z-index so it stacks behind the content of the div
    • The transform-origin is set to 0 0 so that top left of the skewed pseudo element doesn't move.