简体   繁体   English

在引用的CSS3中创建渐变边框

[英]Create a gradient border in CSS3 as referenced

I am doing a gradient border of a div in css3. 我在css3中做一个div的渐变边框。 So far now I have done my coding like this 到目前为止,我已经完成了这样的编码

in css 在css

.bot-left {
  position: relative;
}
.bot-left:before, .bot-left:after {
  content: "";
  position: absolute;
  bottom: -3px;
  left: -3px;

}
.bot-left:before {
  top: -3px;
  width: 3px;
  background-image: -webkit-gradient(linear, 0 100%, 0 0, from(#000), to(transparent));
  background-image: -webkit-linear-gradient(transparent, #000);
  background-image: -moz-linear-gradient(transparent, #000);
  background-image: -o-linear-gradient(transparent, #000);
}
.bot-left:after {
  right: -3px;
  height: 3px;
  background-image: -webkit-gradient(linear, 0 0, 100% 0, from(#000), to(transparent));
  background-image: -webkit-linear-gradient(left, #000, transparent);
  background-image: -moz-linear-gradient(left, #000, transparent);
  background-image: -o-linear-gradient(left, #000, transparent);
}

in html 在HTML中

  <div class="bot-left" style="width: 200px; height: 200px"></div>

But still I am not getting the exact match as reference. 但我仍然没有得到完全匹配作为参考。 The reference image for the gradient border is attached with this 渐变边框的参考图像附有此

UPDATE I want the background-color should be transparent. 更新我希望背景颜色应该是透明的。

在此输入图像描述

I would recommend you to use the gradients as background instead of border images. 我建议你使用渐变作为背景而不是边框​​图像。 The reason I am suggesting you to use this method is because border-image isn't supported by IE10. 我建议你使用这种方法的原因是因为IE10 不支持 border-image Where as you can implement this method to support IE9 as well, by using base64 encoded gradients. 您可以通过使用base64编码的渐变来实现此方法以支持IE9。

Now, here am using two absolute positioned elements along with :before and :after pseudo elements which are positioned absolute. 现在,这里使用两个绝对定位元素:before:after伪元素,它们位于绝对位置。

Demo 演示

Here, you can refactor this to a great extent, I've not done that so that you can figure out how this works. 在这里,您可以在很大程度上重构这一点,我没有这样做,以便您可以弄清楚它是如何工作的。

Also, if you want, you can wrap this inside a position: relative; 此外,如果你愿意,你可以将它包装在一个position: relative; container with a negative z-index set on the elements having class of .frame1 and 2 respectively. 在具有.frame12类的元素上设置负z-index容器。

Demo 2 演示2

body {
    background: #000;
}

.frame1,
.frame2 {
    position: absolute;
    top: 25px;
    left: 25px;
    bottom: 25px;
    right: 25px;
}

.frame1:before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    background: linear-gradient(to bottom, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    width: 1px;
}

.frame1:after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: linear-gradient(to right, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    height: 1px;
}

.frame2:before {
    content: "";
    position: absolute;
    right: 0;
    bottom: 0;
    height: 100%;
    background: linear-gradient(to top, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    width: 1px;
}

.frame2:after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background: linear-gradient(to left, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    height: 1px;
}

For younger browser , you may use one single gradient, box-shadow and transparent border : DEMO 对于年轻的浏览器,您可以使用一个渐变,盒阴影和透明边框: DEMO

CSS used for demo: 用于演示的CSS:

.bot-left {
  background:
    linear-gradient(
      to bottom right,
      #777,
      #555,
      #333,
      #111,
      #333,
      #555,
      #777) center;
  background-size:105% 105%;/* needs to lay under borders */
  box-sizing:border-box;/* keep borders inside width and height setted */
  border:1px transparent solid;/* background will show through */
  box-shadow:inset 0 0 0 500px black, 0 0 0 5px black;/* inset shadow will hide background gradient */
  margin:5px;/* optionnal: includes ouside box-shadow in space needed by element */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM