Making jagged triangle border in CSS - css

I have a shape with an edge like this in Photoshop:
Is it possible to make the repeated triangles as a border with CSS?

You can use gradients to create a zig-zag patterned background, use the ::after pseud-element to apply it like a border.
.header{
color: white;
background-color: #2B3A48;
text-align: center;
}
.header::after {
content: " ";
display: block;
position: relative;
top: 0px;
left: 0px;
width: 100%;
height: 36px;
background: linear-gradient(#2B3A48 0%, transparent 0%), linear-gradient(135deg, #272220 33.33%, transparent 33.33%) 0 0%, #272220 linear-gradient(45deg, #272220 33.33%, #2B3A48 33.33%) 0 0%;
background-repeat: repeat-x;
background-size: 0px 100%, 9px 27px, 9px 27px;
}
<div class="header"><h1>This is a header</h1></div>
Source: CSS Zigzag Border with a Textured Background
JSFiddle: http://jsfiddle.net/kA4zK/

For future viewers, I found this adaptation of #extramaster's answer to be a little simpler.
It's essentially the same, but it uses one fewer background gradients and allows the backing object (.navbar in my markup) to show through instead of hard-coding the second color into the zig-zag.
JsFiddle: http://jsfiddle.net/861gjx0b/2/
.header {
position: relative;
color: white;
background-color: #2B3A48;
text-align: center;
}
.navbar {
background: #272220;
height: 20px;
}
.header:after {
content: "";
position: absolute;
display: block;
height: 10px;
bottom: -10px;
/* -height */
left: 0;
right: 0;
/* TODO Add browser prefixes */
background: linear-gradient( 45deg, transparent 33.333%, #2B3A48 33.333%, #2B3A48 66.667%, transparent 66.667%), linear-gradient( -45deg, transparent 33.333%, #2B3A48 33.333%, #2B3A48 66.667%, transparent 66.667%);
background-size: 8px 20px;
/* toothSize doubleHeight */
background-position: 0 -10px;
/* horizontalOffset -height */
}
<div class="header">
<h1>This is a header</h1>
</div>
<nav class="navbar"></nav>

Personally, I think clip-path is easier to work with/understand than complex background gradients.
body {
font-family:Roboto,'Open Sans',Helvetica,sans-serif;
}
.container {
background:#ddd;
margin:0 auto;
max-width:800px;
padding:30px;
}
h1:first-child {margin:0;}
.jagged-bottom {
position:relative;
}
.jagged-bottom:after {
background:#ddd;
content:"";
height:2vw;
position:absolute;
top:100%;
left:0;
right:0;
clip-path:polygon(
0 0, 2.5% 100%, 5% 0, 7.5% 100%,
10% 0,12.5% 100%,15% 0, 17.5% 100%,
20% 0,22.5% 100%,25% 0, 27.5% 100%,
30% 0,32.5% 100%,35% 0, 37.5% 100%,
40% 0,42.5% 100%,45% 0, 47.5% 100%,
50% 0,52.5% 100%,55% 0, 57.5% 100%,
60% 0,62.5% 100%,65% 0, 67.5% 100%,
70% 0,72.5% 100%,75% 0, 77.5% 100%,
80% 0,82.5% 100%,85% 0, 87.5% 100%,
90% 0,92.5% 100%,95% 0, 97.5% 100%, 100% 0);
}
<div class="container jagged-bottom">
<h1>Looks Like A Receipt</h1>
<p>Simply adjust the clip path on the pseudo-element if you want more or fewer spikes, and the height if you want them to be taller or shorter.</p>
</div>

There is a border-image property in CSS3.
Maybe you can work it out in a way you want. More here:
https://developer.mozilla.org/en-US/docs/Web/CSS/border-image
Or here
https://www.w3schools.com/cssref/css3_pr_border-image.asp

You can create an individual triangle using CSS quite easily (just tweak border properties). In order for this to work you will need to generate quite a bit of markup yourself. I would recommend against this approach.
Instead you are likely better off using an individual image containing a single triangle (preferably a transparent .png) and then use background-image and background-repeat (repeat-x) properties to bind that to a div (your "border").
Unfortunately there is no yet a straight-forward way to achieve this using pure CSS.

Related

Both horizontal and vertical gradient on background color

I'm trying to add a black rectangle as a bakcground for the red div.
I managed to make the horizontal hard fade:
background: rgb(0,0,0);
background: linear-gradient(90deg, rgba(0,0,0,1) 60%, rgba(208,6,30,1) 60%);
It seems it's either horizontal or vertical. Can I do both?
I'd like to do this with CSS, not add a black image as background.
If you set the background-color to the red you can overlay it with a rectangle of the black which has, say, 60% width and 50% height and is positioned from the bottom.
div {
--r: rgba(208, 6, 30, 1);
--b: rgba(0, 0, 0, 1);
display: inline-block;
width: 50vmin;
height: 30vmin;
background-color: var(--r);
background-image: linear-gradient(var(--b), var(--b));
background-size: 60% 50%;
background-position: left bottom;
background-repeat: no-repeat;
}
<div></div>
Use conic-gradient:
.box {
display: inline-block;
width: 300px;
height: 200px;
background: conic-gradient(from -90deg at 70% 50%, rgb(208,6,30) 75%, black 0);
/* adjust the "at 70% 50%" to control the position */
}
<div class="box"></div>

Outline inside clip-path

I'm looking for a way to do this:
https://imgur.com/j7uMWwj.jpg
I add a clip-path to do the shield shape, but I just don't see how I can add this outline following the shape... Outlines or borders just do not follow shapes.
I tried something like this :
.shield_mask{
position: relative;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
&:before{
position: absolute;
background-color: transparent;
top: 12px; /* equal to border thickness */
left: 12px; /* equal to border thickness */
width: 327px; /* container height - (border thickness * 2) */
height: 317px; /* container height - (border thickness * 2) */
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
-webkit-box-shadow:inset 1px 1px 0 0 #FFF;
box-shadow:inset 1px 1px 0 0 #FFF;
}
}
Not working... I tried with gradients too...
Does anyone have an idea ?
As you noted in your question, borders and outlines do not follow the clip-path outline. This is by design: " A clipping path is conceptually equivalent to a custom viewport for the referencing element. Thus, it affects the rendering of an element, but not the element's inherent geometry. The bounding box of a clipped element (meaning, an element which references a element via a clip-path property, or a child of the referencing element) must remain the same as if it were not clipped." -- developer.mozilla.org/en-US/docs/Web/SVG/Element/clipPath
I don't believe it's possible to accomplish this with CSS alone. But it is possible to do it with CSS and SVG. It's a bit manual and fiddly, but essentially you would need to create an SVG that mimics the inset border you're after and overlay it as a pseudo element on a div with a background image.
Not at all what you were trying to do, but it visually mimics the look you're after.
Hopefully, another contributor will wow us with some CSS wizardry, but until then, if you just really need to get it coded, you can try something like the following...
HTML
<div class="shield_mask" style="background-image: url(http:placehold.it/200x300)" " alt=" ">
</div>
CSS
.shield_mask {
width: 200px;
height: 300px;
display: block;
position: relative;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 50% 100%, 0 90%);
}
.shield_mask::after {
content: "";
height: 100%;
width: 100%;
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-image: url("data:image/svg+xml,%3Csvg width='200' height='300' viewBox='0 0 200 300' fill='none' xmlns='http://www.w3.org/2000/svg'%3E %3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M12 13H188V259.233L103.01 286.045L12 259.248V13ZM14 15V257.752L102.99 283.955L186 257.767V15H14Z' fill='white'/%3E %3C/svg%3E ");
z-index: 1000;
background-repeat: no-repeat;
}
See codepen here: https://codepen.io/anon/pen/KYavGq

Is it possible to generate a box-shadow that follows the shape of a clip-path polygon?

Let's say I have this clip path (a triangle generated here)
-webkit-clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
Is it possible to create a box-shadow from this clip path?
Something like this:
box-shadow: 20px 25px 50px -25px #000;
You can use a filter on the containing div, try:
.container {
filter: drop-shadow(0px 10px 5px rgba(0,0,0,0.1))
}
eg: https://plnkr.co/edit/kePuv7OLQwawPjiBLg3J?p=preview
I'm assuming you mean, is it possible to create the shadow along the polygon. If so, then no. box-shadow is unfortunately only a "box", so it can't follow the clip path. It'd still apply to the rectangle of the element itself.
You could however pair it with another element that has the same clipping, but is set below it and offset and create a pseudo-shadow:
#box {
position: relative;
width: 200px;
height: 200px;
}
#content {
width: 100%;
height: 100%;
background: #3CF;
-webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 80% 100%);
}
#shadow {
position: absolute;
z-index: -1;
content: "";
background: rgba(0, 0, 0, .5);
width: 200px;
height: 200px;
left: 5px;
top: 5px;
-webkit-clip-path: polygon(0 100%, 0 0, 100% 0, 80% 100%);
}
<div id="box">
<div id="content"></div>
<div id="shadow"></div>
</div>
Depending on your use-case, with some clever use of a background image, multiple borders, and/or gradients, you could make the background look between with a fading shadow and what not.
It's not possible, I think. I would suggest you this work around.
.triangle {
font-size:100px;
color:blue;
text-shadow:0 0 10px black;
}
<span class="triangle">▲</span>

