【问题标题】:Is it possible to make a gradient border? [duplicate]是否可以制作渐变边框? [复制]
【发布时间】:2011-07-08 03:59:43
【问题描述】:

如标题所述,是否可以在 CSS3 中制作渐变边框,如果可以,如何制作? 我知道你可以制作渐变背景,并且有很多生成器可以用来制作渐变背景,但我还没有找到一个可以为渐变边框创建代码的生成器。

【问题讨论】:

    标签: css border gradient


    【解决方案1】:

    1.

    嗯.. 这不是花哨的 css3,但这是一种可能的解决方案:

    我之前为别的东西做了这个例子,我只是改变了#childWrap的背景网址

    http://jsfiddle.net/qD4zd/1/(注意渐变不是很灵活,因为它是用图像完成的。)

    基本的想法是,如果您想要用渐变、图案或仅图像的边框来框住元素,您应该将该元素包裹在另一个您将用作边框的元素中。


    2.

    更灵活的渐变:您可能想尝试的另一件事是http://www.css3pie.com,并使用外部元素中的渐变背景创建边框,就像我的示例 jsfiddle 中一样。

    http://www.colorzilla.com/gradient-editor/

    (http://jsfiddle.net/qD4zd/2/)


    3.

    第三点。第一种方法可以通过使用实际的<img> 标签变得更灵活,这样你就可以强制图像具有特定的高度和宽度。甚至看起来还不错。

    【讨论】:

    • +1 用于点 1 和 2 的混合,只需在带有填充的包装元素上使用“普通”css3 渐变背景,并在内部元素上放置另一个(非透明)背景跨度>
    • 是的,即使只是没有 css3 pie 的 css3 背景渐变也会比边框渐变有更多的浏览器支持。
    【解决方案2】:

    这里可以使用 CSS3 创建渐变阴影边框:

    -webkit-border-radius: 10px;
       -moz-border-radius: 10px;
        -ms-border-radius: 10px;
            border-radius: 10px;
    
    border: 4px solid rgba(0,0,0,.5);
    -webkit-box-shadow: inset 0 0 20px #000;
       -moz-box-shadow: inset 0 0 20px #000;
        -ms-box-shadow: inset 0 0 20px #000;
            box-shadow: inset 0 0 20px #000;
    

    实际上,这将在边缘创建一个半径为 10 像素的内部阴影边框。

    【讨论】:

      【解决方案3】:

      什么都不用做,只需添加以下代码:

       border-image: linear-gradient(to bottom, black 0%, white 100%);
        /* border-image-slice: 1;*/
      

      只需将上述代码添加到元素中,border-image-slice 属性将设置元素的内部偏移量。

      【讨论】:

      • 为浏览器支持添加所需的扩展。例如 -webkit-border-image
      【解决方案4】:

      具有线性渐变的边框。

      HTML

      <div id="input_parameters">
              ...Some HTML...
      </div>
      

      CSS

      #input_parameters {
      border: 10px solid transparent;
      
      border-image: linear-gradient(#1e2d61 0%,#1f386e 19%,#203c72 20%,#203c73 20%,#266aa8 69%,#2775b5 84%,#2878b9 84%,#2879ba 85%,#297fc0 95%,#2d75ad 100%);
      
      -webkit-border-image: -webkit-linear-gradient(#1e2d61 0%,#1f386e 19%,#203c72 20%,#203c73 20%,#266aa8 69%,#2775b5 84%,#2878b9 84%,#2879ba 85%,#297fc0 95%,#2d75ad 100%);
      
      border-image-slice: 1;
      }
      

      【讨论】:

        【解决方案5】:

        你可以试试这个:

        div {
          width: 170px;
          height: 48px;
          border-radius: 24px;
          border-style: solid;
          border-width: 2px;
          border-image-source: linear-gradient(to bottom, #fff042, #ff5451);
          border-image-slice: 1;
          background-image: linear-gradient(to bottom, #f9e6e6, #c5e0c3), linear-gradient(to bottom, #fff042, #ff5451);
          background-origin: border-box;
          background-clip: content-box, border-box;
          display: flex;
          align-items: center;
          justify-content: center;
          text-transform: uppercase;
        }
        &lt;div&gt;button&lt;/div&gt;

        【讨论】:

          【解决方案6】:

          这是一个可以在 Firefox 下工作的渐变边框示例:

          #gradbor {
              border: 8px solid #000;
              -moz-border-bottom-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
              -moz-border-top-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
              -moz-border-left-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
              -moz-border-right-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
              padding: 5px 5px 5px 15px;
          }
          

          在你的 CSS 中尝试类似的东西让它工作。

          编辑:我不确定它是否可以在其他浏览器上运行。

          【讨论】:

          • 关于你的编辑,我想说它没有,因为-moz-,这意味着参数不被接受为标准。它们是 Mozilla 为 Mozilla 浏览器开发的。也许-webkit-* 和/或-o-* 等将适用于其他浏览器。但怀疑-moz-*
          【解决方案7】:

          可能对您来说是其他工作,但我有非常简单的提示给您,只需将背景图像替换为边框图像即可

          background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.10, #124f7e), color-stop(0.90, #3b89c5) );
          background-image: -moz-linear-gradient(center bottom, #124f7e 10%,#3b89c5 90% );
          background-image: -o-linear-gradient(90deg,rgb(18,79,126),rgb(59,137,197));
          filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#3b89c5', endColorstr='#124f7e'); /* for IE */
          background-color:#124f7e;
          
          border-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.10, #124f7e), color-stop(0.90, #3b89c5) );
          border-image: -moz-linear-gradient(center bottom, #124f7e 10%,#3b89c5 90% );
          border-image: -o-linear-gradient(90deg,rgb(18,79,126),rgb(59,137,197));
          filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#3b89c5', endColorstr='#124f7e'); /* for IE */
          border-color:#124f7e;
          

          【讨论】:

            【解决方案8】:

            我通过设置它的 css 属性来使用 span 元素作为边框。下面是我的代码

            <div id="main_container">
                    <div class="tl"></div>
                    <div class="tr"></div>
                    <div class="bl"></div>
                    <div class="br"></div>
                    <span class="borderH"></span>
                    <span class="borderV"></span>
            </div>
            

            n 这里是我的 CSS -

               #main_container{
                position:relative;
                width:480px;
                height:480px;
                background:#f9f9f9;
                border:1px solid #ff0000;
                left:20%;
                top:100px;
            }
            .tl { position: absolute; top: 0; left: 0; /*background: #ff0000;*/ border-right:1px none #000;width:50%;height:50%;  }
            .tr { position: absolute; top: 0; left: 50%;/*background: blue;*/ border-bottom:1px none #000;width:50%;height:50%;}
            .bl { position: absolute; top: 50%; left: 0; /*background: yellow;*/ border-top:1px none #000;width:50%;height:50%; }
            .br { position: absolute; top: 50%; left: 50%; /*background: #80ff80;*/border-left:1px none #000;width:50%;height:50%; } 
            .borderH{
                position: absolute; left: 0; top: 50%;height:1px;width:100%;
                background: -webkit-linear-gradient(left, #ff0000 , #80ff80); /* For Safari */
                background: -o-linear-gradient(right, #ff0000, #80ff80); /* For Opera 11.1 to 12.0 */
                background: -moz-linear-gradient(right, #ff0000, #80ff80); /* For Firefox 3.6 to 15 */
                background: linear-gradient(to right, #ff0000 , #80ff80); /* Standard syntax (must be last) */
            }
            .borderV{
                position: absolute; top: 0; left: 50%;width:1px;height:100%; 
                background: -webkit-linear-gradient(top, #ff0000 , #80ff80); /* For Safari */
                background: -o-linear-gradient(bottom, #ff0000, #80ff80); /* For Opera 11.1 to 12.0 */
                background: -moz-linear-gradient(bottom, #ff0000, #80ff80); /* For Firefox 3.6 to 15 */
                background: linear-gradient(to bottom, #ff0000 , #80ff80); /* Standard syntax (must be last) */
            }
            

            这段代码将生成像这样的输出https://drive.google.com/file/d/0B2sRswnexZtfUVlTM0t2dWI3cjA/edit?usp=sharing

            【讨论】:

              【解决方案9】:

              这是一个创建彩色渐变边框的解决方案,就像您可以在 Gumroad 或 Vimeo 等网站的顶部看到的那样,例如:

              <div class="u-border-top-rainbow">Lorem ipsum fu fu ma li ma coco go yo.</div>
              
              .u-border-top-rainbow {
                border-style: solid;
                border-width: 30px 0 0 0;
              
                 /* For a gradient repeated 3 times */
                border-image-source: repeating-linear-gradient(to right,
                  hsla(   8,  78%, 63%, 1.00 ) 00.00%,
                  hsla(   8,  78%, 63%, 1.00 ) 03.03%,
                  hsla(   9,  85%, 58%, 1.00 ) 03.03%,
                  hsla(   9,  85%, 58%, 1.00 ) 06.06%,
                  hsla(  12, 100%, 47%, 1.00 ) 06.06%,
                  hsla(  12, 100%, 47%, 1.00 ) 09.09%,
                  hsla( 352,  70%, 47%, 1.00 ) 09.09%,
                  hsla( 352,  70%, 47%, 1.00 ) 12.12%,
                  hsla( 355,  76%, 38%, 1.00 ) 12.12%,
                  hsla( 355,  76%, 38%, 1.00 ) 15.15%,
                  hsla(   2,  78%, 32%, 1.00 ) 15.15%,
                  hsla(   2,  78%, 32%, 1.00 ) 18.18%,
                  hsla( 183, 100%, 30%, 1.00 ) 18.18%,
                  hsla( 183, 100%, 30%, 1.00 ) 21.21%,
                  hsla( 183,  95%, 27%, 1.00 ) 21.21%,
                  hsla( 183,  95%, 27%, 1.00 ) 24.24%,
                  hsla( 183, 100%, 22%, 1.00 ) 24.24%,
                  hsla( 183, 100%, 22%, 1.00 ) 27.27%,
                  hsla(  43,  92%, 54%, 1.00 ) 27.27%,
                  hsla(  43,  92%, 54%, 1.00 ) 30.30%,
                  hsla(  38, 100%, 48%, 1.00 ) 30.30%,
                  hsla(  38, 100%, 48%, 1.00 ) 33.33%
                );
              
                border-image-slice: 1;
              }
              

              Codepen: Colorful CSS gradient border, à la Gumroad or Vimeo

              渐变可以用双停止符号写成一半大小,以这种方式:

              background: linear-gradient(to right, red 20%, orange 20% 40%, yellow 40% 60%, green 60% 80%, blue 80%);
              

              ...不幸的是,Safari 尚不支持此类语法。

              【讨论】:

                【解决方案10】:

                只需使用 ::before

                .card::before{
                           content: '';
                          width: 100%;
                          position: absolute;
                          height: 5px;
                          top:0;
                          left: 0;
                          border-radius:5px 5px 0 0;
                
                          background-color: hsl(195, 100%, 50%);
                    }  
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-07-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-05-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多