边框(轮廓)渐变边框上

时间:2015-03-07 21:53:56

标签: css border gradient

使用此:

background: -moz-linear-gradient(315deg, transparent 10px, black 10px);

如何在不使用border的情况下围绕它创建边框或轮廓?

2 个答案:

答案 0 :(得分:0)

这不是一个理想的解决方案,但我们可以使用颜色停止并使用::before / ::after伪元素伪造边框,如下所示:

(由于简洁,省略了供应商前缀。)

div {
  width: 150px;
  height: 50px;
  background: linear-gradient(315deg, transparent 10px, red 10px, red 12px, black 12px);
  border-top: 2px solid red;
  border-left: 2px solid red;
  position: relative;
}

div::after, div::before {
  content: "";
  position: absolute;
  background: red;
}

div::before {
  width: 2px;
  top: 0; right: 0; bottom: 14px;
}

div::after {
  height: 2px;
  left: 0; right: 14px; bottom: 0;
}
<div></div>

答案 1 :(得分:0)

你可以使用盒子阴影,插入和/或开头,并根据你的意愿使用

div {
  background:linear-gradient(45deg,yellow,purple,gray,lime,turquoise);
  box-shadow:0 0 0 5px turquoise;
  padding:2em 3em;
  display:inline-block;
  margin:0.5em;
  }
div:nth-child(even) {
  box-shadow:0 0 0 3px gray, 0 0 0 6px tomato,inset 0 0 0 2px ,inset 0 0 0 5px white;}
div:last-of-type {
  box-shadow: 0 0;}
<div></div><div></div><div></div><div></div><div></div>

或继续使用渐变:

div {
  background:
    linear-gradient(to left, black, black)  no-repeat,    
    linear-gradient(to top, black, black)  no-repeat,    
    linear-gradient(to right, black, black) no-repeat,    
    linear-gradient(to bottom, black, black)  no-repeat,
    linear-gradient(to bottom left, gray,transparent,yellow) purple
    ;
   background-position: left bottom  ,right top , left top,left top ; 
  background-size: 100% 2px, 2px 100%,100% 2px, 2px 100%, 100%;
  padding:2em 3em;
  float:left;
  margin:0.5em;
  }
<div></div><div></div><div></div><div></div>