Triangular border in DIV - html

I've got a div( well it's actually a <th> but I don't think that's relevant) with some dynamically-sized content in it.
I'd like to put a border triangle in the parent div, that fills it (base same width as div, height same height as div).
With a fixed parent div size I can do it as below. Is it possible to do this without a fixed parent div size?
I'm using VueJs if that's any use
.crossedleft {
background:
linear-gradient(to top left,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%)
}
.crossedright {
background:
linear-gradient(to top right,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%)
}
<div style="height:50px;width:100px">
<div class="crossedleft" style="position:absolute; height:50px;width:50px">
<div class="crossedright" style="position:absolute; height:50px;width:50px;left:50px">
</div>
<pre>
content
</pre>
</div>

Apply both backgrounds to the div instead:
.tri {
background:
linear-gradient(to top left,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%) left, /* one on the left */
linear-gradient(to top right,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%) right; /* the other the right */
background-size:50% 100%; /* both width:50% and height:100% */
background-repeat:no-repeat;
}
<div style="width:100px" class="tri">
<pre>
content
</pre>
</div>
You can simplify the code a little like below:
.tri {
--g:rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px);
background:
linear-gradient(to top left, var(--g)),
linear-gradient(to top right, var(--g)) 100%;
background-size:50% 100%;
background-repeat:no-repeat;
}
<div style="width:100px" class="tri">
<pre>
content
</pre>
</div>

First of all, you can use your own approach totally responsively by working with percentages. You can even eliminate all extra elements by using pseudo elements ::before and ::after like this:
.triangle {
position: relative;
display: inline-block;
padding: 1em;
border-bottom: 1px black solid;
}
.triangle::before,
.triangle::after {
content: "";
display: block;
width: 50%;
height: 100%;
position: absolute;
top: 0;
}
.triangle::before {
left: 0;
background: linear-gradient(to top left, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) calc(50% - 0.8px), rgba(0, 0, 0, 1) 50%, rgba(0, 0, 0, 0) calc(50% + 0.8px), rgba(0, 0, 0, 0) 100%)
}
.triangle::after {
right: 0;
background: linear-gradient(to top right, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) calc(50% - 0.8px), rgba(0, 0, 0, 1) 50%, rgba(0, 0, 0, 0) calc(50% + 0.8px), rgba(0, 0, 0, 0) 100%)
}
<div class="triangle">Short</div>
<div class="triangle">This is a very long sentence</div>
However, in this case you might be better off with an SVG background as it can contain any shape you like. With a simple shape like a triangle you can even embed it into your CSS:
.triangle {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.0' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon points='50,1 98,98 1,98' fill='none' stroke='black' stroke-width='1px' vector-effect='non-scaling-stroke' /%3E%3C/svg%3E");
background-repeat: no-repeat;
background-size: 100% 100%;
display: inline-block;
padding: 1em;
}
<div class="triangle">Short</div>
<div class="triangle">This is a very long sentence</div>
The SVG in this example is
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 100 100" preserveAspectRatio="none">
<polygon points="50,1 99,99 1,99" fill="none" stroke="black" stroke-width="1px" vector-effect="non-scaling-stroke" />
</svg>

Related

Move Top cut corner to bottom using linear-gradient

