<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ruman Saleem</title>
    <description>The latest articles on DEV Community by Ruman Saleem (@rumansaleem).</description>
    <link>https://dev.to/rumansaleem</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F125895%2Fe49f6d45-8682-4b27-8a67-bd4c06a5e003.jpeg</url>
      <title>DEV Community: Ruman Saleem</title>
      <link>https://dev.to/rumansaleem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rumansaleem"/>
    <language>en</language>
    <item>
      <title>Gradient Borders with CSS</title>
      <dc:creator>Ruman Saleem</dc:creator>
      <pubDate>Wed, 25 Dec 2019 06:39:32 +0000</pubDate>
      <link>https://dev.to/rumansaleem/gradient-borders-with-css-3mnk</link>
      <guid>https://dev.to/rumansaleem/gradient-borders-with-css-3mnk</guid>
      <description>&lt;p&gt;You might be familiar with the usage of gradients as &lt;code&gt;background-image&lt;/code&gt; or &lt;code&gt;background&lt;/code&gt; shorthand property in CSS. &lt;br&gt;
But, have you ever tried using gradients as text color or border color? &lt;/p&gt;

&lt;p&gt;If you have, you might also know that CSS gradients don't work with &lt;code&gt;color&lt;/code&gt; property, they can only be used as &lt;code&gt;image&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But, what if you want to use gradients for text color, or use a colorful gradient for borders?&lt;/p&gt;

&lt;p&gt;That gets a little bit tricky with CSS. There are a couple of ways to go about it, having their pros and cons. &lt;/p&gt;

&lt;p&gt;Let's discuss these methods.&lt;/p&gt;
&lt;h3&gt;
  
  
  Method 1: Use &lt;code&gt;border-image&lt;/code&gt; CSS Property
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;border-image-source&lt;/code&gt; and &lt;code&gt;border-image-slice&lt;/code&gt; CSS Property together to apply a gradient on the border. You can also combine those two using &lt;code&gt;border-image&lt;/code&gt; shorthand property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nc"&gt;.border-gradient&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-image-source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nb"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="nl"&gt;border-image-slice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c"&gt;/* Shorthand */&lt;/span&gt;
    &lt;span class="c"&gt;/* border-image: 
        linear-gradient(to right, red 0%, blue 100%) 1;
    */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"border-gradient"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    A div with gradient border using border-image property.
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's understand what's going on here. &lt;/p&gt;

&lt;p&gt;First, the &lt;code&gt;border-image-source&lt;/code&gt; property specifies the image to be used for the border. &lt;/p&gt;

&lt;p&gt;Now, the border-image specified by &lt;code&gt;border-image-source&lt;/code&gt; property is sliced into 9 parts - 4 corners, 4 sides, and 1 middle area. You can specify the slicing behavior by using &lt;code&gt;border-image-slice&lt;/code&gt; property. Middle area is transparent by default. You can use &lt;code&gt;fill&lt;/code&gt; keyword to use the image for middle area as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cDSEfPwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/3814/border-image-slice.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cDSEfPwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/3814/border-image-slice.png" alt="9 parts that border image is sliced into" title="9 parts that border image is sliced into (taken from MDN docs)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;border-image-slice&lt;/code&gt; property specifies how each of these parts should be used to form the actual border. We have specified a single value i.e &lt;code&gt;5&lt;/code&gt;, for all sides (top, right, left and bottom). This means we are slicing equal &lt;code&gt;5px&lt;/code&gt; from each side to form the border.&lt;/p&gt;

&lt;p&gt;For more on &lt;code&gt;border-image-slice&lt;/code&gt; property, see &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-slice"&gt;MDN Docs&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;The middle part can be left transparent or filled using &lt;code&gt;border-image-slice&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can slice each part of border differently (useful when you want some special patterns with borders).&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;We cannot make border-image rounded, even if we give some amount of &lt;code&gt;border-radius&lt;/code&gt;, that's not going to work.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Method 2: Use &lt;code&gt;background-origin&lt;/code&gt; with &lt;code&gt;background-image&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nc"&gt;.border-gradient-rounded&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;7px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
        &lt;span class="n"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nb"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
        &lt;span class="n"&gt;linear-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;15deg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="nl"&gt;background-clip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;padding-box&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;padding-box&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"border-gradient-rounded"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    A container with rounded corners and gradient borders. 
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we understand what's going on here, it's better to first understand &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip"&gt;&lt;code&gt;background-clip&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip"&gt;&lt;code&gt;background-origin&lt;/code&gt;&lt;/a&gt; CSS properties at MDN, if you're not familiar.&lt;/p&gt;

&lt;p&gt;Here, We are using two background images, one is the gradient that we intend to use for borders, and the other one is a solid white color. &lt;/p&gt;

&lt;p&gt;Since specifying solid &lt;em&gt;color&lt;/em&gt; (&lt;code&gt;#ffffff&lt;/code&gt;) would apply &lt;code&gt;background-color&lt;/code&gt; which cannot be clipped and its origin cannot be changed, so we use linear-gradient to generate that solid color as image.&lt;/p&gt;

&lt;p&gt;Next, We are positioning the &lt;code&gt;linear-gradient&lt;/code&gt; background relative to the corner under borders, using &lt;code&gt;background-origin: border-box&lt;/code&gt;. While keeping the other background (which is solid white) starts from the corner of padding-box. &lt;/p&gt;

&lt;p&gt;But if we don't have any &lt;code&gt;border&lt;/code&gt; specified, both padding-box and border-box remains the same. So, we use a transparent border of required width so that we can have our gradient background bleeding out from under the solid white background.&lt;/p&gt;

&lt;p&gt;We are not done yet, still, both the backgrounds overlap each other. To clip these backgrounds to their specific region we use &lt;code&gt;background-clip&lt;/code&gt; property to clip these two backgrounds to their respective regions so that linear gradient is overlayed by the solid background and we have gradient borders.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;You can use rounded borders, works like a charm.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;You cannot have a transparent background. You can mimic the transparent background by using the same color as of background, but this breaks down when your background is not a solid color.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all, folks! &lt;br&gt;
I hope this post helped you learn something. If you have any questions or any corrections, leave them in the comments below.&lt;/p&gt;

&lt;p&gt;Thank You!&lt;/p&gt;

</description>
      <category>css</category>
      <category>border</category>
      <category>gradients</category>
    </item>
  </channel>
</rss>
