Alex
Alex

Reputation: 1073

Element border that will have transparent corners

I need to create a button or an element that will have border with transparent corners like in the example image.

button margins have transparent corners

I didn't find any css for this, and i was thinking to put 4 :after elements that will cover the corners of the element. Do you have any pure css or jquery solution for this ?

Upvotes: 0

Views: 65

Answers (2)

Christina
Christina

Reputation: 34652

I think the selected answer rocks! I did this before I noticed this was answered. It uses :before and :after. It's probably legacy browser compatible -- if the browser supports pseudo elements. It's a single element too.

http://jsbin.com/gopox/1/edit

enter image description here

CSS:

.button {
    position: relative;
    display: inline-block;
    border: 0;
    padding: 8px 15px;
    font-size: 16px;
    box-sizing: border-box;
    text-decoration: none;
    color: #333;
    font-family: sans-serif;
    line-height: 1;
     background:#f7f7f7;
     cursor:pointer;
}
.button:before, .button:after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    border: 1px solid red;
    top: 5px;
    bottom: 5px;
    border-top: 0;
    border-bottom: 0;
}
.button:after {
    left: 5px;
    right: 5px;
    top: 0;
    bottom: 0;
    border: 1px solid red;
    border-left: 0;
    border-right: 0;
}
.button:hover:before,
.button:hover:after {
    border-color: blue;
    color:blue;
}

HTML

  <a href="http://google.com" class="button">My Button</a>

  <button class="button">My Button</button>

Upvotes: 2

G-Cyrillus
G-Cyrillus

Reputation: 106008

Here is an example, as requested, using background-image (wich is a single color linear-gradient)

button {
  border:3px transparent solid;
  background:tomato;
  padding:5px;
  margin:5px;
  display:inline-block;/* optionnal, it should be defaut layout */
  background-image:
    linear-gradient(to left, brown, brown),
    linear-gradient(to left, brown, brown),
    linear-gradient(to left, brown, brown),
    linear-gradient(to left, brown, brown);
  background-size:
    80% 2px,
    80% 2px,
    2px 80%,
    2px 80%;
  background-repeat:no-repeat;
  background-position:
    top, 
    bottom, 
    left, 
    right;
}

Upvotes: 4

Related Questions