Color border in corners seperatly
Asked Answered
G

3

5

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?

Gilliette answered 27/6, 2014 at 13:23 Comment(3)
Try looking at this previous question. I think it may help you.Trehalose
Not that I know of - could you achieve the same result using border-image?Nostoc
You need to specify how many pixels or show a picture of what it should look like.Hoofbound
C
8

You can color each border corner seperately with 4 pseudo elements and style each corner's border color, width and style seperatly :

DEMO

.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>
Chrysarobin answered 27/6, 2014 at 13:38 Comment(2)
@srekoble you can apply it to all corners if you add an other element inside the divChrysarobin
yes indeed but a clean markup is always better, you took my +1 for using pseudo elements otherwise.Wo
R
4

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>
Roaring answered 22/1, 2015 at 22:4 Comment(4)
Interesting answer. Maybe it would be a bit more straight-forward once border-image is available in all browsers.Duplessis
@Duplessis Yes, of course this will be more straightforward with border-image. However, this approach allows for rounded corners, that for the moment is unavailable in border-image. (Unless you set it in the image itself, but then it's a much harder approach)Roaring
Hmm, interesting. Haven't really tried the border-image option so didn't know about the rounded corner problem.Duplessis
If the background is transparent, how do?Upas
F
0

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.

Fascism answered 21/6, 2021 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.