How can I create Triangle shape clip mask using CSS - html

I want to create triangle as shown in image.
Does someone know how to achieve the effect?

Here is a fiddle that should solve your problem. I used :before and :after on a container to place two squares over the container with borders to create the arrows. You can mess with the border colors and widths to get the arrows how you want them (just remember the inside borders have to be the same weight to make an symmetrical triangle).
http://jsfiddle.net/56gur0x4/1/
.hero {
background: url(http://d.pr/i/eqn9+);
height: 200px;
position: relative;
}
.hero:before, .hero:after {
box-sizing: border-box;
content: " ";
position: absolute;
top:0;
display: block;
width: 50%;
height: 100%;
border: 30px solid orange;
border-bottom-color: pink;
}
.hero:before {
left: 0;
border-right: 20px solid transparent;
border-left: 0;
}
.hero:after {
right: 0;
border-left: 20px solid transparent;
border-right: 0;
}

With newer browsers, you can use the clip-path CSS property. This is much less hacky, but you'll need a fallback for IE/Edge and older browsers.
Example
<div class="triangle"></div>
<style>
.triangle {
width: 400px;
height: 400px;
background-color: blue;
clip-path: polygon(50% 0, 0 100%, 100% 100%);
}
</style>

Triangle shape clip mask using CSS to clip image so that background appear.
You can achieve this kind of masking using clip property of CSS With SVG.
HTML
<svg width="0" height="0">
<clipPath id="clipping1" clipPathUnits="objectBoundingBox">
<polygon points="0 0, 0 1, 100 0, .6 0, .5 .2, .4 0">
</clipPath>
</svg>
<img class="clip-animation" alt="" src="http://c1.staticflickr.com/9/8449/7966887330_ddc8018682_h.jpg">
CSS
.clip-animation {clip-path: url(#clipping1);-webkit-clip-path: url(#clipping1); margin:100px; width:500px;}
body{ background-color:#CCCCCC;}
I have mask with an image tag you can also use this with div element or any other tag.
Check a working demo here.
http://jsfiddle.net/VijayDhanvai/495rpzdb/

Imagine the area with photo is split down the middle into two squares, with the photo as a background.
Now imagine you give those squares very thick top and bottom borders with colours corresponding to the areas above and below the photo.
Now imagine you also give them a very thick border for each of their adjacent sides (the left square has a right border, the right square has a left border), but this time, you make the border transparent.
You will see that where the top/bottom borders and the side borders meet, there is a diagonal edge between them where the colour changes to transparent. This leaves a transparent triangle in the adjacent corners where the background shows through.

Related

Is it possible to have a transparent border?

I have this code for my border:
border: 1px solid #CCC;
Is it possible to have some similar CSS where the border has a width but it's not visible. In other words if there's a blue background then that would show right through the border?
A margin occupies space and is transparent. The space a margin occupies is on the outside of the element, so it takes the background color of the parent element. If you want it to be space with the same background color as the element you are modifying, you would want to use padding.
This is a great resource that shows you what you will probably want:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model
Yes, it's entirely possible.
Normally, a transparent border would show the background color of the element beneath it.
This can be overcome by restricting the background to the "box" holding the padding & content using background-clip:padding-box (or optionally content-box which would clip the background to the content area only).
Then you can add a boxshadow (for instance) which will sit outside the transparent border.
body {
background: linear-gradient(to bottom, orange, pink);
}
div {
width: 200px;
height: 200px;
box-sizing: border-box;
background: rebeccapurple;
margin: 2em auto;
border: 10px solid transparent;
box-shadow: 0 0 5px 5px green;
background-clip: padding-box;
}
<div></div>
You can just set the border-color to transparent
Yes it is possible to have transparent border. Just use rgba color defination like so:
border: 10px solid rgba(50,50,50,.5);
The last value 0.5 goes from 0 to 1 and is a opacity (or alpha) value for the color
Working example (see how the border transparency changes on hover): jsFiddle

Mask div using CSS

Ok so say i'm using the follow setup for my divs:
.background will contain an image.
.overlay will have a semitransparent white background for the overlay
.inner would effectively mask out a rectangle the size of the div? So that the background is transparent and cuts through the overlay div.
<div class="background">
<div class="overlay">
<div class="inner">
</div>
</div>
</div>
Is this possible with just css?
Looks like you can achieve that adding a thick border with some opacity (Fiddle). The border widths will determine size of rectangle desired:
<div class="background">
<div class="inner">
</div>
</div>
and CSS:
html, body {
height: 100%;
width: 100%;
}
.background {
width: 100%;
height: 100%;
background: url('http://farm9.staticflickr.com/8242/8558295633_f34a55c1c6_b.jpg');
}
.inner {
width: 100%;
height: 100%;
border-top: 130px solid rgba(255, 255, 255, 0.5);
border-bottom: 130px solid rgba(255, 255, 255, 0.5);
border-left: 100px solid rgba(255, 255, 255, 0.5);
border-right: 100px solid rgba(255, 255, 255, 0.5);
box-sizing: border-box;
}
YES, if you use a PNG image for the masking. It is possible to clip the background div using it's children. What you would need to do it use a PNG with transparent area in the middle or where ever you want.
While you can't mask complex shapes, you can mask simple shapes like a cube or their rounded edges.
Just use:
overflow-x
or
overflow-y
or
overflow
Which according to an inspect of Google Chrome, can be set to:
auto, hidden, inherit, initial, overlay, revert, scroll, unset, or visible
Although, I find that:
inherit, initial, revert, unset and visible
do not provide a mask in most cases.
Finally, if you want to mask on a curve, simply set a border-radius property.
Remember, in a border-radius, you can use:
1 value: A; all corners
2 values: A, B; A=Top-Left & Bottom-Right B=Top-Right & Bottom-Left
3 values: A, B, C; A=Top-Left B=Top-Right & Bottom-Left C=Bottom-Right
4 values: A, B, C, D; A=Top-Left B=Top-Right C=Bottom-Right D=Bottom-Left
-tested in Google Chrome Version 96.0.4664.45 (Official Build) (64-bit) with HTML and CSS on the date of this posting.
Short answer is - no, you could not clip div by it's children.
But you can solve your problem without clipping. As I understand, you just need white border around inner div. You may use border or box-shadow. Also you can create such border with 4 divs on each side

CSS responsive, Equilateral triangle, for firefox

I know this question has been asked in another form very popularly here:
How do CSS triangles work?
and I have extensively read the entire thread, but it does not address what I'm trying to do.
I want to make a cross-browser equilateral triangle clip that is responsive.
I found a lot of css like this that uses pixels:
#triangle-up {
width: 0;
height: 0;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
border-bottom: 100px solid red;
}
But it's not responsive. I'm currently drawing it using polygon below like so:
.tri-Up {
-webkit-clip-path: polygon(50% 0, 0 100%, 100% 100%);
clip-path: polygon(50% 0, 0 100%, 100% 100%);
}
But this is not compatible in Firefox. I've been exploring this question for weeks, but have yet to find a way to clip a equilateral triangle, have it be responsive, and have it work in firefox, chrome, and Safari.
Any thoughts or attempts/success at this would garner much of my appreciation and respect.
Yeah it can be done, I needed that a while ago, and found a solution to this issue right here:
you can use a <div> or some other that you think it can represent a triangle, and a :pseudo selector from it (actually you can use 2x <div> and leave out the :pseudo selector)
the :pseudo selector can be used to represent the triangle itself, like you posted in your question, with border prop.
the parent <div> acts like a mask that either shrinks/grows the :pseudo selector, using a combination of width and padding specified in percetage
as this mask grows, with your container, more of the triangle is revealed and as soon as it shrinks, it covers up the triangle
the border prop set on the :pseudo element acts like a max-width to which the triangle will grow, so you can specify some larger values to it, to the point you think that will the max that it needs to be
Kudos for the author of this solution, and more about this:
One div, a :pseudo element, and a responsive triangle
Two divs and a responsive triangle
Documentation website
Check out the demo here or the snippet bellow:
*,
*:after,
*before {
box-sizing: border-box;
}
h3 {
margin: 10px;
text-align: center;
}
.small-container {
max-width: 10%;
float: left;
}
.medium-container {
max-width: 30%;
float: left;
}
.large-container {
float: left;
max-width: 50%;
}
.fancy-triangle {
width: 50%;
height: 0;
padding-left: 50%;
padding-bottom: 50%;
overflow: hidden;
}
.fancy-triangle:after {
content: "";
display: block;
width: 0;
height: 0;
margin-left: -2000px;
border-left: 2000px solid transparent;
border-right: 2000px solid transparent;
border-bottom: 2000px solid #4679BD;
}
<h3>Now isnt that nice?</h3>
<div class='fancy-triangle'></div>
UPDATE
Ok, since you need to actually mask a image in a sorta responsive triangle, the above method wont cut it.
Instead, you could use a svg and some percentage clip path points like so:
use the svg to draw up a triangle, used to clip the image if the clip-path isnt working properly custom points/shapes
then use the clip-path to draw a triangle with custom percentage points representing a triangle
Alternatively, you could a position absolute on the <img> wrapper, and set the width/height in some percentage values that will be bound to a set parent with a position relative, that will then grow/shrink with it.
Demo here
Resources
UPDATE V3
Instead of using a <img> tag you could alternatively use a <svg> with the src attr of you're image and it should work out pretty nice.
Demo here
Resources
.fancy-triangle-image {
max-width: 1200px;
-webkit-clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
clip-path: url(#triangle);
}
.fancy-triangle-image img{
width: 100%;
}
<div class='fancy-triangle-image'>
<img src='http://insolitebuzz.fr/wp-content/uploads/2014/10/test-all-the-things.jpg'/>
</div>
<svg display="none;">
<defs>
<clipPath id="triangle">
<polygon points="150 0, 300 300, 0 300" />
</clipPath>
</defs>
</svg>
The best bet I can think of is using vw as your unit on the triangle, as this is the only responsive unit that you can use in the border property. See gist here http://sassmeister.com/gist/1b0d70bf4cc35ff05fec
Browser support for vw is pretty good. http://caniuse.com/#search=vw

Overflow & border-radius : PIE not working

I can't get border-radius property work on IE8, even with PIE.
What I am trying to do ?
A progress bar
HOW ?
A parent div with round border (border-radius: 15px) and a grey background. A child div (rectangular) with green background. The child corners should be hidden outside of the parent shape (overflow:hidden).
Problem :
On IE8, the parent shape has round corners but the child div has not (You can get the same result on other browser by removing overflow: hidden)
Why this solution "Overflow:hidden for rounded borders using css3pie in IE8?" does not answer my problem ?
I don't want my child div to have round corners, for instance at 61% (see my JSFiddle), the right side of the green shape is rectangular. (I don't want to add border-radius property on it)
Code & Demo ?
JSFiddle : link
HTML :
<div class="container">
<div class="progress" style="width:61%"></div>
</div>
CSS :
.container {
width: 200px;
height: 30px;
border: solid 1px black;
position: relative;
background: #AAA;
border-radius: 15px;
behavior: url(/Content/PIE.htc);
/* REMOVE TO SEE THE RESULT ON IE8 : */
overflow:hidden;
}
.progress {
height: 100%;
background: #5F5;
}
Does anyone has an idea to deal with it ?

Transparent png button hover without background

I have 40*40px png with transparent background with a 30*30px circle in the middle.
I would like to use that png as a button with a simple hover effect, but i want the hover effect to take place only when the cursor is actually on the circle, not on the transparent background.
Is there a plain HTML+CSS solution for this? I tried to check it here and on other forums, but I didn't find anything.
Yes, you can do this with HTML and CSS. First create a circle element and place it before your image. Then wrap both your image and the circle in a container, like this:
<div class="container">
<div class="circle"></div>
<img src="your-image.jpg" />
</div>
Then, use position: absolute to position the circle on top of the image (align it with the circle that's in the image), and use the + selector to select the next adjacent element when the circle is hovered.
.container {
position: relative;
}
.circle {
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
border-radius: 50%;
background: #222;
}
.circle:hover+img {
border: 5px solid aqua;
}
See DEMO.
Check out this script if you need to activate hover/click only when mouse is within the circle (and not in the square bounding box) http://tympanus.net/codrops/2011/11/22/hover-and-click-trigger-circular-elements/
It’s not possible in CSS only, as all elements are treated as rectangles, even if they are rendered with rounded corners.