Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Border Colors for a section tag using CSS

Tags:

html

css

border

I created a centered slightly rotated section border. Also, I gave each side of the section different color. Here what my basic code looks like:

body{
    background-color: #594451;
    color: #fff;
}

.boxed{
    position: absolute;
    margin:auto;
    /* border: 3pc solid rgba(255, 255, 255 ,1); */
    border-left: solid rgba(127, 203, 170, 1) 3pc;
    border-right: solid rgba(186, 179, 173, 1) 3pc;
    border-bottom: solid rgba(0, 171, 238, 1) 3pc;
    border-top: solid rgba(229, 99, 57, 1) 3pc;
    height: 20pc;
    width: 20pc;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    
}
<h1>Welcome to Rahul's Lab</h1>
    <section class="boxed">
        <!-- <p>This will contain the box properly</p> -->
    </section>

Can I achieve border overlapped on each other? Is it possible by just using CSS or Do I have to create separate section/div for that? HTML & CSS Design and build websites

Reference: Cover picture of HTML & CSS Design and build websites by Jon Duckett.

like image 348
rahulkesharwani Avatar asked May 25 '18 07:05

rahulkesharwani


People also ask

How do you make a two color border in CSS?

If you mean using two colours in the same border. Use e.g. border-right: 1px white solid; border-left: 1px black solid; border-top: 1px black solid; border-bottom: 1px white solid; there are special border-styles for this as well ( ridge , outset and inset ) but they tend to vary across browsers in my experience.

Can you style 4 different colors to a border in CSS?

You can use the border-image property to create a gradient border with 4 colors.


3 Answers

Borders overlaping isn't possible…

… but, here is a solution to create your effect with CSS only, using:

  • Multiple linear-gradients to create the background
  • background-blend-mode to mix the colors in the corners

⋅ ⋅ ⋅

Updated snippet:
Using background shorthand syntax, like Temani did here https://stackoverflow.com/a/50526694/5061000

.boxed {
  position: absolute;
  margin: 5pc auto; /* Modified to better fit in tiny snippet */
  --border-pc: 10%; /* You could even use a CSS variable to store the width if you want to be able to modify it easily. Or the colors. Or both. */
  background:
    /* Shorthand syntax used below: image position/sizeX sizeY */
    linear-gradient(rgba(229, 099, 057, 1), rgba(229, 099, 057, 1)) top   /100% var(--border-pc), /* Orange with CSS var */
    linear-gradient(rgba(000, 171, 238, 1), rgba(000, 171, 238, 1)) bottom/100% var(--border-pc), /* Blue */
    linear-gradient(rgba(127, 203, 170, 1), rgba(127, 203, 170, 1)) left  /var(--border-pc) 100%, /* Green */
    linear-gradient(rgba(186, 179, 173, 1), rgba(186, 179, 173, 1)) right /var(--border-pc) 100%; /* Gray */
  background-blend-mode: multiply;
  background-repeat: no-repeat;
  height: 20pc;
  width: 20pc;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: rotate(45deg);
}
<section class="boxed">
  <!-- <p>This will contain the box properly</p> -->
</section>

⋅ ⋅ ⋅

Old style snippet:
Yep, it's working, but I prefer the first one!

.boxed {
  position: absolute;
  margin: 5pc auto; /* Modified to better fit in tiny snippet */
  --border-pc: 10%; /* You could even use a CSS variable to store the width if you want to be able to modify it easily. Or the colors. Or both. */
  /* I tried to indent it to better see the code: */
  background-image:
    linear-gradient(to top, /* Blue */
      rgba(0, 171, 238, 1) 0%,
      rgba(0, 171, 238, 1) var(--border-pc), /* example use of the CSS var */
      transparent var(--border-pc) ),
    linear-gradient(to right, /* Green */
      rgba(127, 203, 170, 1) 0%,
      rgba(127, 203, 170, 1) 10%,
      transparent 10% ),
    linear-gradient(to bottom, /* Orange */
      rgba(229, 99, 57, 1) 0%,
      rgba(229, 99, 57, 1) 10%, 
      transparent 10% ),
    linear-gradient(to left, /* Gray */
      rgba(186, 179, 173, 1) 0%,
      rgba(186, 179, 173, 1) 10%,
      transparent 10% );
  background-blend-mode: multiply; /* Added to mix the colors */
  height: 20pc;
  width: 20pc;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: rotate(45deg);
}
<section class="boxed">
  <!-- <p>This will contain the box properly</p> -->
</section>

(Note that if you don't use the background-blend-mode you can pick which gradients are overlapping the others by playing with their order!)

Hope it helps.

like image 101
Takit Isy Avatar answered Oct 28 '22 22:10

Takit Isy


It is not really possible to overlap the borders, as they always meet at a certain angle in a div. It's still possible to create your effect by making separate blocks, though. The property mix-blend-mode allows for the overlay effect as provided by your image.

Keep in mind that mix-blend-mode doesn't have any support for IE/Edge.

Here's how it can be done:

body {
  margin: 30px
}

.block1 {
  background: rgba(127, 203, 170, 1);
}

.block2 {
  background: rgba(0, 171, 238, 1);
  transform: rotate(90deg);
  transform-origin: 20px 0;
  mix-blend-mode: color-burn;
}

.block3 {
  background: rgba(186, 179, 173, 1);
  transform: translateY(140px);
  mix-blend-mode: color-burn;
}

.block4 {
  background: rgba(229, 99, 57, 1);
  transform: translateY(30px) translateX(90px) rotate(90deg);
  mix-blend-mode: darken;
}

.block {
  height: 20px;
  width: 200px;
}
<div class="block1 block"></div>
<div class="block2 block"></div>
<div class="block3 block"></div>
<div class="block4 block"></div>
like image 3
Maharkus Avatar answered Oct 28 '22 21:10

Maharkus


Here is another solution with gradient and without the need of mix-blend-mode. The idea is to make each corner using a gradient so you can easily control the color and simulate the overlap:

.box {
  margin:50px;
  height:200px;
  width:200px;
  box-sizing:border-box;
  
  background:
    /*The 4 corners*/
    linear-gradient(#905c3f,#905c3f) top left/20px 20px,
    linear-gradient(#b04d32,#b04d32) top right/20px 20px,
    linear-gradient(#02a9ae,#02a9ae) bottom left/20px 20px,
    linear-gradient(#0085aa,#0085aa) bottom right/20px 20px,
    
    /*The 4 borders*/
    linear-gradient(#e6663a,#e6663a) top/100% 20px,
    linear-gradient(#01adf0,#01adf0) bottom/100% 20px,
    linear-gradient(#7fcaac,#7fcaac) left/20px 100%,
    linear-gradient(#bab1ae,#bab1ae) right/20px 100%;  
   background-repeat:no-repeat;
   
   transform:rotate(45deg);
}
<div class="box">

</div>
like image 2
Temani Afif Avatar answered Oct 28 '22 23:10

Temani Afif