I need to create an svg shape, with this kind of look:
I'm trying to implement this with properties stroke-dasharray and stroke-dashoffset, but I can not do it.
Example:
CSS:
@import url(https://fonts.googleapis.com/css?family=Roboto:300,400,500);
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
*:after, *:before {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
height: 100%;
overflow: hidden;
padding: 0;
margin: 0;
background-color: #333;
padding-top: 50px;
}
.row {
width: 700px;
margin: 0 auto;
overflow: hidden;
}
.col {
float: left;
width: 33.33333%;
text-align: center;
}
.button {
position: relative;
height: 50px;
padding: 0 10px;
width: 180px;
font-family: 'Roboto', sans-serif;
text-transform: uppercase;
display: inline-block;
cursor: pointer;
}
.button .shape {
stroke-dasharray: 45, 180px;
stroke-dashoffset: 18px;
}
.button svg {
position: relative;
z-index: 1;
display: block;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
z-index: 0;
}
.button .content {
color: #2980b9;
line-height: 50px;
text-align: center;
z-index: 2;
position: relative;
vertical-align: middle;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-webkit-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
.button:hover .content {
color: #e74c3c;
}
.button:hover .shape {
stroke: #000;
}
.button .shape {
fill: transparent;
stroke: #fff;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
-webkit-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
stroke-width: 6px;
}
HTML:
<div class='row'>
<div class='col'>
<div class='button'>
<svg xmlns='http://www.w3.org/2000/svg'>
<rect class='shape' height='100%' width='100%'></rect>
</svg>
<div class='content'>Hello</div>
</div>
</div>
</div>

It's going to be very hard to achieve if you define your
<rect>with100%forwidthandheight. If you instead use a specific size for the rectangle (eg. 100 x 50) and use an appropriateviewBox, finding an appropriate dash pattern will be a lot easier.Because the SVG has a
viewBox, and we specifypreserveAspectRatio="none", it will stretch to fit the button.How the dasharray is defined
Our rectangle has a width of 100 and a height of 50. And I am going to make the "legs" of the corner pieces 10.
Dash arrays for rectangle elements start at the top left and proceed clockwise. Therefore the dash pattern for the top and bottom sides of the rectangle are:
And for the left and right sides it is
(except vertical of course)
The three corners except the top left one can be joined together, since they flow around the corner. So our dash array becomes:
Giving a final
stroke-dasharray: 10 80 20 30 20 80 20 30 10 0;Demo