Good morning,
I need assistance with the below attached image:
Cut corner
Using linear gradient how do I move the top cut corner to the bottom corner and make the top corner square/sharp like the other corners. Please see current css below:
body.page {
background: url(../img/Login-bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
font-size: 14px;
align-items: center;
padding-top: 50px;
padding-bottom: 50px;
}
.cut-corner {
position: relative;
color: #4a4a4c;
background-repeat: no-repeat;
background-image: linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431),
linear-gradient(to bottom left, transparent calc(50% - 0.5px), #bcd431 calc(50% - 0.5px), #bcd431 calc(50% + 0.5px), transparent calc(50% + 0.5px)), linear-gradient(transparent, transparent), linear-gradient(transparent, transparent);
background-size: 1.5px 100%, 1.5px 100%, 100% 1.5px, 100% 1.5px, 65px 65px, 100% 100%, 100% 100%;
background-position: 0% 0%, 100% 65px, -65px 0%, 0px 100%, 100% 0%, -65px 0%, 100% 65px;
margin: 25px;
}
.cut-corner:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
right: 0;
top: 0;
z-index: -1;
opacity: 100%;
background-repeat: no-repeat;
background-image: linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431),
linear-gradient(to bottom left, transparent calc(50% - 0.5px), #bcd431 calc(50% - 0.5px), #bcd431 calc(50% + 0.5px), transparent calc(50% + 0.5px)), linear-gradient(transparent, transparent), linear-gradient(transparent, transparent);
background-size: 0.5px 100%, 1.5px 100%, 100% 0.5px, 100% 1.5px, 65px 65px, 100% 100%, 100% 100%;
background-position: 0% 0%, 100% 65px, -65px 0%, 0px 100%, 100% 0%, -65px 0%, 100% 65px;
}
HTML:
<body class="page cut-corner">
...
</body>
Any assistance would be greatly appreciated.
You manage the cut in the background-position part like this
background-position: 0% 0%, 100% -65px, 0px 0%, -65px 100%, 100% 100%, -65px 50%, 100% 65px;
As you can see between each coma, there are two values defined of which the first one determines the position of each border (top border, bottom border...) and the other value defines the length of the border, try playing with the values and you will be able to make the top border full length, the bottom border a bit shorter, move the right border a bit higher and move the diagonal line to the bottom, then just change this from bottom left to bottom right as a way to change the angle of the small diagonal line like this
background-image: linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431),
linear-gradient(to bottom right, transparent calc(50% - 0.5px), #bcd431 calc(50% - 0.5px), #bcd431 calc(50% + 0.5px), transparent calc(50% + 0.5px)), linear-gradient(transparent, transparent), linear-gradient(transparent, transparent);
If you don't care about understanding it and just want to apply the code, here you go the entire code already modified (the only changes made are in cut-corner:after)
.cut-corner:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
right: 0;
top: 0;
z-index: -1;
opacity: 100%;
background-repeat: no-repeat;
background-image: linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431), linear-gradient(#bcd431, #bcd431),
linear-gradient(to bottom right, transparent calc(50% - 0.5px), #bcd431 calc(50% - 0.5px), #bcd431 calc(50% + 0.5px), transparent calc(50% + 0.5px)), linear-gradient(transparent, transparent), linear-gradient(transparent, transparent);
background-size: 1px 100%, 1.5px 100%, 100% 1px, 100% 1.5px, 65px 65px, 100% 100%, 100% 100%;
background-position: 0% 0%, 100% -65px, 0px 0%, -65px 100%, 100% 100%, -65px 50%, 100% 65px;
}
You can try like below, a combination of gradient and clip-path which is easier to handle
.box {
margin:10px;
height:200px;
border: 2px solid red;
background:
linear-gradient(to bottom right,transparent 50%,red 50.5%)
bottom right/50px 50px no-repeat;
clip-path:polygon(0 0,100% 0,100% calc(100% - 50px),calc(100% - 50px) 100%, 0 100%);
}
body {
background:yellow;
}
<div class="box"></div>
With CSS variable to easily adjust the corner:
.box {
--c:50px;
margin:10px;
height:200px;
border: 2px solid red;
background:
linear-gradient(to bottom right,transparent 50%,red 50.5%)
bottom right/var(--c) var(--c) no-repeat;
clip-path:polygon(0 0,100% 0,100% calc(100% - var(--c)),calc(100% - var(--c)) 100%, 0 100%);
}
body {
background:yellow;
}
<div class="box"></div>
<div class="box" style="--c:100px;"></div>

how to slice background color with before/after element

please have a look at my exampleI have added background color, and now i want to slice some point and must be with background transparent like i added
.single_cat_list_in .meta:before {
content: "";
width: 20px;
height: 20px;
background: #ffffff !important;
position: absolute;
right: -35px;
bottom: -10px;
border-radius: 50%;
border-left: 1px solid rgba(0, 0, 0, 0.5);
}
https://prnt.sc/pi94k5
You can use multiple linear and radial gradients to create the rounded corners (taken from this article):
.item {
height: 100px;
background:
linear-gradient(45deg, transparent 10px, gold 0) top left,
linear-gradient(315deg, transparent 10px, gold 0) top right,
linear-gradient(225deg, transparent 10px, white 0) bottom right,
linear-gradient(135deg, transparent 10px, white 0) bottom left;
background-image:
radial-gradient(circle at 0 100%, transparent 14px, gold 15px),
radial-gradient(circle at 100% 100%, transparent 14px, gold 15px),
radial-gradient(circle at 100% 0, transparent 14px, white 15px),
radial-gradient(circle at 0 0, transparent 14px, white 15px);
background-size: 50% 50%;
background-repeat: no-repeat;
}
body {
background: silver;
}
<div class="item"></div>

How can I make the bottom of my div have a peak shape?

I am trying to make my div have this form:
I only get this result with the code I am using:
div {
background: lightblue;
height: 34rem;
-webkit-clip-path: polygon(0 0, 100% 0%, 100% 70%, 0% 100%);
clip-path: polygon(0 0, 100% 0%, 100% 70%, 0% 100%);
}
<div></div>
Thanks for you help guys!
If it's only about coloration you don't need to use clip-path. You can easily achieve this with multiple background and have a better support:
div.box {
height: 300px;
background:
linear-gradient(blue,blue) top/100% 70%,
linear-gradient(to bottom right,blue 49.8%,transparent 50%) bottom right/50% 30%,
linear-gradient(to bottom left, blue 49.8%,transparent 50%) bottom left/50.2% 30%;
background-repeat:no-repeat;
margin-bottom:20px;
}
<div class="box">
</div>
In case you need the black border you can adjust like below:
div.box {
height: 300px;
background:
linear-gradient(blue,blue) top/100% 70%,
linear-gradient(to bottom right,blue calc(50% - 5px),#000 calc(50% - 5px),#000 49.8%,transparent 50%) bottom right -20px/calc(50% + 20px) 30%,
linear-gradient(to bottom left, blue calc(50% - 5px),#000 calc(50% - 5px),#000 49.8%,transparent 50%) bottom left -20px/calc(50% + 21px) 30%;
background-repeat:no-repeat;
margin-bottom:20px;
}
<div class="box">
</div>
div {
background: lightblue;
height: 34rem;
-webkit-clip-path: polygon(100% 0, 100% 75%, 50% 100%, 0% 75%, 0 0);
clip-path: polygon(100% 0, 100% 75%, 50% 100%, 0% 75%, 0 0);
}
<div></div>
This link is helpful https://bennettfeely.com/clippy/

Changing the X to a different color using Linear Gradient

I found this code, but I'm confused about how I would change the color of the X. Can someone show me how to? This is what I'm asking for help with.
And, am I required to use RGBA for this?
https://jsfiddle.net/58tfv09z/
If I am, then there's
HEX to RGBA converter
http://hex2rgba.devoth.com
and
RGB to RGBA converter
http://jsfiddle.net/wb5fwLoc/1/
.crossed {
width: 266px;
height: 266px;
border: 3px solid red;
background:
linear-gradient(to top left,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%),
linear-gradient(to top right,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,0,0,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%);
<div class="crossed"></div>
It uses linear-gradient to display a line by setting other parts invisible, you can change the color by modifying the visible parts with alpha channel = 1:
.crossed {
width: 266px;
height: 266px;
border: 3px solid red;
background:
linear-gradient(to top left,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,255,0,1) 50%, /* put your rgb value here */
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%),
linear-gradient(to top right,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0) calc(50% - 0.8px),
rgba(0,255,255,1) 50%,
rgba(0,0,0,0) calc(50% + 0.8px),
rgba(0,0,0,0) 100%);
}
<div class="crossed"></div>
You can find more details here.

CSS gradient (background and border)

I'm trying to style an <a> tag with a gradient background and a gradient border.
I followed a tutorial online and tweaked it with the right colours, however then realised it needs to set the background-image property in order to give the borders a gradient.
.btn-primary {
background-color: blue;
background-image: linear-gradient(to bottom, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100%);
background-position: 0 0, 100% 0;
background-repeat: no-repeat;
background-size: 10% 100%;
border-bottom: 4px solid #f15a24;
border-top: 4px solid #f7931e;
box-sizing: border-box;
margin: 50px auto;
}
Is there a way I can modify the code so that I can specify a different gradient for the background of the button?
you can use border-image for the border, and just use background-image for the background gradient. Just like this:
.btn-primary {
background-color: blue;
background-image: linear-gradient(to bottom, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100%);
background-position: 0 0, 100% 0;
background-repeat: no-repeat;
background-size: 10% 100%;
border-bottom: 4px solid #f15a24;
border-top: 4px solid #f7931e;
box-sizing: border-box;
margin: 50px auto;
border-image: linear-gradient(to left, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100%
}
You could probably apply your rule to a pseudo-element :before or :after and then position the pseudo-element over your anchor.
how to add a pseudo-element gradient effect
Hope that helps!
Here is a way to create the illusion of a gradient border using multiple background gradients:
.btn-primary {
display:inline-block;
padding:80px;
background: linear-gradient(to bottom, transparent 0, transparent 30px, blue 30px, white calc(100% - 30px), transparent calc(100% - 30px), transparent 100%), linear-gradient(to left, red 0%, yellow 100%);
background-repeat: no-repeat;
background-position: 30px 0, 0 0;
background-size: calc(100% - 60px), auto;
}
<a class="btn-primary">test</a>