Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have different colored left and top borders in CSS with straight join?

Tags:

css

border

I would like to have a border that is 4px thick pink on the left and 1px grey elsewhere:

border: 1px solid #E5E5E5;
border-left: 4px solid #F24495;

The issue is the join is diagonal so I get a horrible overlay. I tried:

.item:before{ 
  border-left: 4px solid #F24495;
}

But no luck.

jsFiddle Example

  • jsFiddle example highlighting diagonal connection

Screenshot

Screenshot

like image 479
Kevin Mann Avatar asked Jun 15 '12 14:06

Kevin Mann


People also ask

How do you make a multi color border in CSS?

To achieve a multi-color border like shown above, we will use the position property and the ::after selector with a color gradient. First, we will make a header area using a HTML <div> class and then we will style it with CSS to have a multi-color border dividing it and the content below.

Can you have 2 borders CSS?

Multiple borders in CSS can be done by box-shadow property. Generally, we can get a single border with border property. Box-shadow property is not for multiple borders, but still, we are creating multiple borders with box-shadow property.

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.

Is it possible to style for different color to a border in HTML?

This property can take from one to four values: One value, like: p {border-color: red} - all four borders will be red. Two values, like: p {border-color: red transparent} - top and bottom border will be red, left and right border will be transparent.


3 Answers

.item::before was the right approach, but it needs a bit of work past a single border-left property. You'll need to make the pseudo element visible (display: block; content: "";), position the pseudo element on the left side of .item, and stretch it to line up with the top and bottom borders properly.

While this can be done manually, I highly recommend using CSS Variables (or variables in your preprocessor) since it makes updating the widths of borders less error-prone and painful.

.item {    display: inline-block;    padding: 0.2em 0.3em;    background: #f8f8f8;    color: #454545;      /* Set border widths with variables */    --top-border-width: 4px;    --bottom-border-width: var(--top-border-width);    --left-border-width: 16px;    --right-border-width: var(--top-border-width);      /* Set border styles for each side */    border-top: var(--top-border-width) solid #e4e4e4;    border-bottom: var(--bottom-border-width) solid #e4e4e4;    border-right: var(--right-border-width) solid #e4e4e4;      /* Remove the left border and add blank space where the border should be placed */    border-left: 0;    margin-left: var(--left-border-width);      /* Contain the ::before */    position: relative;  }    .item::before {    /* Give the pseudo element substance */    display: block;    content: "";      /* Add a left border with a straight edge */    border-left: var(--left-border-width) solid #f84995;      /* Position pseudo element's border where the normal border would have been placed */    position: absolute;    top: calc(0px - var(--top-border-width));    bottom: calc(0px - var(--bottom-border-width));    left: calc(0px - var(--left-border-width));  }
<h1 class="item">Gen.2</h1>
like image 74
0b10011 Avatar answered Oct 25 '22 22:10

0b10011


this should work but requires extra markup:

.outer {     border: 1px solid #E5E5E5;     border-left: 0; }  .inner {     border-left: 4px solid #F24495; } 

and

<div class="outer">     <div class="inner">         ...     </div> </div> 
like image 41
Luca Avatar answered Oct 25 '22 22:10

Luca


If you wish to use the :before pseudo selector you need to set some content as well. See for example this jsfiddle with the following sample code:

<div>Container</div>

CSS:

div {
    border: 10px solid black;
    border-left-width: 0;
}
div::before {
    border: 10px solid orange;
    border-right-width: 0;
    content: '';
}

Displays as:

Screenshot from working code

Edit
Hmm, although this should strictly answer the question, while trying to adapt my solution into the question's fiddle I find this doesn't play very well with paddings. Open to suggestions/edits/other answers that can handle that bit :(...

like image 20
Jeroen Avatar answered Oct 25 '22 22:10

Jeroen