How would one create a triangle container for an image (x-browser)

前端 未结 2 411
无人共我
无人共我 2021-01-18 06:48

How would I create a DIV containing an IMG where the DIV cuts the image into a triangle, thereby displaying only part of the image though a triangle.

so..



        
相关标签:
2条回答
  • 2021-01-18 07:28

    You can't create a non-rectangular DOM element.

    There are a few ways to hack it.

    Firstly, there is a method of using CSS borders with varying widths on each side of the element to make it look triangular. It will still be a rectangle, but it will look like a triangle.

    There's a tutorial on this here: http://www.russellheimlich.com/blog/pure-css-shapes-triangles-delicious-logo-and-hearts/

    The down-side of this approach is that it is limited to creating right-angled triangles. You can join several of them together to get around this, but then you don't have a single container for your image.

    An alternative hacky way of doing it would be to place rotated elements on top of the main element so that they cover the appropriate parts of the image and make it look triangular. This works in all browsers, although IE does have some very nasty syntax to do rotation, and it's quite heavy on the browser, considering that you'd only be using it to make shapes.

    Another option might be to use CSS transforms. However this is only supported by a minority of up-to-date browsers, so it won't work for most users.

    A better approach might be to use a proper graphics library for this, rather than trying to shoe-horn it into a <div> element.

    I'd recommend using Raphael. It's a Javascript library which can draw directly into the browser using SVG (or VML for IE). It's trivial to create triangles using it and to fill them with a graphic. See the examples on the Raphael home page to get you started.

    0 讨论(0)
  • 2021-01-18 07:44

    Depending on what you want the outcome to be, as far as i'm aware you cant make a triangle DIV without Transform:; However one solution would be having a div positioned inside the div in question with a PNG cutting of half the image showing only the transparent part through. Not sure if this is a viable option for you though.

    0 讨论(0)
提交回复
热议问题