9

I am trying to create angled tabs to sit above a content section, and came across this great example:

HTML:

<div class="tab">
    <div class="arrow"></div>
</div>

CSS:

body
{
    background-color: #666;    
}
.tab
{
    height: 50px;
    width: 150px;
    border-radius: 10px 10px 0px 0px;
    background-color: #FFF;
    position: relative;
}

.arrow
{
  border-color: transparent transparent #FFF #FFF;
  border-style: solid;
  border-width: 23px 23px 23px 23px;
  height:0;
  width:0;
  position:absolute;
  bottom:0px;
  right:-43px;
}

http://jsfiddle.net/P3P3Z/2/

However, I would like to set a different 2px border colour to this shape, and unfortunately this method doesn't work as it uses the border to create the right hand side of the shape.

Any ideas on how I could mod it?

Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
alias51
  • 8,178
  • 22
  • 94
  • 166

1 Answers1

16

You can try this approach: jsFiddle

Instead of using the borders to create the slanted effect, I'm using an :after pseudo element to create it. This allows me to set borders around it. Then I'm using a :before pseudo element to hide the borders which I don't want to see. The recurring 2px in the CSS is derived from the border width value.

CSS

.tab:before {
    height: 50px;
    width: 10px;
    display: block;
    content:" ";
    background-color: #FFF;
    position: absolute;
    right: -2px;
    top: -2px;
    border-top: 2px solid blue;
    border-bottom: 2px solid blue;
}
.tab {
    height: 50px;
    width: 150px;
    border-radius: 10px 10px 0px 0px;
    background-color: #FFF;
    position: relative;
    border: 2px solid blue;
}
.tab:after {
    display: block;
    content:" ";
    width: 100px;
    height: 50px;
    top: -2px;
    background-color: #FFF;
    position: absolute;
    right: -29px;
    transform:skewX(45deg);
    -ms-transform:skewX(45deg);
    -webkit-transform:skewX(45deg);
    border: 2px solid blue;
    z-index: -1;
}
Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
  • +1. This is similar to my technique used here: http://stackoverflow.com/a/13273672/1654265 where i use after and before as two triangles overlapped and a bit shifted one from each other... – Andrea Ligios May 29 '13 at 23:07
  • Thanks, I've modified your approach for some advanced styling, here: http://jsfiddle.net/robmc/ZvEyx/4/ I want to try and achieve the styled top right curve corner on the tab; I dont think you need the tab:before for this, but I cant see a way to get rid of the blue curve on the parent. Any ideas? – alias51 May 29 '13 at 23:50
  • Just turn off the `border-radius` on the top right corner of the tab and move the `:after` pseudo element a bit more to the right (increase its negative right margin): http://jsfiddle.net/ZvEyx/6/ – Mathijs Flietstra May 30 '13 at 00:11
  • Improved on this a bit: https://stackoverflow.com/a/74242597/165673 – Yarin Oct 29 '22 at 03:25