I am trying to create a triangle using internal and external SVG.
However for some reason it won't work.
I tried to use this tool here: http://cssplant.com/clip-path-generator
and get it's "POINTS" coordinates to create a perfect clip TRIANGLE on my internal and external SVG but no luck.
Here's my HTML:
<figure class="clip-holder">
<img src="https://www.thesun.co.uk/wp-content/uploads/2016/06/nintchdbpict000244881006.jpg" class="clip-svg-inline" width="200" height="200">
<figcaption>Inline SVG</figcaption>
</figure>
<figure class="clip-holder">
<img src="https://www.thesun.co.uk/wp-content/uploads/2016/06/nintchdbpict000244881006.jpg" width="200" height="200">
<figcaption>External SVG</figcaption>
</figure>
</div>
<svg class="clip-svg">
<defs>
<clipPath id="triangle" clipPathUnits="objectBoundingBox" >
<polygon points="120 263,325 262,222 42"/>
</clipPath>
</defs>
</svg>
And here's the CSS:
.clip-holder {
display: inline-block;
padding: 0;
margin: 30px;
}
.clip-css {
-webkit-clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
.clip-svg {
width: 0;
height: 0;
margin: 0 auto;
}
.clip-svg-inline {
-webkit-clip-path: url("#triangle");
clip-path: url("#triangle");
}
.clip-svg-external {
-webkit-clip-path: url("https://www.thesun.co.uk/wp-content/uploads/2016/06/nintchdbpict000244881006.jpgt");
clip-path: url("https://www.thesun.co.uk/wp-content/uploads/2016/06/nintchdbpict000244881006.jpg");
}
Am i making any mistakes?
Here's the JSFIDDLE: https://jsfiddle.net/stjtudvj/
(show me jsfiddle solution for better understanding)
The actual value of the clip-path property has to be an SVG clipPath. It can never be an image (like a JPG). It always to be the actual shape that should be applied on your image.
For example these are clipPath elements: https://wiki.selfhtml.org/wiki/CSS/Eigenschaften/Anzeige/clip-path#Grundformen_.28basic_shapes.29.
There is an example too, which pretty much looks like what you try to accomplish: https://wiki.selfhtml.org/wiki/CSS/Eigenschaften/Anzeige/clip-path#Anwendungsbeispiel.
So basically you apply a your predefined shape (e.g. by using your linked generator) to an image using the CSS-property clipPath (which describes the shape). Like this:
HTML
<img src="/path/to/my/image.jpg" id="triangle" />
CSS
img#triangle {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
You can use the clip-path property to supply an actual shape (like I mentioned above) or via url(). Latter is pointing either to an existing SVG in the DOM ("internal SVG") or to an actual URL containing an SVG ("external SVG").
An example you can find here: http://codepen.io/imohkay/pen/GJpxXY
Based on that example I updated your fiddle: https://jsfiddle.net/stjtudvj/2/
I fixed the inline #triangle SVG. Your values were exceeding the image dimensions.
Please keep in mind, that not all browsers support this property fully yet: http://caniuse.com/#search=clip-path
Related
This question already has answers here:
How to make a curved edge hexagon by using CSS
(6 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I'm working on a small project by myself. I want to make the edges of that hexagonal pfp round.
The code:
.hex img {
width: 50px;
height: 50px;
-webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
clip-path: polygon(0% 50%, 25% 100%, 75% 100%, 100% 50%, 75% 0%, 25% 0%);
object-fit: cover;
}
I've tried using border-radius, but little did I know that it would only make the sides of that hexagon round.
I'm trying to get something like this.
How do I make it like that?
Curved Path
I suggest that you use an SVG path with rounded corner:
https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
It should look like this (this is a heart shape as an example):
.hex img {
clip-path: path('M0.5,1 C0.5,1,0,0.7,0,0.3 A0.25,0.25,1,1,1,0.5,0.3 A0.25,0.25,1,1,1,1,0.3 C1,0.7,0.5,1,0.5,1 Z');
}
Here's the documentation concerning paths:
https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
You can also create the path with a vector editing software like Illustrator, or directly only with tools like this: https://yqnn.github.io/svg-path-editor/
SVG filter
Another solution would be to use SVG filters. Although it may look "simpler" I strongly suggest that you use curved paths (the solution I mentioned earlier) for performance and readability.
You can declare a filter that will smooth corners like that:
<svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="round">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
</svg>
Then, you just need to use the filter CSS property
.hex::before {
content:"";
display:block;
padding-top:86.6%;
background:red;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
.hex {
width: 100px;
height: 100px;
filter: url(#round)
}
source: https://dev.to/afif/css-shapes-with-rounded-corners-56h
I have created a right pointing content box using css clip-path polygon, It works well inside chrome, For firefox I have added Inline SVG, but still it is not working.
Here is work in progress link
Link
Here is the markup and code I used for creating the right pointing content box
HTML :
<svg width="0" height="0">
<defs>
<clipPath id="clip-shape" clipPathUnits="objectBoundingBox">
<polygon points="0 0, 0.95 0, 1 0.5, 0.95 1, 0 1" />
</clipPath>
</defs>
</svg>
<div class="application-content">
<h3>clip-path is not working in firefox</h3>
<p>Hi, I have created a right pointing content box using css clip-path polygon, It works well inside chrome, For firefox I have added Inline SVG, but still it is not working.</p>
</div>
CSS:
.application-content{
width:35%;
-webkit-clip-path: polygon(0% 0%, 95% 0, 100% 50%, 95% 100%, 0% 100%);
clip-path: polygon(0% 0%, 95% 0, 100% 50%, 95% 100%, 0% 100%);
clip-path: url("#clip-shape");
background-color:#f5f5f5;
padding:30px;
}
I don't know, where it is going wrong.
Interesting thing here is, The above code is working when used with jsfiddle or codepen. I am using latest version of wordpress with a custom code theme.
Here is the codepen link
I recently ran into a PSD template that has an unusual border with triangles. I know how to create triangles by itself using pure CSS, but the question is (just for interest) about this images:
Are these created just by a background that is already cut like this, or can I do it using the same CSS?
There are multiple ways in which this shape could be achieved. Using clip-path, CSS triangles, SVG, Canvas and image backgrounds are some of them. Each method has their own pros and cons. We cannot suggest one without knowing your needs fully. You should choose the method which fits your needs the best. Generally I would suggest using SVG to create such complex paths/shapes.
Below are a couple of sample snippets for creating this with CSS and SVG clip-path feature. It is not 100% exactly as you want it to be but I would leave the fine tuning part to you.
You would also need to adjust the content position such that part of it also doesn't get clipped. In the snippet I have used padding-top to achieve this. You can use other methods like positioning also.
Using CSS:
Using CSS clip-path, you can create a polygonal path and clip the element into the required shape. The main drawback of this approach would be the poor browser support for CSS clip-paths. Currently only Webkit powered browsers support the CSS clip-path feature.
.unusual-border{
box-sizing: border-box;
height: 200px;
width: 100%;
padding-top: 10%;
background: rgb(74,139, 207);
-webkit-clip-path: polygon(0% 25%, 40% 5%, 55% 20%, 60% 15%, 75% 25%, 100% 10%, 100% 100%, 0% 100%);
clip-path: polygon(0% 25%, 40% 5%, 55% 20%, 60% 15%, 75% 25%, 100% 10%, 100% 100%, 0% 100%);
}
/* Just for demo */
body{
background: url(http://lorempixel.com/600/400/abstract/1);
}
*{
margin: 0;
padding: 0;
}
<div class="unusual-border">Some content</div>
Using SVG:
SVG clip-path is very similar in working to the CSS version except that it has much better browser support than its CSS counterpart.
.unusual-border {
box-sizing: border-box;
height: 200px;
width: 100%;
padding-top: 10%;
background: rgb(74, 139, 207);
-webkit-clip-path: url("#unusual-border");
clip-path: url("#unusual-border");
}
/* Just for demo */
body {
background: url(http://lorempixel.com/600/400/abstract/1);
}
* {
margin: 0;
padding: 0;
}
<svg width="0" height="0">
<defs>
<clipPath id="unusual-border" clipPathUnits="objectBoundingBox">
<path d="M0,0.25 0.4,0.05 0.55,0.2 0.6,0.15 0.75,0.25 1,0.1 1,1 0,1z" />
</clipPath>
</defs>
</svg>
<div class="unusual-border">Some content</div>
Note: Clip paths (either version) are not supported in IE. If you wish to support IE then you can use pure SVG path element (not clipPath) to create the background image (or) use an image (or) use complex CSS transformations with multiple elements. I would certainly not recommend using CSS transformations and multiple elements.
Technically you could build something like that using pure CSS3, but that doesn't mean that it's a good idea.
More than likely, the template uses transparent PNG images which are defined as the background-image to the section's :before and :after pseudo-elements. Another option may be the use of <canvas>, or SVG.
Of course, without seeing the markup and CSS, it's almost impossible to say for certain how this designer has handled it. And of course you could have quite easily checked the source code to find the answer yourself...
I want to create a perspective crop like effect with CSS3 like this:
I did try it doing my self but couldn't, any help would be appreciated.
here's my attempt: http://jsfiddle.net/krish7878/y9eusob9/
HTML Code:
<div class="container">
<img src="http://img.netcarshow.com/Lotus-Evora_GX_Racecar_2013_1600x1200_wallpaper_01.jpg" alt="main image" />
</div><!-- /.container -->
CSS Code:
.container{
overflow: hidden;
width: 300px;
height: 200px;
margin-top: 30px;
margin-left: 30px;
border-top-right-radius: 20px;
}
One option could be to add a container <div>, e.g..perspective, that gets the yellow background color and to use a polygon clip-path on the image.
A good tool to create these polygons is Bennett Feely's clippy. Browser support for clip-path isn't quite there yet though. See Clipping and Masking in CSS for more background information and options.
.perspective {
display: inline-block;
background-color: #ff0;
}
.perspective__image {
display: block;
-webkit-clip-path: polygon(10% 20%, 90% 10%, 90% 90%, 10% 80%);
clip-path: polygon(10% 20%, 90% 10%, 90% 90%, 10% 80%);
}
<div class="perspective">
<img src="http://www.nicenicejpg.com/400/300" class="perspective__image"/>
</div>
You might be able to accomplish this effect using a layering technique like what is found here:
http://cssglobe.com/angled-content-mask-with-css/
Basically, the author used a few nested elements that were rotated back and forth with overflow: hidden on the container elements.
For maximum browser support you could use svg's clipPath.
<svg width="400" height="400">
<defs>
<clipPath id="shape">
<path d="M50,50 L400,0 L400,400 L50,350" />
</clipPath>
</defs>
<foreignObject clip-path="url(#shape)" width="400" height="400">
<img src="http://www.lorempixel.com/400/400" />
</foreignObject>
</svg>
I found a great tutorial on css-tricks that talks about doing exactly this:
http://css-tricks.com/almanac/properties/p/perspective/
You'll be making use of the transform property to make the rotations you need to simulate the perspective change.
Hope this helps!
I'm wanting to have a diagonal line drawn from the upper-right corner of a <div> or <span> to the lower-left corner. The problem is that the content will sometimes be longer or shorter. So, something like a static image won't work. Basically I want what's described here (How to create a diagonal line within a table cell?) but for a <div> or a <span>.
This has some interesting ideas: http://jsfiddle.net/zw3Ve/11/
So does this: http://css-tricks.com/forums/discussion/13384/diagonal-line/p1
This is kind of a retry at this post: How to strike through obliquely with css
I can't seem to figure any of this out though. It seems like a pure CSS solution should work here, but I just don't have the skills to make that happen. jQuery is an option for me as well.
You can do this with an inline SVG background, using only CSS and no javascript.
.background {
// Draw an SVG top left to bottom right
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' preserveAspectRatio='none' viewBox='0 0 10 10'> <path d='M0 0 L0 10 L10 10' fill='red' /></svg>");
background-repeat:no-repeat;
background-position:center center;
//scale it
background-size: 100% 100%, auto;
}
See my fiddle:
http://jsfiddle.net/3GWw2/
Is first fiddle as example with image in background instead not good enough?
http://jsfiddle.net/zw3Ve/410/
.line {
display:inline-block;
border: 1px solid #ccc;
margin: 10px;
padding: 10px;
background:url(http://i.piccy.info/i7/c7a432fe0beb98a3a66f5b423b430423/1-5-1789/1066503/lol.png);
background-size:100% 100%;
}
You can do this with linear-gradient. For example if I want a green and white square that cuts diagonally from bottom left to top right, I give it this background attribute:
background: linear-gradient(to bottom right, white, white 50%, green 50%, green);
This means that it starts as white at the top left corner and continues as white all the way to the diagonal line. The transition is immediately from white to green with no actual gradient as both are set at 50%. If you want a gray line between, you could try this:
background: linear-gradient(to bottom right, white, white 48%, gray 48%, gray 52%, green 52%, green);
You might use an SVG image like this one:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
<svg version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="200px" height="50px" viewBox="0 0 200 50">
<line fill="none" stroke="#000" x1="0" y1="0" x2="200" y2="50"/>
</svg>
Set it as background of your span or div
.class-of-div-or-span { background: url("line.svg") no-repeat scroll 0 0 / 100% 100% transparent; }
Note: you have to give your span display:block or display:inline-block in order to work.
You could also inline the svg, use it in an object tag or embed it in your css.
Actually this is more of a question about geometry than coding. With squares this is easy, but with rectangles you'll have to do the math yourself. Remember those kids complaining that they'll never have to calculate a diagonal's length in "real life"? :P
Here's what I did:
div.container /*makes a square container (300x300)*/
{
width: 300px;
height: 150px;
background-color: #aaa;
padding-top: 150px;
}
div.line
{
position: relative;
z-index: 1;
left: -61px; /*this is something I don't understand but apparently is required*/
width: 423px; /*since container is a square this equals to its diagonal: 300*1.41*/
height: 1px;
background-color: #000;
transform: rotate(45deg); /*again, this is easy since its a square. In rectangle you'll have to find a tangent*/
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
HTML:
<div class="container">
<div class="line"></div>
</div>
and a jsfiddle: http://jsfiddle.net/LWAKn/