Create arrowed background with CSS using repeating-gradient and blend-mode

I thought about creating a background with arrows. Something that looks like in this codepen: http://codepen.io/DaSch/pen/rrWAmy
.top {
height: 5em;
background:
repeating-linear-gradient(
45deg,
lightgray,
lightgray 25%,
gray 0,
gray 50%
);
background-size: 5em 5em;
}
.bottom {
height: 5em;
background:
repeating-linear-gradient(
135deg,
lightgray,
lightgray 25%,
gray 0,
gray 50%
);
background-size: 5em 5em;
}
In the given example there are two elements but together to make it look like I want it to. If I but the gradients together I just get strips. I tried a lot but I can't figure out how to create arrows. with multiple gradients an background-blend-mode.
I'm not sure if this is possible. But I'm looking for a solution without external background-images. If it's not possible a good explanation why would be great.
Here is what I have. It looks like an arrow, and can be repeated using JavaScript. I can't do this with pure CSS. Maybe this solution gives you an idea for your code.
.top {
height: 5em;
width:80px;
margin-left:120px;
background:
repeating-linear-gradient(
45deg,
white,
white 25%,
gray 0,
gray 50%
);
background-size: 5em 5em;
}
.bottom {
height: 5em;
width:80px;
margin-left:120px;
background:
repeating-linear-gradient(
135deg,
white,
white 25%,
gray 0,
gray 50%
);
background-size: 5em 5em;
}
.middle
{
background-color:gray;
height:30px;
width:200px;
margin-right:10px;
}
.maskCornerTop
{
width:40px;
height:40px;
position:relative;
background-color:white;
float:right;
}
.maskCornerBottom
{
width:40px;
height:40px;
background-color:white;
float:right;
margin-top:40px;
position:relative;
}
<div class="top"><div class="maskCornerTop"></div></div>
<div class="middle"></div>
<div class="bottom"><div class="maskCornerBottom"></div></div>
<br/>
<div class="combo"></div>
After some research I found, that the solution is to overlay different backgrounds and use only the half hight for the upper one.
It'll look like this
.combo {
height: 10em;
background:
repeating-linear-gradient(
45deg,
lightgray,
lightgray 33.33%,
gray 33.33%,
gray 66.66%
),
repeating-linear-gradient(
135deg,
gray,
gray 25%,
lightgray 25%,
lightgray 50%
);
background-size: 10em 50%, 10em 100%;
background-repeat: repeat-x, repeat-x;
}
Still this maybe isn't the best solution as it only works if the height of the container is known and fixed.

