This is basically what i want to achieve --> https://jsfiddle.net/tak1pyt7/1/
.clip {
width: 500px;
height: 500px;
-webkit-clip-path: polygon(100% 0, 100% 81%, 82% 100%, 0 100%, 0 0);
clip-path: polygon(100% 0, 100% 81%, 82% 100%, 0 100%, 0 0);
}
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
height: 100%;
width: 100%;
}
<body>
<div class="clip">
<img src="http://lorempixel.com/600/600/" width="100%" height="100%" />
</div>
</body>
Although i have a solution but there are various issues with the code in the fiddle as follows :
- It doesn't work in IE or Edge as clip path isn't supported
- Clip path doesn't support css shapes in firefox and if you need to make it work in firefox then you need to supply inline svg
- Tried supplying inline svg as well but it has it's own set of issues that doesn't solve my requirement. I need the cut dimensions to remain the same irrespective of the height and width of the container. With inline svg that cuts the image right now, it's dimension changes with change in height and width i.e. the cut is responsive. I need static dimensions of the cut.
Other than above solution i searched a whole lot of solutions that might help me create div's with corners cut and the cut is transparent itself because i don't have a solid background at the back.
Other solutions explored
Using CSS3 Gradients sample
- Sample --> http://jsfiddle.net/spdawson/3Tc8S/light/
- Doesn't work when you have the content which occupies full height and width of the area such as the fiddle i provided at the beginning of the post.
Using CSS Borders
Sample --> http://jsfiddle.net/spdawson/HhZQe/light/
Doesn't work for me because The container on which the cut has to be made has to contain solid background color which is not the case for my design.
Using jQuery plugin (http://jquery.malsup.com/corner/)
Sample code --> http://jsfiddle.net/spdawson/nxnCD/light/
You need to have solid background color which isn't the case considering my requirement. I have a image as a background instead.
A Solution that WORKS but is very HACKY
Sample --> http://jsfiddle.net/26v7pfLb/
I am using this solution right now for containers that have fixed height and width but i have a page in my app where the containers have a static width but dynamic height. In that case the hack would be pretty difficult to implement and it seems very weird to use this solution personally.
I am trying to find something more elegant and clean to implement such a kind of solution in CSS
Any pointers would be helpful.
You can use the approach I described in this question : Cut Corners using CSS using a rotate container with
overflow:hidden;
For your example (as you want to display an image inside the element) you need to "unrotate" the content like this :
This solution also requires positioning either with margins or with absolute positioning.
CSS Solution:
The solution you use is best at this time (September 2015). It is not a hack, but just a simple 2D rotations and it will work in IE also.
Best article for CSS masking in browsers other than IE you can find on this page. It uses SVG with html foreignObject element, which is not fully supported.
An
elegant and clean
CSS solution will be available in years to come, when css-mask and clip-path come alive.Javascript Solution:
We can use HTML Canvas element to store the image data, than using Javascript to make part of the image transparent.
In following example we shall make bottom-right triangle transparent. There is a catch: the image must be on same domain, which is not case here on StackOverflow (it also cannot be base64 encoded).
I made it work on Liveweave playground, just open and wait for image to load and script to execute (the image is huge).
The source code is also below.
The image we (cannot) use:
PHP Solution:
Similar to above, you could use iMagick library to make part of the images transparent. You could use iMagick mask, or directly change pixels' alpha - you can find an example in this answer.