{"id":22244,"date":"2019-10-24T18:19:37","date_gmt":"2019-10-24T10:19:37","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=22244"},"modified":"2023-07-16T19:59:18","modified_gmt":"2023-07-16T11:59:18","slug":"css-gradient-border","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/","title":{"rendered":"How to Create CSS Gradient Border Colors"},"content":{"rendered":"<p>With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing gradient colors.<\/p>\n<p>Today, it becomes <strong>much leaner with the use of CSS3 Gradient Background<\/strong>. In previous posts, we have shown you how to bring CSS3 Gradient into play as a background color in various forms and directions; <a href=\"https:\/\/www.hongkiat.com\/blog\/css3-linear-gradient\/\">Linear<\/a>, <a href=\"https:\/\/www.hongkiat.com\/blog\/css3-circular-elliptical-gradient\/\">Elliptical<\/a>, and <a href=\"https:\/\/www.hongkiat.com\/blog\/css3-repeating-gradients\/\">Repeating gradients<\/a>.<\/p>\n<p>But CSS3 Gradient does not stop only for background use. Did you know that <strong>you can also utilize it within borders<\/strong>? Read on to learn how to do this.<\/p>\n<h2>First Method<\/h2>\n<p>The first method is by applying CSS3 Gradient within <em>pseudo-elements<\/em>. Well, let\u2019s see how the trick works.<\/p>\n<h3>Top to Bottom Gradient Border<\/h3>\n<p>We will start off with a simple gradient that spreads from top to bottom. To get started, create a box with a <code>div<\/code>, as follows.<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre>\r\n &lt;div class=\"box\"&gt;&lt;\/div&gt;\r\n <\/pre>\n<p><strong>CSS<\/strong><\/p>\n<pre>\r\n .box {\r\n width: 400px;\r\n height: 400px;\r\n background: #eee;\r\n }<\/pre>\n<p>To form the gradient in the box borders, set a solid border on the top and bottom side of the box first. We also create 2 rectangles with 2 <em>pseudo-element<\/em>s \u2013 <code>:before<\/code> and <code>:after<\/code> \u2013 and specify the width in the same size as the box border width.<\/p>\n<p>Position the rectangles on the left and right side of the box and employ <code>linear-gradient<\/code> through <code>background-image<\/code>. You can see how this trick turns out below:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/r544F\/1\/embedded\/result,html,css\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<h3>Left to Right Border Gradient<\/h3>\n<p>Now let\u2019s create a gradient that spans to the left and right in an identical manner to the previous example. Only, this time, we\u2019ll add the box border on left and right side, instead of on top and bottom.<\/p>\n<p>Similarly, we also utilize <em>pseudo-element<\/em> \u2013 <code>:before<\/code> and <code>:after<\/code> \u2013 to shape 2 rectangles. But, contrary to the previous example, we now place them at the top and bottom side of the box.<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/r544F\/2\/embedded\/result,html,css\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<h3>Diagonal Border Gradient<\/h3>\n<p>Creating diagonal gradient with this trick is, well, technically complicated.<\/p>\n<p>Still, we rely on 2 <em>pseudo-elements<\/em>, <code>:before<\/code> and <code>:after<\/code> and use <code>linear-gradient<\/code>. This time, however, we will employ 2 <code>linear-gradient<\/code> within the <em>pseudo-element<\/em>. And each gradient spans in opposition to one another. See the following source code for the details.<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/r544F\/3\/embedded\/result,html,css\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<h2>Second Trick<\/h2>\n<p>The second method is by using CSS3 <code>border-image<\/code> property. The <code>border-image<\/code> property in CSS3 allows us to fill the border with an image as well as CSS3 Gradient.<\/p>\n<p>The browsers support for <code>border-image<\/code> are quite great; Chrome, Internet Explorer 11, Firefox, Safari, and Opera are all capable to fully render <code>border-image<\/code>. It should be noted, however, that the <code>border-image<\/code> will only work on rectangular shapes or boxes.<\/p>\n<p>That means adding <code>border-radius<\/code> will deviate the <code>border-image<\/code> output.<\/p>\n<p>The following is the <code>border-image<\/code> property specification:<\/p>\n<pre>\r\n border-image: &lt;source&gt; &lt;slice&gt; &lt;width&gt; &lt;outset&gt; &lt;repeat|initial|inherit&gt;;\r\n <\/pre>\n<p>The <strong>&lt;source&gt;<\/strong> is the path that specifies the image used in the border. Herein, we will fill it out with CSS3 Gradient instead. To achieve the same output as in the previous examples, we apply the CSS3 Gradient within the <code>border-image<\/code>, as follows.<\/p>\n<pre>\r\n.box {\r\n  width: 250px;\r\n  height: 250px;\r\n  background: #eee;\r\n  border: 20px solid transparent;\r\n  -moz-border-image: -moz-linear-gradient(top, #3acfd5 0%, #3a4ed5 100%);\r\n  -webkit-border-image: -webkit-linear-gradient(top, #3acfd5 0%, #3a4ed5 100%);\r\n  border-image: linear-gradient(to bottom, #3acfd5 0%, #3a4ed5 100%);\r\n  border-image-slice: 1;\r\n}\r\n<\/pre>\n<p>The <code>border-image<\/code> will show nothing if we don\u2019t specify the <code>border<\/code> width. So, as you can see above, we add <code>20px<\/code> of border width with transparent border color. Then, we set the value of the<code>border-image<\/code>and<code>linear-gradient<\/code> along with the vendor prefix for earlier Webkit and Firefox versions.<\/p>\n<p>The addition of <strong>border-image-slice<\/strong> shown above will set the inner offsets of the <code>image-border<\/code> content. This property is required to display the gradient fully within the surrounding area of the box. See the output below:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/796XC\/7\/embedded\/result,html,css\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p>This method offers more flexibility to adjust the gradient in every possible direction; left to right, top to bottom, diagonal, or at an angle. Below are some examples:<\/p>\n<h3>Left to Right Gradient<\/h3>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/796XC\/8\/embedded\/result,html,css\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<h3>Diagonal Gradient<\/h3>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"350\" src=\"https:\/\/jsfiddle.net\/agusesetiyono\/796XC\/9\/embedded\/result,html,css\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>","protected":false},"excerpt":{"rendered":"<p>With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing gradient colors. Today, it becomes much leaner with the use of CSS3 Gradient Background. In previous posts, we have shown you how to bring CSS3 Gradient&hellip;<\/p>\n","protected":false},"author":141,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[507,4501,506,2016],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Create CSS Gradient Border Colors - Hongkiat<\/title>\n<meta name=\"description\" content=\"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create CSS Gradient Border Colors\" \/>\n<meta property=\"og:description\" content=\"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-24T10:19:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-16T11:59:18+00:00\" \/>\n<meta name=\"author\" content=\"Agus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bagusdesain\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Agus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/\"},\"author\":{\"name\":\"Agus\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/b23dad06815dff0bcc222088bed549dd\"},\"headline\":\"How to Create CSS Gradient Border Colors\",\"datePublished\":\"2019-10-24T10:19:37+00:00\",\"dateModified\":\"2023-07-16T11:59:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/\"},\"wordCount\":577,\"commentCount\":12,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"CSS\",\"CSS Tutorials\",\"HTML\",\"HTML5 \\\/ CSS3 Tutorials\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/\",\"name\":\"How to Create CSS Gradient Border Colors - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2019-10-24T10:19:37+00:00\",\"dateModified\":\"2023-07-16T11:59:18+00:00\",\"description\":\"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-gradient-border\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create CSS Gradient Border Colors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/b23dad06815dff0bcc222088bed549dd\",\"name\":\"Agus\",\"description\":\"Agus is a music enthusiast, backpacker and code writer. He has an ambition to build a Skynet on top of HTML and CSS.\",\"sameAs\":[\"https:\\\/\\\/x.com\\\/bagusdesain\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/agus\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Create CSS Gradient Border Colors - Hongkiat","description":"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/","og_locale":"en_US","og_type":"article","og_title":"How to Create CSS Gradient Border Colors","og_description":"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing","og_url":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2019-10-24T10:19:37+00:00","article_modified_time":"2023-07-16T11:59:18+00:00","author":"Agus","twitter_card":"summary_large_image","twitter_creator":"@bagusdesain","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Agus","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/"},"author":{"name":"Agus","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/b23dad06815dff0bcc222088bed549dd"},"headline":"How to Create CSS Gradient Border Colors","datePublished":"2019-10-24T10:19:37+00:00","dateModified":"2023-07-16T11:59:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/"},"wordCount":577,"commentCount":12,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["CSS","CSS Tutorials","HTML","HTML5 \/ CSS3 Tutorials"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/","url":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/","name":"How to Create CSS Gradient Border Colors - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2019-10-24T10:19:37+00:00","dateModified":"2023-07-16T11:59:18+00:00","description":"With all the new features in CSS3, we are now able to build image-less websites. In the past, the use of image was inevitable when it comes to showing","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/css-gradient-border\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create CSS Gradient Border Colors"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/b23dad06815dff0bcc222088bed549dd","name":"Agus","description":"Agus is a music enthusiast, backpacker and code writer. He has an ambition to build a Skynet on top of HTML and CSS.","sameAs":["https:\/\/x.com\/bagusdesain"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/agus\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-5MM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/22244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=22244"}],"version-history":[{"count":4,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/22244\/revisions"}],"predecessor-version":[{"id":68224,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/22244\/revisions\/68224"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=22244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=22244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=22244"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=22244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}