hatched
hatched

Reputation: 865

CSS - Rectangle div with cut corners and border color

I'm trying to achieve the shape as shown in this image:
To have 2 rectangle divs with cut corners , and 1 div positioned behind another div.

enter image description here

But the corners seems incorrect and I can't find the way to show the borders of the shapes.

.wrapper {
  display: flex;
  justify-content: center;
}

.connect {
  width: 254px;
  height: 50px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  position: absolute;
  bottom: 0;
  z-index: 5;
}

.connect::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: -2px;
  border-top: 52px solid white;
  border-left: 42px solid transparent;
}

.connect::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -2px;
  border-top: 52px solid white;
  border-right: 42px solid transparent;
}

.connect-behind {
  width: 300px;
  height: 44px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  position: absolute;
  bottom: 0;
}

.connect-behind::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: -2px;
  border-top: 46px solid white;
  border-left: 26px solid transparent;
}

.connect-behind::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -2px;
  border-top: 46px solid white;
  border-right: 26px solid transparent;
}
  <div class="wrapper">
    <div class="connect"></div>
    <div class="connect-behind"></div>
  </div>

I took reference from other threads to use behind and after for the solution but it doesn't seem working correct for my problem. Please help, thanks.

Upvotes: 3

Views: 744

Answers (3)

G-Cyrillus
G-Cyrillus

Reputation: 105883

You could use perspective and transform:

possible example (for infos : with grid instead absolute) :

.wrapper {
  display: grid;
  justify-content: center;
  align-items: end;
  height: 300px;
  perspective: 50px;
}

.connect,
.connect-behind {
  transform: rotatex(50deg);
  background: red;
  margin: 0 auto;
  background: #FF2D5069;
  border-top: 2px solid #FF2175;
  grid-row: 1;
  grid-column: 1;
  transform-origin: bottom center;
}

.connect-behind {
  width: 300px;
  height: 44px;
}

.connect {
  width: 254px;
  height: 50px;
  ;
}
<div class="wrapper">
  <div class="connect"></div>
  <div class="connect-behind"></div>
</div>

to draw a border around the shape, drop-shadow could be usefull

.wrapper {
  display: grid;
  justify-content: center;
  align-items: end;
  height: 300px;
  perspective: 50px;
   filter:
   drop-shadow( 1px  0px 0 )
   drop-shadow(-1px  0px 0 )
   drop-shadow( 0px  1px 0 )
   drop-shadow( 0px -1px 0 );
}

.connect,
.connect-behind {
  transform: rotatex(50deg);
  background: red;
  margin: 0 auto;
  background:white;
  grid-row: 1;
  grid-column: 1;
  transform-origin: bottom center; 
  background:#ffa500;
}

.connect-behind {
  width: 254px;
  height: 50px; 
  border-left:solid 2px;
  border-right:solid 2px;
}

.connect {
  background:#ed1c24;
  width: 300px;
  height: 44px;
  ;
}
<div class="wrapper">
  <div class="connect"></div>
  <div class="connect-behind"></div>
</div>

Upvotes: 6

Temani Afif
Temani Afif

Reputation: 272806

an idea with skew transformation, clip-path and multiple background:

.box {
  --b:3px;   /* border width */
  --t:20px; /* top part width */
  --s:30px; /* side part width */
  margin:10px;
  display:inline-block;
  width:250px;
  height:150px;
  position:relative;
}
.box::before,
.box::after {
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  width:50%;
  border-style:solid;
  border-width:var(--b) 0 0 var(--b);
  background:
    linear-gradient(black 0 0) 0 var(--t)/100% var(--b),
    linear-gradient(black 0 0) var(--s) 0/var(--b) 100%,
    linear-gradient(red 0 0)  left/var(--s) 100%, 
    orange;
  background-repeat:no-repeat;
  transform-origin:bottom right;
  transform:skew(-20deg);
  clip-path:polygon(0 calc(var(--t) + var(--b)), calc(var(--s) + var(--b)) calc(var(--t) + var(--b)),calc(var(--s) + var(--b)) 0,60% 0,100% 100%,0 100%);
}
.box::after {
  transform:scale(-1,1) skew(-20deg);
 }
<div class="box"></div>
<div class="box" style="--b:2px;--t:30px;--s:15px;"></div>

Upvotes: 1

yerme
yerme

Reputation: 855

You can use clip-path for things like this. Works well in a ( I think ) most browsers. Some, like ie11 and older browsers won't render it correctly, though, so you may need a fallback for those cases.

body {
overflow: hidden;
}

.wrapper {
  display: flex;
  justify-content: center;
}

.connect {
  width: 254px;
  height: 80px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid black;
  position: absolute;
  bottom: 0;
  z-index: 5;
  clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
}

.connect-border-left {
    height: 80px;
    width: 2px;
    background: black;
    left: calc(50% - 131px);
    position: absolute;
    bottom: -12px;
    transform: rotate(34deg) translateX(-50%);
    display: inline-block;
}

.connect-border-right {
    height: 80px;
    width: 2px;
    background: black;
    right: calc(50% - 131px);
    position: absolute;
    bottom: -12px;
    transform: rotate(-34deg) translateX(-50%);
    display: inline-block;
}


.connect-behind {
  width: 300px;
  height: 60px;
  background: red;
  background: #FF2D5069;
  border-top: 2px solid black;
  position: absolute;
  bottom: 0;
  clip-path: polygon(14% 0%, 86% 0%, 100% 100%, 0% 100%);
}

.connect-behind-border-right {
    height: 100px;
    width: 2px;
    background: black;
    right: calc(50% - 103px);
    position: absolute;
    bottom: -11px;
    transform: rotate(-32deg) translateX(-50%);
    display: inline-block;
}

.connect-behind-border-left {
    height: 100px;
    width: 2px;
    background: black;
    left: calc(50% - 103px);
    position: absolute;
    bottom: -11px;
    transform: rotate(32deg) translateX(-50%);
    display: inline-block;
}
<div class="wrapper">
    <div class="connect"></div>
    <div class="connect-border-left"></div>
    <div class="connect-border-right"></div>
    <div class="connect-behind"></div>
    <div class="connect-behind-border-left"></div>
    <div class="connect-behind-border-right"></div>
  </div>

Upvotes: 2

Related Questions