Is there any way to color a border corner in CSS?
I.e. : border-top-left-corner-color: red
If it can't be done in pure CSS, can it be done with some JS/jQuery trickery?
Is there any way to color a border corner in CSS?
I.e. : border-top-left-corner-color: red
If it can't be done in pure CSS, can it be done with some JS/jQuery trickery?
You can color each border corner seperately with 4 pseudo elements and style each corner's border color, width and style seperatly :
.outer{
width:500px;
height:500px;
background:gold;
position:relative;
}
div:before, div:after{
content:'';
position:absolute;
height:10%;
width:10%;
}
.outer:after{
right:0;
border-right: 3px solid red;
border-top: 3px solid red;
}
.outer:before{
border-left: 3px solid green;
border-top: 3px solid green;
}
.inner:before{
bottom:0;
border-left: 3px solid black;
border-bottom: 3px solid black;
}
.inner:after{
bottom:0; right:0;
border-right: 3px solid blue;
border-bottom: 3px solid blue;
}
<div class="outer">
<div class="inner"></div>
</div>
div
–
Chrysarobin A little bit late ..
Here is my solution
.test {
width: 200px;
height: 100px;
border: 10px solid transparent;
background-image: linear-gradient(0deg, white 0%, white 100%),
linear-gradient(0deg, green 0%, green 100%),
linear-gradient(0deg, red 0%, red 100%),
linear-gradient(0deg, yellow 0%, yellow 100%),
linear-gradient(0deg, blue 0%, blue 100%);
background-origin: padding-box, border-box, border-box, border-box, border-box;
background-repeat: no-repeat;
background-size: 100% 100%, 50% 50%, 50% 50%, 50% 50%, 50% 50%;
background-position: top left, top left, top right, bottom left, bottom right;
}
<div class="test"></div>
The colors are achieved as 4 backgrounds set to border-box. They are then masked by a white background set to padding-box.
Notice that the thickness of the border is still set with the border property. (but the border itself is transparent)
An alternate approach, using a pseudo element to mask the inner part
.test {
width: 200px;
height: 100px;
border: 10px solid transparent;
background-image: linear-gradient(0deg, green 0%, green 100%),
linear-gradient(0deg, red 0%, red 100%),
linear-gradient(0deg, yellow 0%, yellow 100%),
linear-gradient(0deg, blue 0%, blue 100%);
background-origin: border-box, border-box, border-box, border-box;
background-repeat: no-repeat;
background-size: 50% 50%, 50% 50%, 50% 50%, 50% 50%;
background-position: top left, top right, bottom left, bottom right;
border-radius: 40px;
position: relative;
}
.test:after {
content: "";
position: absolute;
top: 0px;
right: 0px;
left: 0px;
bottom: 0px;
background-color: white;
border-radius: 30px;
}
<div class="test"></div>
border-image
is available in all browsers. –
Duplessis border-image
option so didn't know about the rounded corner problem. –
Duplessis I had to do something with a similar wanted result (I just wanted the square corners to be colored).
So I made this:
div#jazzyDIV {
--borderWidth: 5px;
background-repeat: no-repeat;
background-origin: padding-box, border-box, border-box, border-box, border-box;
background-image: linear-gradient(0deg, transparent 0%, transparent 100%), linear-gradient(0deg, green 0%, green 100%), linear-gradient(0deg, red 0%, red 100%), linear-gradient(0deg, yellow 0%, yellow 100%), linear-gradient(0deg, blue 0%, blue 100%);
width: 40px;
height: 40px;
border-width: var(--borderWidth);
border-style: solid;
border-color: transparent;
background-size: cover, var(--borderWidth) var(--borderWidth), var(--borderWidth) var(--borderWidth), var(--borderWidth) var(--borderWidth), var(--borderWidth) var(--borderWidth);
background-position: center,top left, top right, bottom right, bottom left;
}
<div id="jazzyDIV"></div>
if you want to expand the colored corner you can use "calc(...)" on each pair (width and height) of values of "background-size" (skip the first single one because it's for the background inside)
if you want an image on the background you can replace the first parameter of background-image with "url('your_image.png')". it will be rendered in the padding-box
another twist for this:
div#jazzyDIV {
--borderWidth: 5px;
background-repeat: no-repeat;
background-origin: padding-box, border-box, border-box, border-box, border-box, border-box, border-box, border-box, border-box;
background-image: linear-gradient(0deg, transparent 0%, transparent 100%), linear-gradient(0deg, green 0%, green 100%), linear-gradient(0deg, red 0%, red 100%), linear-gradient(0deg, yellow 0%, yellow 100%), linear-gradient(0deg, blue 0%, blue 100%), linear-gradient(0deg, orange 0%, orange 100%), linear-gradient(0deg, pink 0%, pink 100%), linear-gradient(0deg, magenta 0%, magenta 100%), linear-gradient(0deg, cyan 0%, cyan 100%);
width: 90px;
height: 40px;
border-width: var(--borderWidth);
border-style: solid;
border-color: transparent;
background-size: cover, var(
--borderWidth) var(--borderWidth), calc(100% - var(--borderWidth) * 2) var(--borderWidth), var(--borderWidth) var(--borderWidth), var(--borderWidth) calc(100% - var(--borderWidth) * 2), var(--borderWidth) var(--borderWidth), calc(100% - var(--borderWidth) * 2) var(--borderWidth), var(--borderWidth) var(--borderWidth), var(--borderWidth) calc(100% - var(--borderWidth) * 2);
background-position: center,top left, top, top right, right, bottom right, bottom, bottom left, left;
}
<div id="jazzyDIV"></div>
both are based on the solution that @vals gave.
© 2022 - 2024 — McMap. All rights reserved.