12

I would like to put a gradient border at the top of a div.

So the start color should be #c4268c and ends with #9a0b72

<div class="bordertest"></div>

For easing here is the fiddle :http://jsfiddle.net/aKhjk/

I searched but could not find a suitable way.

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
Prithviraj Mitra
  • 11,002
  • 13
  • 58
  • 99
  • Are you married to just have your current elements, or would you be open to adding elements. I have done what you are trying to do by placing text inside of a

    tag

    – Cam Jan 09 '14 at 18:21
  • Possible duplicate of [CSS3 Gradient Borders](https://stackoverflow.com/questions/2717127/css3-gradient-borders) – Adriana Hernández Mar 01 '18 at 20:31

3 Answers3

12

you could use an image http://border-image.com/ or use a pseudo element over your border :

.bordertest {
    height:300px;
    width:300px;
    border-top:30px solid #c4268c;
    background:#000;
    position:relative;
    margin:1em;
}
.bordertest:first-child:before {
    content:'';
    position:absolute;
    width:100%;
    height:30px;
    background:linear-gradient(to left, #c4268c, #9a0b72);
    top:-30px;
    left:0;
}

http://jsfiddle.net/aKhjk/1/ - jsfiddle.net/aKhjk/3

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
  • 1
    Oh, the link you posted was http://jsfiddle.net/aKhjk/1/ so I didn't read your code. Not sure if they wanted left to right or top to bottom. They didn't really specify did they? I guess our fiddles cover both options. – Code Maverick Jan 09 '14 at 18:34
3

Try this, its also cross browser ready. So not so much work for you to do.

http://jsfiddle.net/cornelas/aKhjk/6/

.bordertest {
    height:300px;
    width:300px;
    border-top:30px solid #c4268c;
    /** Created at http://www.colorzilla.com/gradient-editor/ **/
    background: rgb(196,38,140); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(196,38,140,1) 0%, rgba(154,11,114,1) 5%, rgba(0,0,0,1) 10%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(196,38,140,1)), color-stop(5%,rgba(154,11,114,1)), color-stop(10%,rgba(0,0,0,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(196,38,140,1) 0%,rgba(154,11,114,1) 5%,rgba(0,0,0,1) 10%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c4268c', endColorstr='#000000',GradientType=0 ); /* IE6-9 */

}
Cam
  • 1,884
  • 11
  • 24
2

Now, you can use linear gradients with the border-image attribute in all modern browsers.

.bordertest {
   color: pink;
   border: 10px solid pink;
   border-image: repeating-linear-gradient( 45deg, pink, pink 1%, purple 1%, purple 8%) 10;
}

See: https://css-tricks.com/almanac/properties/b/border-image/

Mehmet Ali Peker
  • 701
  • 1
  • 6
  • 19
  • 1
    It's 2020 and IDK you're still here but i found your answer best in compare to these long answers whichh doesn't make much sense to cause i'm a beginner. Thanks anyway. – Jeet Viramgama Sep 12 '20 at 05:42
  • 3
    Its a pretty useful answer, but sadly doesnt apply "only" on the top-border. – Mbotet Dec 30 '21 at 12:29