Search code examples
csscss-shapes

css div containers with irregular shapes


Hi,

I am aware that it is possible to achieve all kind of shapes with CSS but I havent found an example that allows you to actually put content INSIDE which is READABLE as well. I want to make a div that looks like a trapezoid and put some text in it like this:

#trapezoid {
  border-left: 25px solid transparent;
  border-radius: 10px;
  border-right: 25px solid transparent;
  border-top: 100px solid rgba(114, 230, 245, 0.3);
  height: 0;
  width: 100px;
}

this code draws the trapezoid but I cant put anything inside the trapezoid because everything goes below the actual shape. How should I do it?

Thank you.


Solution

  • Since all the answers use 2 elements, let's post another with a single element. Uses padding to keep the text inside the trapezoid, and 2 gradients to create the background in trapezoid shape

    div {
      width: 300px;
      padding: 0px 50px;
      background-image: linear-gradient(80deg, transparent 40px, lightblue 40px), linear-gradient(-80deg, transparent 40px, lightblue 40px);
      background-size: 51% 100%;
      background-position: left bottom, right bottom;
      background-repeat: no-repeat;
    }
    <div>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>

    Another example, with border and shadows, but for this one you need 3d transform on a pseudo element

    div {
      width: 400px;
      margin: 50px;
      position: relative;
    }
    
    div:after {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0px;
      left: 0px;
      background-color: lightblue;
      border: solid 2px blue;
      z-index: -1;
      transform: perspective(200px) rotateX(-10deg);
      transform-origin: center bottom;
      border-radius: 10px;
      box-shadow: 10px 10px 10px gray;
    }
    <div>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>