Max Scho
Max Scho

Reputation: 49

Element with sloped borders

I think this is easy for your, but really hard for me.

Can you explain how I can build an element with such a sloped border in the middle? I would greatly appreciate your input.

Example:

enter image description here

Best,

Max

Upvotes: 0

Views: 131

Answers (4)

Rounin
Rounin

Reputation: 29463

There are many ways to achieve your desired effect.

Here is one approach deploying a single ::after pseudo-element, which has a skew() transform applied to it.

One advantage of this approach is that you can add any text content to the first <span> and the background effect will always adapt to the length of that text content.

body {
background-color: rgb(224, 224, 224);
}

div {
display: inline-block;
background-color: rgb(255, 255, 255);
overflow-x: hidden;
border-radius: 9px;
}

div span {
display: inline-block;
position: relative;
height: 72px;
padding: 12px;
line-height: 72px;
font-size: 64px;
}

div span:nth-of-type(1) {
color: rgb(255, 255, 255);
background-color: rgb(95, 197, 78);
text-transform: uppercase;
border-radius: 9px 0 0 9px;
}

div span:nth-of-type(1)::after {
content: '';
position: absolute;
top: 0;
right: -10px;
width: 20px;
height: 100%;
background-color: rgb(95, 197, 78);
transform: skew(-10deg);
}

div span:nth-of-type(2) {
color: rgb(95, 197, 78);
padding-left: 16px;
border-radius: 0 9px 9px 0;
}
<div>
<span>Free</span>
<span>Wifi</span>
</div>

Upvotes: 1

Meer
Meer

Reputation: 2835

try this

div {
    min-height: 100px;
    background: #D25A1E;
    position: relative;
    width: calc(50% - 30px);
}
.div1 {
    float: left;
    background-color:#5DC351;
    width:130px;
}
.div2 {
    float: right;
     background-color:#FFF;
}
.div1:after, .div2:before {
    content:'';
    position: absolute;
    top: 0;
    width: 0;
    height: 0;
}
.div1:after {
    left: 100%;
    border-top: 100px solid #5DC351;
    border-right: 50px solid transparent;
}
.div2:before {
    right: 100%;
    border-bottom: 100px solid #FFF;
    border-left: 50px solid transparent;
}
.inrtxt
{
font-size:50px;
margin:20px;
position:absolute;
}
#parent
{
background-color:#CcC;
width:300px;
padding:15px;
}
<div id="parent">
<div class="div1"><span class="inrtxt" style="color:white">Free</span></div>
<div class="div2"><span class="inrtxt" style="color:#5DC351">Wifi</span></div>
</div>

Upvotes: 2

Nadezhda Serafimova
Nadezhda Serafimova

Reputation: 792

One more option for you is to use the transform: skewX(20deg); css property, but you need to have in mind the browser support.

For example:

.bttn:after, .bttn:before {
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    height: 100%;
    content: '';
    transform: skewX(20deg);
}

Upvotes: 1

saleh katebi
saleh katebi

Reputation: 71

Use following:

<a class="btn-sloped" href="#">
    <span class="sloped-border">free</span>
    <span>wifi</span>
</a>

with these CSS:

.btn-sloped
{
    border-radius: 10px;
    color: green;
    background-color: white;
    display: inline-block;
    font-size: 22px;
    width: 260px;
    line-height: 1;
    height: 60px;
    text-align: center;
}
.btn-sloped > span
{
    display: inline-block;
    position: relative;
    padding: 15px;
    float: left;
    width: 160px;
}
.btn-sloped .sloped-border
{
    background-color: green;
    color: #fff;
    border-radius: 10px 0 0 10px;
    width: 100px;
}
.btn-sloped .sloped-border:before
{
    content: "";
    position: absolute;
    right: -30px;
    top: 0;
    bottom: 0;
    width: 0px;
    height: 0px;
    border-bottom: 52px solid transparent;
    border-left: 30px solid green;
}

Upvotes: 1

Related Questions