html - 仅为顶部和底部边缘创建线性渐变边框?

标签 html css border linear-gradients

使用下面的代码,我只能为元素的底部边缘生成线性渐变 border-image。我怎样才能修改代码以使其顶部也有一个?

div {
    /* gradient shining border */
    border-style: solid;
    border-width: 3px;
    -webkit-border-image: -webkit-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    -moz-border-image: -moz-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    -o-border-image: -o-linear-gradient(
        left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
    border-image: linear-gradient(
        to left,
        rgba(0,0,0,1) 1%,
        rgba(0,255,255,1) 50%,
        rgba(0,0,0,1) 100%
    ) 0 0 100% 0/0 0 3px 0 stretch;
}

当前输出:

enter image description here

最佳答案

您正在使用速记 border-image 属性来设置渐变的大小,并根据提供的值取消上、左、右边框。

100% 设置为顶部边框渐变的宽度,将 3px 设置为其高度将导致仅在顶部和底部应用渐变。

border-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%) 
              100% 0 100% 0/3px 0 3px 0 stretch;

上面几行代码中,100% 0 100% 0/3px 0 3px 0表示每边渐变边框的大小(读作[top] [right ] [底部] [左侧])。最初它是 0 0 100% 0/0 0 3px 0

div {
  /* gradient shining border */
  border-style: solid;
  border-width: 3px;
  border-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%) 
                100% 0 100% 0/3px 0 3px 0 stretch;
  
  /* other demo stuff */
  height: 50px;
  line-height: 50px;
  background-color: #222;
  color: white;
  text-align: center;  
}
<div>Some content</div>


请注意 border-image property still has pretty low browser support如果您需要支持 IE10 及更低版本,则无法使用。取而代之的是,您可以使用 background-image 就像下面的代码片段一样来产生类似的效果。这在 IE10 中也有效(但在 IE9 中仍然无效 - 因为它们根本不支持渐变)。

div {
  /* gradient shining border */
  background-image: linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%), 
                    linear-gradient(to left, rgba(0, 0, 0, 1) 1%, rgba(0, 255, 255, 1) 50%, rgba(0, 0, 0, 1) 100%);
  background-size: 100% 3px;
  background-position: 0% 0%, 0% 100%;
  background-repeat: no-repeat;
  
  /* other demo stuff */
  height: 50px;
  line-height: 50px;
  background-color: #222;
  color: white;
  text-align: center;
}
<div>Some content</div>

关于html - 仅为顶部和底部边缘创建线性渐变边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32329591/

相关文章:

html - 小屏幕的 CSS 定位问题

html - 导航边框

jquery - 如果未使用 jquery 在 html 中登录,则重定向用户

html - 网站与 IE 和 firefox 不兼容

css - 流体布局和位置固定

ios - 创建和删除文本字段上的边框线

wpf - 在 WPF 中,如何在具有焦点的窗口部分周围放置边框?

html - 有没有办法强制浏览器刷新/下载图像?

javascript - 如何删除存储在本地存储中的数组元素

html - 仍然渲染 png 图像背景黑色