Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a div with an irregular shape?

Is it possible to create a div with an irregular shape with CSS? I already searched but I can't find a good example. The style is something like this:

              /
             /                     \
            /                       \
           /                         \
          /___________________________\    

There should be a line on the top that connects it. Basically it has different height on the left and right side.

like image 216
Lenonskie Avatar asked Aug 13 '15 11:08

Lenonskie


People also ask

How do you make an irregular shape with HTML and CSS?

HTML & CSS Answer This can be done by using perspective and transform . You create two divs: one that will get the perspective and one that will be transformed. Just know that anything in the . test -div will also be transformed, so if you put text in there, it will also get transformed.


1 Answers

This can also be done using CSS clip-path

div {
  width: 200px;
  height: 150px;
  background: red;
  -webkit-clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
  clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
}
<div></div>

You can then just change the background element if you need an image.

div {
  width: 200px;
  height: 150px;
  background: url(https://lorempixel.com/200/150/);
  -webkit-clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
  clip-path: polygon(20% 10%, 85% 30%, 100% 100%, 0% 100%);
}
<div></div>

In their current state, clip-paths aren't as widely supported as inline or imported SVG but is a much cleaner, and in some cases, easier variant to use.

  • Browser Support
like image 98
Stewartside Avatar answered Oct 26 '22 16:10

Stewartside