CSS advanced shape (heart cut out of background)

Basically I want to create a shape in CSS only (so no images) that is the opposite of a heart shape. I don't know how to explain it properly so here is an image:
The blue is the background, as you can see, but the shape that I want to create is not a heart, it is the shape of the black rectangle.
If I would have the following shape (THE GRAY NOT THE BLACK)
I could duplicate it and then rotate it, that would give me the shape I am looking for.
Heart shape cut out using box-shadow
Let's create this — the blue is the background color of <body>
The pieces
Feel free to skip directly to the complete demo at the bottom of this answer :)
1 - The rounded corners
The rounded top left and top right corners are created with box-shadow on the two pseudo elements with border-radius: 50% — .heart:before and .heart:after — They form two crescent shapes that look like this:
2 - The angle
The angled shape is created by the box-shadow on .heart. Combined with the two circles, it looks like this:
3 - The filler
We now need to fill in the gaps. This is done by the pseudo elements of the .box-shape container — .shape-box:before and .shape-box:after. The excess is cut-off neatly with overflow: hidden on the .shape-box. Combined with our pieces above, they look like this:
The Complete Example
Combine it all together and we get this nicely cut out heart shape. It is all contained in .shape-box.
body {
background: #00A2F6;
}
.shape-box {
height: 504px;
width: 504px;
position: relative;
margin: 100px;
overflow: hidden;
}
.shape-box:before,
.shape-box:after {
content: '';
display: block;
height: 100px;
width: 120px;
background: #2B2B2B;
transform: rotate(45deg);
left: 190px;
position: absolute;
top: 40px;
}
.shape-box:after {
width: 760px;
height: 750px;
box-shadow: inset 0 0 0 220px #2B2B2B;
top: -150px;
left: -130px;
background: none;
}
.heart {
transform: rotate(45deg);
height: 357px;
width: 356px;
box-shadow: inset 0 0 0 50px #2B2B2B;
position: absolute;
left: 74px;
top: 34px;
}
.heart:before,
.heart:after {
content: '';
display: block;
width: 151px;
height: 151px;
border-radius: 50%;
box-shadow: -40px -15px 0 20px #2B2B2B;
position: absolute;
left: 50px;
top: 157px;
}
.heart:after {
box-shadow: -15px -40px 0 21px #2B2B2B;
left: 156px;
top: 51px;
}
<div class="shape-box">
<div class="heart"></div>
</div>
This can be done with a combination of svg gradients, multiple backgrounds, and a little creative tiling/placement. Sample CSS from my working jsfiddle (without vendor prefixes, i.e. -webkit and -moz):
height: 400px;
width: 400px;
background-image:
radial-gradient(75% 85.5%, circle, transparent 25%, black 26%),
radial-gradient(25% 85.5%, circle, transparent 25%, black 26%),
linear-gradient(225deg, transparent 25%, black 25%),
linear-gradient(135deg, transparent 25%, black 25%);
background-size: 200px 200px;
background-position: top left, top right, bottom left, bottom right;
background-repeat: no-repeat;
This makes a heart-shaped cutout in the middle of a 400px square element. It can be modified to fit whatever size element you want.
Update: here’s a more complex fiddle that uses six gradients instead of four, but looks a bit nicer.
Based on the work that Mark Hubbart did I was able to push this to a slightly more advanced form in this fiddle
This is not 100% complete yet as it will need some media queries to work across more browsers but it does show the start of a much more flexible working for the same goal.
#backgrounder {
z-index: 2;
background-image:
radial-gradient(68% 100%, circle, transparent 48%, white 30%),
radial-gradient(32% 100%, circle, transparent 48%, white 30%),
radial-gradient(110% 1%, circle, transparent 65%, white 30%),
radial-gradient(-8.5% 1%, circle, transparent 65%, white 30%),
linear-gradient(220deg, transparent 41%, white 30%),
linear-gradient(139deg, transparent 41%, white 30%);
background-image:
-webkit-radial-gradient(68% 100%, circle, transparent 48%, white 30%),
-webkit-radial-gradient(32% 100%, circle, transparent 48%, white 30%),
-webkit-radial-gradient(110% 1%, circle, transparent 65%, white 30%),
-webkit-radial-gradient(-8.5% 1%, circle, transparent 65%, white 30%),
linear-gradient(220deg, transparent 41%, white 30%),
linear-gradient(139deg, transparent 41%, white 30%);
background-size: 51% 31%, 50% 31%, 51% 50%, 50% 50%, 51% 51%, 50% 51%;
background-position: top left, top right, 0% 30%, 100% 30%, bottom left, bottom right;
background-repeat: no-repeat;
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}

Resources