<?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: Frida Nyvall</title>
    <description>The latest articles on DEV Community by Frida Nyvall (@fridanyvall).</description>
    <link>https://dev.to/fridanyvall</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%2F125696%2Fd91cc6bf-fbea-4971-930d-80986ebf372e.png</url>
      <title>DEV Community: Frida Nyvall</title>
      <link>https://dev.to/fridanyvall</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fridanyvall"/>
    <language>en</language>
    <item>
      <title>CSS Special Effects Summary</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 28 Aug 2020 08:45:16 +0000</pubDate>
      <link>https://dev.to/fridanyvall/css-special-effects-summary-25f2</link>
      <guid>https://dev.to/fridanyvall/css-special-effects-summary-25f2</guid>
      <description>&lt;p&gt;Blend-modes, filters and backdrop-filters can be used in CSS to create special effects similar to effects in image editing software programs.&lt;/p&gt;

&lt;p&gt;The pros of using CSS to create these effects are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ease of use for website administrators, who does not need to know how to create or edit special images.&lt;/li&gt;
&lt;li&gt;Since less specialty knowledge is needed, more junior members of staff can edit and update the website. This can help the company use its personnel more effectively and ultimately save money.&lt;/li&gt;
&lt;li&gt;Improved website performance since many of the effects previously was only obtainable through images. When created with CSS instead, the website page load will decrease.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use Which Effect
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Blend-modes
&lt;/h4&gt;

&lt;p&gt;Use blend-modes to create effects by blending multiple elements or backgrounds. &lt;code&gt;background-blend-mode&lt;/code&gt; blends background images, gradients, and colors. &lt;code&gt;mix-blend-mode&lt;/code&gt; is for blending different elements with their surroundings and can also be used with pseudo-elements.&lt;/p&gt;

&lt;p&gt;See the article &lt;a href="https://dev.to/fridanyvall/blend-modes-in-css-268"&gt;“Blend Modes in CSS”&lt;/a&gt; for more details about background-blend-mode and mix-blend-mode.&lt;/p&gt;

&lt;h4&gt;
  
  
  Filters
&lt;/h4&gt;

&lt;p&gt;Use filters to add effects to a single element. &lt;code&gt;backdrop-filter&lt;/code&gt; is used to apply the filter to an elements background, while &lt;code&gt;filter&lt;/code&gt; applies the effect to the whole element.&lt;/p&gt;

&lt;p&gt;It is possible to define your own filters in SVG for even more custom effects. Just keep an eye out for &lt;a href="https://bugs.chromium.org/p/chromium/issues/detail?id=109212"&gt;a bug&lt;/a&gt; that (at the time of writing) might prevent linking to external SVG file filters in Chrome.&lt;/p&gt;

&lt;p&gt;See our blogposts &lt;a href="https://dev.to/fridanyvall/css-backdrop-filter-2coo"&gt;“CSS backdrop-filter”&lt;/a&gt; and &lt;a href="https://dev.to/fridanyvall/css-filters-25b8"&gt;“CSS Filter”&lt;/a&gt; for more details.&lt;/p&gt;

</description>
      <category>css</category>
      <category>cssfilter</category>
      <category>filter</category>
      <category>cssblendmode</category>
    </item>
    <item>
      <title>CSS Filter</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 28 Aug 2020 08:42:02 +0000</pubDate>
      <link>https://dev.to/fridanyvall/css-filters-25b8</link>
      <guid>https://dev.to/fridanyvall/css-filters-25b8</guid>
      <description>&lt;p&gt;Use filters to add effects to single elements. Filters share the same syntax and options as backdrop-filter.&lt;/p&gt;

&lt;p&gt;Just as with backdrop-filter, it is possible to use more than one filter on an element by adding additional filters:&lt;br&gt;
&lt;code&gt;filter: blur(5px) hue-rotate(90deg);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fredonion.se%2Fwp-content%2Fuploads%2F2020%2F08%2Finfluencer-instagram-filters2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fredonion.se%2Fwp-content%2Fuploads%2F2020%2F08%2Finfluencer-instagram-filters2.gif" alt="Examples of different filter effects applied to a photo."&gt;&lt;/a&gt;Examples of different filter effects applied to a photo.&lt;/p&gt;



&lt;h3&gt;
  
  
  CSS Filter Values
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blur:&lt;/strong&gt; applies a Gaussian blur and works similar to blurring effects in graphical software programs. Takes a length value. Increasing the value increases the blurring effect. Example: &lt;code&gt;filter: blur(5px);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Brightness:&lt;/strong&gt; Takes a number or percentage value. &lt;code&gt;filter: brightness(100%)&lt;/code&gt; or &lt;code&gt;brightness(1)&lt;/code&gt; is the normal brightness. Values below 100% or 1 will make the element darker, and values above 100% or 1 will make the element brighter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contrast:&lt;/strong&gt; takes a number or percentage value, where &lt;code&gt;filter: contrast(100%)&lt;/code&gt; or &lt;code&gt;contrast(1)&lt;/code&gt; is the starting point resulting in an unaltered element. Increasing the value means increasing the contrast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drop-shadow:&lt;/strong&gt; takes 3 length values controlling offset-x, offset-y, and blur-radius (in that order), and a color at the end. Example: &lt;code&gt;filter: drop-shadow(2px 3px 4px black);&lt;/code&gt; Compared to &lt;code&gt;box-shadow&lt;/code&gt;, &lt;code&gt;drop-shadow&lt;/code&gt; can provide hardware acceleration for some browsers, resulting in slightly better performance. The most important benefit however, is that &lt;code&gt;drop-shadow&lt;/code&gt; has the ability to restrict the shadow to follow the edges of the visible element rather than the edges of its bounding box. The effect can be applied to for example SVGs, images, linear backgrounds as well as clipped or masked elements. See &lt;a href="https://css-irl.info/drop-shadow-the-underrated-css-filter/" rel="noopener noreferrer"&gt;“Drop-shadow the underrated CSS filter”&lt;/a&gt; by M. Barker for examples.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grayscale:&lt;/strong&gt; takes a number or percentage value. Any number above 100% or 1 leads to a complete black-and-white result. 0 or 0% means no black-and-white filtering is applied, and anywhere from 0 or 0% to 1 or 100% means applying a partial black-and-white filter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hue-rotate:&lt;/strong&gt; takes an angle that represents how much the hue should change. Example: &lt;code&gt;backdrop-filter: hue-rotate(45deg);&lt;/code&gt; Degree values range from 0 to 360, where 0 is red, 120 is green and 240 is blue. Negative values or values above 360 are also accepted, although since the values represent degrees around the color circle those numbers will have equivalents within the range 0 to 360.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invert:&lt;/strong&gt; takes a number or percentage. 0 or 0% means no effect is applied. 1 and 100% means a totally inverted effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Opacity:&lt;/strong&gt; takes a number or percentage between 0 to 1 and 0 to 100%. When using opacity with filter instead of the usual opacity property, some browsers can benefit from hardware acceleration for better performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sepia:&lt;/strong&gt; takes a number or percentage between 0 to 1 and 0 to 100%, where a lower number indicates a fainter sepia-tone level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saturate:&lt;/strong&gt; takes a number or percentage, where a lower number means lower saturation and a higher number increases saturation. 1 or 100% is the starting point resulting in an unchanged element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;None, inherit, initial, unset:&lt;/strong&gt; There are also the values &lt;code&gt;none&lt;/code&gt;, &lt;code&gt;inherit&lt;/code&gt;, &lt;code&gt;initial&lt;/code&gt;, and &lt;code&gt;unset&lt;/code&gt; to control inheritance, reset or remove any existing setting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SVG filters:&lt;/strong&gt; use your own SVG filter for a custom effect. Example: &lt;code&gt;filter: url(filters.svg#filter);&lt;/code&gt; There is &lt;a href="https://bugs.chromium.org/p/chromium/issues/detail?id=109212" rel="noopener noreferrer"&gt;a bug&lt;/a&gt; that prevents linking to external SVG file filters in Chrome. Even though linking to external SVG file filters might work at some point in the future, the recommendation is to link to filters in the same HTML document using inline styles. Another alternative to avoid using inline styles in the HTML document is to just reference the SVG filter ID (which still has to be placed in the HTML file) in the CSS file: &lt;code&gt;filter: url(#duotone);&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  Browser Compatibility
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://caniuse.com/#feat=css-filters" rel="noopener noreferrer"&gt;Filter effects&lt;/a&gt; are (at the time of writing) supported in 96% of all browsers globally. It is not supported in Internet Explorer or Opera Mini.&lt;/p&gt;

&lt;p&gt;Using &lt;a href="https://caniuse.com/#feat=svg-filters" rel="noopener noreferrer"&gt;filter effects on SVG objects&lt;/a&gt; has a global compatibility percentage of 98%, including support in Internet Explorer 10 and 11.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filter Online Tools
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.cssfilters.co/" rel="noopener noreferrer"&gt;CSSfilters.co&lt;/a&gt;, is a website where users can upload photos and recreate Instagram filters using presets from &lt;a href="https://una.im/CSSgram/" rel="noopener noreferrer"&gt;CSSGram&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://andresgalante.com/RGBAtoFeColorMatrix/" rel="noopener noreferrer"&gt;RGBA to feColorMatrix&lt;/a&gt; is a website where users can create their own SVG filters based on RGBA colors.&lt;/p&gt;

</description>
      <category>css</category>
      <category>cssfilter</category>
      <category>filter</category>
    </item>
    <item>
      <title>CSS backdrop-filter</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 14 Aug 2020 09:42:45 +0000</pubDate>
      <link>https://dev.to/fridanyvall/css-backdrop-filter-2coo</link>
      <guid>https://dev.to/fridanyvall/css-backdrop-filter-2coo</guid>
      <description>&lt;p&gt;Backdrop-filter is as the name implies used for adding a filter on an element’s backdrop. It is similar to and shares the options of the CSS property filter.&lt;/p&gt;

&lt;p&gt;Backdrop-filter is mostly known for making it possible (finally) to create a frosted glass effect just using CSS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example of Frosted Glass Effect:
&lt;/h3&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/PoNPJbB?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Apart from the popular blur setting, there are several other effects. More than one effect can be used simultaneously by adding each effect after one another:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.classname&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;backdrop-filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brightness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;150%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;saturate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;150%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;blur&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; To see the effects, there have to be some transparent (or partially transparent) parts of the element. No effects will be visible if the element for example has a solid background-color or background-image.&lt;/p&gt;

&lt;h3&gt;
  
  
  Backdrop Filter Values
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blur:&lt;/strong&gt; takes a length value, much like blurring effects in graphical software programs. Example: &lt;code&gt;backgroup-filter: blur(2px);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Brightness:&lt;/strong&gt;  takes a number or percentage value. &lt;code&gt;backdrop-filter: brightness(100%)&lt;/code&gt; or &lt;code&gt;brightness(1)&lt;/code&gt; is the normal brightness. Anything below 100% or 1 will make the area darker, and any number above 100% or 1 will make the area brighter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contrast:&lt;/strong&gt; takes a number or percentage value. &lt;code&gt;backdrop-filter: contrast(100%)&lt;/code&gt; or &lt;code&gt;contrast(1)&lt;/code&gt; is the normal contrast. Anything below 100% or 1 will lower the contrast, and any number above 100% or 1 will increase the contrast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drop-shadow:&lt;/strong&gt; takes 3 length values controlling offset-x, offset-y and blur-radius (in that order), and a color at the end. Example: &lt;code&gt;backdrop-filter: drop-shadow(4px 4px 10px blue);&lt;/code&gt; However, since it will only show outside of the parent container, the result of this setting might be difficult to predict. An example by A.Montoro can be viewed on &lt;a href="https://codepen.io/alvaromontoro/pen/XWrKEqW?editors=1100"&gt;Codepen&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;
      &lt;div class="ltag__twitter-tweet__media"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hom8Hbep--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/media/EZ_zVZwXkAEzrXI.jpg" alt="unknown tweet media content"&gt;
      &lt;/div&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--zvoeHIFq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/943190142210191360/dP2tm8WE_normal.jpg" alt="Ana Tudor 🐯 profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Ana Tudor 🐯
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @anatudor
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Dumb CSS question: what does `backdrop-filter: drop-shadow()` even do?&lt;br&gt;&lt;br&gt;Because it really doesn't seem like it does anything at all&lt;br&gt; ¯\_(ツ)_/¯ &lt;br&gt;&lt;br&gt;&lt;a href="https://t.co/7TBHmwPcEE"&gt;codepen.io/thebabydino/pe…&lt;/a&gt; 
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      14:34 PM - 08 Jun 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1270001307584270336" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1270001307584270336" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      0
      &lt;a href="https://twitter.com/intent/like?tweet_id=1270001307584270336" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      15
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grayscale:&lt;/strong&gt; takes a number or percentage value. Any number above 100% or 1 leads to a complete black-and-white result. 0 or 0% means no black-and-white filtering is applied, and anywhere from 0 or 0% to 1 or 100% means applying a partial black-and-white filter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hue-rotate:&lt;/strong&gt; takes an angle that represents how much the hue should change. Degree values range from 0 to 360, where 0 is red, 120 is green and 240 is blue. Negative values are also accepted. Example: &lt;code&gt;backdrop-filter: hue-rotate(45deg);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invert:&lt;/strong&gt; takes a number or percentage. 0 or 0% means no effect is applied. 1 and 100% means a totally inverted effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Opacity:&lt;/strong&gt; takes a number or percentage between 0 to 1 and 0 to 100%. It seems like the effect can only be seen when it is also paired with for example a mix-blend-mode. When using backdrop-filter opacity with mix-blend-mode it is also possible to work with background-colors and colors that are not semi-transparent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sepia:&lt;/strong&gt; takes a number or percentage between 0 to 1 and 0 to 100%, where a lower number indicates a fainter sepia-tone level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saturate:&lt;/strong&gt; takes a number or percentage, where a lower number means lower saturation and a higher number increases saturation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;None, inherit, initial, unset:&lt;/strong&gt; There are also the values &lt;code&gt;none&lt;/code&gt;, &lt;code&gt;inherit&lt;/code&gt;, &lt;code&gt;initial&lt;/code&gt;, and &lt;code&gt;unset&lt;/code&gt; to control inheritance, reset or remove any existing setting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SVG filters:&lt;/strong&gt; It is possible to add a URL to an SVG filter to use as well. Example: &lt;code&gt;backdrop-filter: url(filters.svg#filter);&lt;/code&gt; There is &lt;a href="https://bugs.chromium.org/p/chromium/issues/detail?id=109212"&gt;a bug&lt;/a&gt; in &lt;code&gt;filter&lt;/code&gt; that prevents linking to external SVG file filters in Chrome, that might also impact backdrop-filter. Even though linking to external SVG file filters might work at some point in the future, the recommendation is to link to filters in the same HTML document using inline styles.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--HTML--&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;"frosted-glass-parent"&lt;/span&gt;&lt;span class="nt"&gt;&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;"frosted-glass-child"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"backdrop-filter: url(#duotone);"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Frosted Glass Effect&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&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;Another alternative to avoid using inline styles in the HTML document is to just reference the SVG filter ID (which still has to be placed in the HTML file) in the CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.classname&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;backdrop-filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url(#duotone)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Areas of Use
&lt;/h3&gt;

&lt;p&gt;Just like with blend-modes and filters, backdrop-filters are useful for adding enhancements and effects that are uniform and does not require website administrators to know how to edit images in an image editing software program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Browser Support
&lt;/h3&gt;

&lt;p&gt;Backdrop-filter is at the time of writing supported in about &lt;a href="https://caniuse.com/#search=backdrop-filter"&gt;86% of all modern browsers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Support is missing from Internet Explorer as well as from a handful of mobile browsers. Most importantly, the feature is behind a flag in Firefox, meaning users will have to enable it manually for it to work. Since the normal user will probably not have done this, it is important to have sufficient fallbacks when using this feature in production.&lt;/p&gt;

&lt;p&gt;One way of creating fallbacks is to use &lt;code&gt;@supports&lt;/code&gt; queries for browsers where such queries work. Browsers where @support queries does not work will need special treatment in the form of separate, additional fallback styling served specifically to them.&lt;/p&gt;





&lt;h3&gt;
  
  
  How to Allow the Feature in Firefox:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Enter about:config in the URL input bar.&lt;/li&gt;
&lt;li&gt;Accept that you are now going to alter important settings.&lt;/li&gt;
&lt;li&gt;Search for backdrop-filter in the search bar.&lt;/li&gt;
&lt;li&gt;Click on the arrows to toggle true/false.&lt;/li&gt;
&lt;li&gt;Apart from allowing backdrop-filter, there is also &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1178765"&gt;a bug&lt;/a&gt; that requires you to also to set gfx.webrender.all to true.&lt;/li&gt;
&lt;li&gt;After changing the settings, restart Firefox.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>backdropfilter</category>
      <category>cssfilter</category>
    </item>
    <item>
      <title>Blend Modes in CSS</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 05 Jun 2020 08:56:36 +0000</pubDate>
      <link>https://dev.to/fridanyvall/blend-modes-in-css-268</link>
      <guid>https://dev.to/fridanyvall/blend-modes-in-css-268</guid>
      <description>&lt;p&gt;Being able to blend background images and HTML elements is not a new technology. However, as global browser support nowadays is slightly above &lt;a href="https://caniuse.com/#search=blend-mode"&gt;90% world-wide&lt;/a&gt;, these techniques have become increasingly accessible for most users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: Skip the theory and go straight to the image examples!&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Blend Modes?
&lt;/h2&gt;

&lt;p&gt;Blend modes in CSS are used to blend two or more layers using mathematics to calculate the color value for every pixel. You can use blend modes on both images, gradients, and solid-colored elements. Those familiar with graphical software programs like Photoshop will recognize some of the blend modes available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Blend Modes?
&lt;/h2&gt;

&lt;p&gt;Blend modes are useful for creating visual enhancements while still utilizing HTML elements. This is great for SEO and performance. It also makes it easier to create coherent image and text styles, without requiring skills in image editing software programs.&lt;/p&gt;

&lt;p&gt;Using blend modes, website administrators can create visual effects with dynamic content without having to rely on or create specific image content. Creating images also takes time and requires skills in image editing software (like Photoshop). If more employees can help editing because fewer skills are required, it will be easier, faster, and cheaper to edit the website.&lt;/p&gt;

&lt;p&gt;Being able to avoid using text as part of images is also beneficial for SEO as well as site performance (since images are always heavy resources to load compared to a few lines of code).&lt;/p&gt;



&lt;h2&gt;
  
  
  background-blend-mode
&lt;/h2&gt;

&lt;p&gt;Blends the background of an element. An element can simultaneously have background images, gradients and colors set to it. Using &lt;code&gt;background-blend-mode&lt;/code&gt;, these layers will get blended with each other. The way layers are blended is decided by one or more (comma separated) &lt;code&gt;background-blend-mode&lt;/code&gt; values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--HTML--&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;"example"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*CSS*/&lt;/span&gt;
&lt;span class="nc"&gt;.example&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="sx"&gt;url('your-image.jpg')&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;bottom&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="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-repeat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;background-blend-mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;luminosity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;500px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;500px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The order layers are blended seems to be from the first defined background value, which is also the “top” layer. In the above example, the background image will be blended with the linear gradient using the &lt;code&gt;luminosity&lt;/code&gt; blend mode value. The color green will then be blended with the result of the image and the gradient, using the &lt;code&gt;screen&lt;/code&gt; blend mode value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If the number of &lt;code&gt;background-blend-mode&lt;/code&gt; values does not match the list of background images, gradients, or colors set on an element, the &lt;code&gt;background-blend-mode&lt;/code&gt; values will be repeated or removed to make the lists match.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blend Mode Values
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Color:&lt;/strong&gt; combines the hue and saturation of the overlapping layer with the luminosity of the bottom layer. It can be used to add color or tint to images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Color-burn:&lt;/strong&gt; makes the overlapping area darker unless white is used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Color-dodge:&lt;/strong&gt; makes the overlapping area brighter unless black is used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Darken:&lt;/strong&gt; uses the darkest value of the colors to be blended.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Difference:&lt;/strong&gt; subtracts the darker of the colors to be blended from the lighter one. Using white inverts the color of the overlapping area. Using black nothing will change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exclusion:&lt;/strong&gt; is similar to difference, but with lower contrast. Using white inverts the color of the overlapping area, and using black nothing will change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard-light:&lt;/strong&gt; multiplies or screens the colors, depending on the colors of the overlapping layer. Similar to overlay, but with the layers swapped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hue:&lt;/strong&gt; combines the hue from the overlapping layer and the saturation and luminosity of the other layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inherit:&lt;/strong&gt; any value inherited from a parent element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initial:&lt;/strong&gt; the initial, default value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lighten:&lt;/strong&gt; uses the lightest values of the colors to be blended.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Luminosity:&lt;/strong&gt; inverting the effect of the color mode: similar to color, but with the layers swapped. The result has the luminosity of the top layer, with the hue and saturation of the bottom layer. Used to make monochrome tinted images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiply:&lt;/strong&gt; the color values of the layers are multiplied with each other. Colors multiplied with black produces black. Multiplying a color with white renders the original color.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Normal:&lt;/strong&gt; no fancy effect happening. This is like stacking the elements with no blending mode applied.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overlay:&lt;/strong&gt; uses either multiply or screen, depending on the colors of the two layers. Similar to hard-light, but with the layers swapped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saturation:&lt;/strong&gt; blends the saturation of the top with the hue and luminosity of bottom layer. If used on an area that has no saturation (is grey), nothing will happen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Screen:&lt;/strong&gt; inverts the colors, multiplies them and inverts the resulting value. Using screen on any color with white renders white. Using with black results in the original color. Compare with multiply.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Soft-light:&lt;/strong&gt; darkens or lightens the colors, depending on the colors of the overlapping layer. Similar to hard-light, but softer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unset:&lt;/strong&gt; global value, resetting the property to fallback to inherit (if the property will inherit from its parent) or initial.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since there are so many different blend modes available, and the fact that they can be used simultaneously on multiple background layers makes it possible to create a lot of visual effects. Sometimes the best way to find a suitable effect is simply to try different combinations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ilyashubin.github.io/FilterBlend/"&gt;FilterBlend&lt;/a&gt; is a free online tool to play around with different effects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sarasoueidan.com/demos/css-blender/"&gt;CSS Blender&lt;/a&gt; by Sara Soueidan is another online demo tool.&lt;/p&gt;

&lt;p&gt;There is also a more general map of the blend modes and their visual impacts at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/blend-mode"&gt;Mozilla&lt;/a&gt;.&lt;/p&gt;



&lt;h2&gt;
  
  
  mix-blend-mode
&lt;/h2&gt;

&lt;p&gt;Just as &lt;code&gt;background-blend-mode&lt;/code&gt; blends background images, gradients, and colors, &lt;code&gt;mix-blend-mode&lt;/code&gt; is for blending different elements with their surroundings. &lt;code&gt;mix-blend-mode&lt;/code&gt; can also be used with pseudo-elements, which makes it even more practical for creating visual effects.&lt;/p&gt;

&lt;p&gt;Typically, this can be used when you need to change the color of an element depending on the colors of its background.&lt;/p&gt;

&lt;p&gt;Thankfully, &lt;code&gt;mix-blend-mode&lt;/code&gt; takes the same values as &lt;code&gt;background-blend-mode&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Isolate Elements: Prevent Elements from Blending
&lt;/h4&gt;

&lt;p&gt;Sometimes it can be required to prevent an element from blending. This can be done by setting &lt;code&gt;isolation: isolate;&lt;/code&gt; on a parent wrapper around the element you want to prevent from blending.&lt;/p&gt;

&lt;p&gt;Isolation is not needed for background-blend-mix, since all the blending is supposed to take place within the element itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; You can achieve the same result of not blending an element if the overlapping element is placed in a parent with &lt;code&gt;opacity&lt;/code&gt; lower than 1.&lt;/p&gt;



&lt;h2&gt;
  
  
  Browser Support
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;background-blend-mode&lt;/code&gt; is supported in all modern browsers, with a &lt;a href="https://caniuse.com/#search=background-blend-mode"&gt;global support of about 93%&lt;/a&gt;. &lt;code&gt;background-blend-mode&lt;/code&gt; is not supported in Internet Explorer or older versions of Edge (versions 12-18). iOS Safari might not support multiple background-blend-modes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mix-blend-mode&lt;/code&gt; has a &lt;a href="https://caniuse.com/#search=mix-blend-mode"&gt;global support of around 91%&lt;/a&gt; (for HTML elements, or blending HTML and SVG, significantly less for only SVG) and is supported in most modern browsers. The feature is not supported in Internet Explorer or older versions of Edge (versions 12-18). Support is reported as “unknown” for some less popular browsers (like Opera Mini).&lt;/p&gt;



&lt;h2&gt;
  
  
  Blend Mode Examples
&lt;/h2&gt;

&lt;p&gt;The amount of effects that can be achieved using all the capabilities of blend modes is of course mind-bogglingly large. However, some are more common than others.&lt;/p&gt;
&lt;h3&gt;
  
  
  Tinting an image
&lt;/h3&gt;

&lt;p&gt;Tinting an image with a color of your choice. This helps reduce the time spent on image editing. Employees with no image editing skills can update the images.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/dyYQNyK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Blending multiple images
&lt;/h3&gt;

&lt;p&gt;Making two or more images blend artfully.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/yLYQgaV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Knockout Text and Overlapping Text
&lt;/h3&gt;

&lt;p&gt;Useful for improving SEO since the text does not have to be included in the image. Can help improve performance if the image can be made smaller when not having to incorporate text. This helps reduce the time spent on image editing.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/KKdrqwV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/KKdrqXN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/fridanyvall/embed/paPwJL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdesign</category>
      <category>blendmode</category>
    </item>
    <item>
      <title>Importing Styles to Component Style Tags in Nuxt.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 07 Feb 2020 10:47:13 +0000</pubDate>
      <link>https://dev.to/fridanyvall/importing-styles-to-component-style-tags-in-nuxt-js-5dj7</link>
      <guid>https://dev.to/fridanyvall/importing-styles-to-component-style-tags-in-nuxt-js-5dj7</guid>
      <description>&lt;p&gt;In Vue.js projects, there are different ways of importing styles to a project. See the article &lt;a href="https://dev.to/fridanyvall/importing-style-files-to-component-style-tags-in-vue-js-14nl"&gt;“Importing Style Files to Component Style Tags in Vue.js”&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;In Nuxt.js projects, there are similar ways of importing styles, however with some slight differences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing Global Styles in Component Style Tags
&lt;/h2&gt;

&lt;p&gt;Importing global styles to be used in component style tags. A convenient way of not having to import certain files manually every time for every component. This is meant for adding the type of SCSS files that contains mixins and variables that will not generate any output before they are being used in a context. This is because it will be added to each component’s CSS when imported.&lt;/p&gt;

&lt;p&gt;Making this type of import has been made available natively in Vue.js, but Nuxt.js still seems to be in need of the &lt;a href="https://www.npmjs.com/package/@nuxtjs/style-resources"&gt;style-resources plugin&lt;/a&gt;. In the project folder, run:&lt;br&gt;
&lt;code&gt;npm install --save-dev @nuxtjs/style-resources&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add the following to your &lt;strong&gt;nuxt.config file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
  ** Nuxt.js modules
  */&lt;/span&gt;
  &lt;span class="nx"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nuxtjs/style-resources&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="cm"&gt;/*@nuxtjs/style-resources*/&lt;/span&gt;
  &lt;span class="nx"&gt;styleResources&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//array of strings that are paths to the file:&lt;/span&gt;
    &lt;span class="nl"&gt;scss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;~assets/styles/globalvariables.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  :src Imports
&lt;/h2&gt;

&lt;p&gt;Importing separate style files in the style tag by using “src” imports seems to work the same for Nuxt.js as in Vue.js.&lt;/p&gt;

&lt;p&gt;For example pages/subfolder/filename.scss can be used with for example pages/subfolder/index.vue. This works for normal as well as scoped style tags. Thanks to &lt;a href="https://dev.to/souljorje"&gt;Georgiy Bukharov&lt;/a&gt; for also showing that it works with the module tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--Normal example--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./filename.scss"&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Note&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;any&lt;/span&gt; &lt;span class="nt"&gt;other&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;ignored&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Only&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;src&lt;/span&gt; &lt;span class="nt"&gt;import&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;being&lt;/span&gt; &lt;span class="nt"&gt;used&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--Scoped styles--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;scoped&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./filename.scss"&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Note&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;any&lt;/span&gt; &lt;span class="nt"&gt;other&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;ignored&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Only&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;src&lt;/span&gt; &lt;span class="nt"&gt;import&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;being&lt;/span&gt; &lt;span class="nt"&gt;used&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--CSS Module styles--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"$style.classname"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Remember to add the classes with js.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!--Remember to add the '.module' suffix just before the file extension.--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./filename.module.scss"&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Note&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;any&lt;/span&gt; &lt;span class="nt"&gt;other&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;ignored&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Only&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;src&lt;/span&gt; &lt;span class="nt"&gt;import&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;being&lt;/span&gt; &lt;span class="nt"&gt;used&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To read more about src imports in Vue, see the section “:src Imports” in the article &lt;a href="https://dev.to/fridanyvall/importing-style-files-to-component-style-tags-in-vue-js-14nl#src-imports"&gt;“Importing Style Files to Component Style Tags in Vue.js”&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Setup
&lt;/h2&gt;

&lt;p&gt;Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve used create-nuxt-app version 2.10.0 to set up the project. When setting up the project, I chose NPM as package manager and jsconfig.json as development tool (only choice available and recommended for VS Code).&lt;/p&gt;

</description>
      <category>nuxt</category>
      <category>css</category>
      <category>vue</category>
    </item>
    <item>
      <title>CSS Modules in Nuxt.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Sun, 02 Feb 2020 12:03:55 +0000</pubDate>
      <link>https://dev.to/fridanyvall/css-modules-in-nuxt-js-815</link>
      <guid>https://dev.to/fridanyvall/css-modules-in-nuxt-js-815</guid>
      <description>&lt;p&gt;The second article in a series about working with CSS in Nuxt.js, this time focusing on how to use CSS Modules with Nuxt.&lt;/p&gt;

&lt;p&gt;Just like in Vue.js, support for CSS Modules is provided out of the box in Nuxt.js projects. (To learn more about CSS Modules in Vue, &lt;a href="https://dev.to/fridanyvall/css-modules-in-vue-js-2267"&gt;see this article&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;To alter the generated class names, edit the &lt;strong&gt;nuxt.config.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*
    ** You can extend webpack config here
    */&lt;/span&gt;      
    &lt;span class="nl"&gt;loaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;cssModules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;//this is where you can alter the generated class names:&lt;/span&gt;
          &lt;span class="na"&gt;localIdentName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[local]--[Frida]_[hash:base64:4]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above, I’ve added my own name in the middle of the class name.&lt;/p&gt;

&lt;p&gt;Add the module keyword to the style section of the component file:&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 &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.moduleclass&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use CSS Modules by binding the class name using JavaScript in the template part of the component .vue file:&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;template&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"$style.moduleclass"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is also possible to use CSS Modules for just some and not all of the components in a project. Check out &lt;a href="https://vue-loader.vuejs.org/guide/css-modules.html#opt-in-usage"&gt;the documentation&lt;/a&gt; on how to set up opt-in usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing Module Style Files
&lt;/h2&gt;

&lt;p&gt;In Vue.js projects, it is possible to put styles in a separate filename.module.css file and import it in the script tag of a .vue file. (See the article &lt;a href="https://dev.to/fridanyvall/css-modules-in-vue-js-2267"&gt;“CSS Modules in Vue.js”&lt;/a&gt; for more info.)&lt;/p&gt;

&lt;p&gt;The same approach works for Nuxt projects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--CSS Module styles--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"$style.classname"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Remember to add the classes with js.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!--Remember to add the '.module' suffix just before the file extension.--&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./filename.module.scss"&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Note&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;any&lt;/span&gt; &lt;span class="nt"&gt;other&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;ignored&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;Only&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;styles&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;src&lt;/span&gt; &lt;span class="nt"&gt;import&lt;/span&gt; &lt;span class="nt"&gt;are&lt;/span&gt; &lt;span class="nt"&gt;being&lt;/span&gt; &lt;span class="nt"&gt;used&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Start Setup
&lt;/h2&gt;

&lt;p&gt;Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve used create-nuxt-app version 2.10.0 to set up the project. When setting up the project, I chose NPM as package manager and jsconfig.json as development tool (only choice available and recommended for VS Code).&lt;/p&gt;

</description>
      <category>nuxt</category>
      <category>css</category>
      <category>vue</category>
    </item>
    <item>
      <title>Working with CSS in Nuxt.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 24 Jan 2020 18:41:07 +0000</pubDate>
      <link>https://dev.to/fridanyvall/working-with-css-in-nuxt-js-50i0</link>
      <guid>https://dev.to/fridanyvall/working-with-css-in-nuxt-js-50i0</guid>
      <description>&lt;p&gt;The first part of a series of articles about working with CSS in Nuxt.js, showing different ways of adding CSS to a Nuxt.js project.&lt;/p&gt;

&lt;p&gt;Main topics in this article are preprocessors, autoprefixing, CSS Source Maps , global styles and how to add a separate CSS file to the document head.&lt;/p&gt;

&lt;p&gt;There are a lot of similarities with how CSS works in Vue.js (read the article series &lt;a href="https://dev.to/fridanyvall/working-with-css-in-vue-js-ek5"&gt;“Working with CSS in Vue.js”&lt;/a&gt;), however since the Nuxt.js project is set up slightly differently, there are also some differences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Setup
&lt;/h2&gt;

&lt;p&gt;Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve used create-nuxt-app version 2.10.0 to set up the project. When setting up the project, I chose NPM as package manager and jsconfig.json as development tool (only choice available and recommended for VS Code).&lt;/p&gt;

&lt;h2&gt;
  
  
  Production Output
&lt;/h2&gt;

&lt;p&gt;Unlike the default production setting in a Vue.js project, styles are not automatically output as a CSS file. The CSS output depends on the chosen setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding SASS/SCSS to a Nuxt.js project
&lt;/h2&gt;

&lt;p&gt;Adding SASS/SCSS to a Nuxt.js project works just like for a Vue.js project. Install &lt;a href="https://www.npmjs.com/package/sass-loader"&gt;sass-loader&lt;/a&gt; and &lt;a href="https://github.com/sass/node-sass"&gt;node-sass&lt;/a&gt; in your project:&lt;br&gt;
&lt;code&gt;npm install --save-dev sass-loader&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm install --save-dev node-sass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To read more about working with CSS in Vue.js projects, see the articles series starting with &lt;a href="https://dev.to/fridanyvall/working-with-css-in-vue-js-ek5"&gt;“Working with CSS in Vue.js”&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Autoprefixing
&lt;/h2&gt;

&lt;p&gt;Autoprefixing is included in Nuxt.js and is handled by autoprefixer, which in turn relies on &lt;a href="https://github.com/browserslist/browserslist"&gt;Browserslist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To control which browser versions are included, you could create a separate browserslistrc config file, or add a setting in the package.js file. Place the setting after devdependecies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;browserslist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cover 99.5%&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;//or whatever settings you prefer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prefixes are added to the CSS during development as well as for production.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Source Maps
&lt;/h2&gt;

&lt;p&gt;CSS source maps seem to be supported out of the box with the create-nuxt-app package. To also have CSS source maps in production, add the setting &lt;code&gt;cssSourceMap: true&lt;/code&gt; to the &lt;strong&gt;nuxt.config file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="cm"&gt;/* Build configuration */&lt;/span&gt;
  &lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* You can extend webpack config here */&lt;/span&gt;
   &lt;span class="nl"&gt;cssSourceMap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that for some reason, the source maps did not seem to work for me when testing in Mozilla Firefox. This &lt;a href="https://github.com/nuxt/nuxt.js/issues/3213"&gt;closed bug thread&lt;/a&gt; suggests that extending the webpack configuration manually might do the trick. However note that the syntax of the suggested configuration differs from what is suggested in the &lt;a href="https://nuxtjs.org/api/configuration-build/#csssourcemap"&gt;nuxt documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Styles
&lt;/h2&gt;

&lt;p&gt;Global styles in the form of .css or .scss files can be added via the &lt;strong&gt;nuxt.config file&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="cm"&gt;/* Global CSS */&lt;/span&gt;
  &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;~assets/styles/global.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;~assets/styles/globalscss.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In that way, it is possible to set up an SCSS or CSS workflow that is completely outside of the style tags in .vue files or components, similarly to working without a framework.&lt;/p&gt;

&lt;p&gt;Note that the variables declared in these files are not available in the .vue file style section. Classes declared in the global files are available to use in the template parts of .vue files, and can be overwritten in the .vue style section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Output Separate CSS File
&lt;/h2&gt;

&lt;p&gt;This corresponds to the setting &lt;code&gt;css: {extract: true}&lt;/code&gt; in a vue.config.js file for a Vue.js project.&lt;/p&gt;

&lt;p&gt;For a Nuxt.js project, add the line &lt;code&gt;extractCSS: true&lt;/code&gt; to the &lt;strong&gt;nuxt.config.js file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="cm"&gt;/* Build configuration */&lt;/span&gt;
  &lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* You can extend webpack config here */&lt;/span&gt;
    &lt;span class="nl"&gt;extractCSS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that this will also take scoped styles and place them in .css files to be linked in the document head. The .css files are not concatenated; each one is added separately for every scoped section.&lt;/p&gt;

&lt;p&gt;To get all of the styles into one single .css file (and save some requests), use the Nuxt.js optimization setting &lt;a href="https://nuxtjs.org/api/configuration-build/#optimization"&gt;splitChunks&lt;/a&gt;. splitChunks is a part of webpack, so the &lt;a href="https://webpack.js.org/plugins/split-chunks-plugin/"&gt;splitChunks webpack documentation&lt;/a&gt; is more detailed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding External CSS to Document Head
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to add external CSS, like for example Google Fonts to your project. In Nuxt.js projects, you’ll find settings for this in the &lt;strong&gt;nuxt.config.js file&lt;/strong&gt;. Add the link to your file in the link array in the head object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="cm"&gt;/* Headers of the page */&lt;/span&gt;
  &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* ...other things that will be output to the head element */&lt;/span&gt;
    &lt;span class="nl"&gt;link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="cm"&gt;/* add your header links here, for example to favicon and Google Fonts*/&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;icon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image/x-icon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/favicon.ico&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://fonts.googleapis.com/css?family=Yrsa&amp;amp;display=swap&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>nuxt</category>
      <category>css</category>
      <category>vue</category>
    </item>
    <item>
      <title>Nuxt.js with ESLint in VS Code</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Sat, 18 Jan 2020 17:10:14 +0000</pubDate>
      <link>https://dev.to/fridanyvall/nuxt-js-with-eslint-in-vs-code-1ne2</link>
      <guid>https://dev.to/fridanyvall/nuxt-js-with-eslint-in-vs-code-1ne2</guid>
      <description>&lt;p&gt;This is a short “note to self” type of article where a future me and perhaps someone else as well, might save some time googling for tooling configurations.&lt;/p&gt;

&lt;p&gt;When setting up a Nuxt.js project with ESLint using create-nuxt-app version 2.10.0, I ran into this &lt;a href="https://github.com/nuxt/create-nuxt-app/issues/274"&gt;problem&lt;/a&gt;, where the project did not autofix on save.&lt;/p&gt;

&lt;p&gt;I was not using Prettier so the error would stem from the ESLint integration solely. Since I had ESLint working in a previous Vue.js project out of the box, I decided to use those settings instead and installed the ESLint plugin used in the Vue.js project: &lt;code&gt;npm install --save-dev eslint-plugin-vue&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I then changed the VS Code settings.json file to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eslint.validate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;language&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;autoFix&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;language&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;autoFix&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;language&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;autoFix&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;language&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;javascriptreact&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;autoFix&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eslint.autoFixOnSave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;editor.formatOnSave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And made the following changes to the .eslintrc.js file, where I removed the nuxt eslint extensions and added one for vue instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;parserOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-eslint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;extends&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;//'@nuxtjs',&lt;/span&gt;
    &lt;span class="c1"&gt;//'plugin:nuxt/recommended',&lt;/span&gt;
    &lt;span class="c1"&gt;//'plugin:vue/recommended',&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin:vue/essential&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;eslint:recommended&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you also need to add Prettier to your project, you might find some help in this post: &lt;a href="https://medium.com/@gogl.alex/how-to-properly-set-up-eslint-with-prettier-for-vue-or-nuxt-in-vscode-e42532099a9c"&gt;"How to properly set up Nuxt with ESLint and Prettier in VSCode"&lt;/a&gt;&lt;/p&gt;

</description>
      <category>eslint</category>
      <category>vscode</category>
      <category>nuxt</category>
    </item>
    <item>
      <title>Importing Style Files to Component Style Tags in Vue.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 22 Nov 2019 10:43:20 +0000</pubDate>
      <link>https://dev.to/fridanyvall/importing-style-files-to-component-style-tags-in-vue-js-14nl</link>
      <guid>https://dev.to/fridanyvall/importing-style-files-to-component-style-tags-in-vue-js-14nl</guid>
      <description>&lt;p&gt;Part four in a series of articles about working with CSS in Vue.js, discussing different ways of importing separate style files to .vue file style tags.&lt;/p&gt;

&lt;p&gt;The examples are using SCSS. To read more about adding SCSS support to a Vue.js project, see the article &lt;a href="https://redonion.se/en/working-with-css-in-vue-js-2/" rel="noopener noreferrer"&gt;”Working with CSS in Vue.js”&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing Global Styles in Component Style Tags
&lt;/h2&gt;

&lt;p&gt;Adding an automatic import of a file instead of repeating the same import in all of your component files can save time and effort.&lt;/p&gt;

&lt;p&gt;Note that the code in the imported file should be such that it is only output when used (like scss mixins and scss variables). &lt;strong&gt;This is because it will be added to each component’s CSS when imported.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In earlier versions of Vue.js, a plugin called &lt;a href="https://www.npmjs.com/package/vue-cli-plugin-style-resources-loader" rel="noopener noreferrer"&gt;style-resources-loader&lt;/a&gt; was needed for this to work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An Example Setup&lt;/strong&gt;&lt;br&gt;
In the src folder, add a new folder for your styles. Add any other SCSS files you want to use. An example might look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi5rgte7ra0q04vg9nicv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi5rgte7ra0q04vg9nicv.jpg" alt="Folder structure: styles/folders or scss files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember to import everything (mixins, variables) you want to distribute in your &lt;strong&gt;global.scss file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s1"&gt;'./mixins/mixins'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s1"&gt;'variables'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following code to your &lt;strong&gt;vue.config.js file&lt;/strong&gt;, to link to your global.scss file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;loaderOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;scss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`@import "~@/styles/global.scss";`&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the file &lt;strong&gt;Component.vue style tag&lt;/strong&gt;, you should now be able to use the styles defined in all of the files accessed in global.scss:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;style&lt;/span&gt; &lt;span class="nt"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"scss"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nc"&gt;.testclass&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$primary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* variable declared in src/styles/_variables.scss */&lt;/span&gt;
    &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;fontSizePX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;24&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="cm"&gt;/* mixin declared in src/styles/mixins/_mixins.scss */&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;style&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
Vue CLI v3 works with sass-loader v7 only at the moment. You can downgrade it for now by uninstalling sass-loader and installing the latest v7 version. To do this, run &lt;code&gt;npm uninstall sass-loader&lt;/code&gt; and &lt;code&gt;npm i --save-dev sass-loader@7.3.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read more about this in the &lt;a href="https://github.com/vuejs/vue-cli/issues/4526" rel="noopener noreferrer"&gt;Vue CLI GitHub Issues&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With Vue CLI v4+ and sass-loader v8+, &lt;code&gt;data&lt;/code&gt; will be renamed &lt;code&gt;prependData&lt;/code&gt; in the vue.config.js file.&lt;/p&gt;
&lt;h2&gt;
  
  
  :src Imports
&lt;/h2&gt;

&lt;p&gt;Another way of importing styles from a separate file is to write a src declaration in the style tag, referencing the style file you want to use.&lt;/p&gt;

&lt;p&gt;For example you have a separate style file, &lt;strong&gt;Tipp.scss&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.tipp&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a component file, &lt;strong&gt;Tipp.vue&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&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;"tipp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Hello Tipp&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- will be in uppercase --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./Tipp.scss"&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is handy if you want to keep your component styles in a separate file. It is also possible to put the component js code in a separate file and add it in the same way to the script tag in the .vue file.&lt;/p&gt;

&lt;p&gt;When experimenting with this, it seemed like any additional styles declared within the component style tag were ignored. So the only styles active were the ones in the linked file. If I did a normal import inside the same style tag, as described in the following section, those styles did however work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normal SCSS Imports
&lt;/h2&gt;

&lt;p&gt;Using normal import declarations for external style files work as well. In the file &lt;strong&gt;global-ui.scss&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.green&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$global-ui-darkred&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;136&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;Component.vue&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&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;"darkred"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"green"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;I get style from file global-ui.scss imported&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- class declared in the imported file --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s1"&gt;"./src/styles/global-ui.scss"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;.darkred&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;global-ui-darkred&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c"&gt;/*SCSS variable from the imported file*/&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Setup
&lt;/h4&gt;

&lt;p&gt;The starting code for this article is created by the Vue CLI command tool version 3.3.0 and Vue.js version 2.6.10. Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>css</category>
      <category>vue</category>
    </item>
    <item>
      <title>CSS Modules in Vue.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 15 Nov 2019 12:36:07 +0000</pubDate>
      <link>https://dev.to/fridanyvall/css-modules-in-vue-js-2267</link>
      <guid>https://dev.to/fridanyvall/css-modules-in-vue-js-2267</guid>
      <description>&lt;p&gt;Part three in a series of articles about working with CSS in Vue.js, focusing on using CSS Modules in Vue.js.&lt;/p&gt;

&lt;p&gt;In these types of javascript frameworks, it has become popular to use CSS Modules. It is a way of automatically creating unique class names, thereby preventing any possible style leaking or accidental cascading between elements.&lt;/p&gt;

&lt;p&gt;Support for CSS Modules is already setup by default by the Vue CLI.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;.vue file&lt;/strong&gt; where you want to use CSS modules, add &lt;code&gt;module&lt;/code&gt; to the style tag:&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 &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.hello&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;mistyrose&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the &lt;strong&gt;.vue file&lt;/strong&gt; template, the class name is then added though javascript:&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;div&lt;/span&gt; &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"$style.hello"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;$style&lt;/code&gt; prefix used in .vue file templates refers to any styles within the component (.vue file) style tag.&lt;/p&gt;

&lt;h4&gt;
  
  
  Targeting Elements Without Defined Class Names
&lt;/h4&gt;

&lt;p&gt;For targeting HTML elements without defined class names like &lt;code&gt;p&lt;/code&gt; or &lt;code&gt;h1&lt;/code&gt;, you would have to make sure they are related to their parent for them to be included in the module class name output.&lt;/p&gt;

&lt;p&gt;In the example below, the &lt;code&gt;h3&lt;/code&gt; style text-decoration underline will be added to the CSS as &lt;code&gt;.hello-[modulename-identifier] h3&lt;/code&gt;. The &lt;code&gt;h3&lt;/code&gt; outside of the &lt;code&gt;.hello&lt;/code&gt; block will not be included in the &lt;code&gt;.hello-[modulename-identifier]&lt;/code&gt; block and therefore risk affecting other &lt;code&gt;h3&lt;/code&gt; tags outside of the module.&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 &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.hello&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;mistyrose&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nc"&gt;.hello&lt;/span&gt; &lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt; &lt;span class="m"&gt;0&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Changing the Class Name
&lt;/h2&gt;

&lt;p&gt;If you want to change the generated module class names, this can be done in the &lt;strong&gt;vue.config.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;loaderOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;localIdentName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[local][Frida][hash:base64:8]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="c1"&gt;// camelCase: 'only' //optional setting for how the name is output&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above, I’ve added my name &lt;code&gt;Frida&lt;/code&gt; followed by a random string to make sure the name is unique.&lt;/p&gt;

&lt;h2&gt;
  
  
  Utilizing the Class Name
&lt;/h2&gt;

&lt;p&gt;It is possible to use the expression &lt;code&gt;$style.hello&lt;/code&gt; as a reference to the class name elsewhere within the vue component.&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;p&amp;gt;&lt;/span&gt;Printing $style.hello: {{ $style.hello }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code above will output: “Printing $style.hello: hello-[modulename-identifier]”&lt;/p&gt;

&lt;p&gt;This can also be used to for example target the element using JavaScript, access and print out its styles or do any other operation. Because of the module naming convention, the class name acts like an ID meaning there will be just one element with this specific name.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example: Outputting an Element Style
&lt;/h4&gt;

&lt;p&gt;A very basic example where an element’s background-color is output.&lt;br&gt;
In the &lt;strong&gt;.vue file template section&lt;/strong&gt;:&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;p&amp;gt;&lt;/span&gt;Output: {{ $style.hello }} background-color is: {{ dataBackground }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!-- Output: hello-[modulename-indentifier] background-color is: #ffe4e1 --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the &lt;strong&gt;.vue file script section&lt;/strong&gt;:&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;script&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;dataBackground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;background&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;classNameBackground&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;compStyles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getComputedStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;compStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPropertyValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;background-color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataBackground&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;bg&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;bg&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;mounted&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classNameBackground&lt;/span&gt;&lt;span class="p"&gt;()&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;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the &lt;strong&gt;.vue file style section&lt;/strong&gt;:&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 &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.hello&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ffe4e1&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  CSS Modules :export
&lt;/h2&gt;

&lt;p&gt;Another way of accessing information in the CSS style block is to use the &lt;code&gt;:export&lt;/code&gt; feature in CSS Modules to export strings from the style tag:&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;template&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{ $style.primaryColor }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Outputs: #ffe4e1 --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{ $style.displayAs }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Outputs: some-string --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;module&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;”scss”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;primary-color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;#ffe4e1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nd"&gt;:export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;primaryColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;primary-color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="py"&gt;displayAs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;some-string&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;:export&lt;/code&gt; feature seems restricted to working with strings. To get a specific CSS style from a named class, it might be better to go the JavaScript route as per the example above.&lt;/p&gt;

&lt;p&gt;Read more about the &lt;code&gt;:export&lt;/code&gt; feature in the &lt;a href="https://github.com/css-modules/icss"&gt;CSS Modules documentation&lt;/a&gt;, and more about CSS Modules in Vue, in the &lt;a href="https://vue-loader.vuejs.org/guide/css-modules.html#usage"&gt;vue-loader documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing Module Style Files
&lt;/h2&gt;

&lt;p&gt;For when you want to place your CSS Module styles in separate files, rather than in the style tag in each .vue component file.&lt;/p&gt;

&lt;p&gt;Create your style file and place it for example in the same folder as your component. Name it “filename.module.fileextension”. In my example, I’ve created the style file “HelloWorld.module.scss”. In &lt;strong&gt;HelloWorld.module.scss&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.imported&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Import the style file in the script tag of the component where you want to use it, in this example &lt;strong&gt;HelloWorld.vue&lt;/strong&gt;. I found that I also had to add the import to data:&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;script&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;scssStyles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./HelloWorld.module.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;scssStyles&lt;/span&gt;
      &lt;span class="p"&gt;}&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;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the template part of your component, add the class by referencing the import name and the class name in the style file:&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;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"scssStyles.imported"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Make all CSS Files CSS Modules
&lt;/h4&gt;

&lt;p&gt;If you want to be able to omit the extra “.module” in the file name, you can add this setting to the &lt;strong&gt;vue.config.js file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However setting modules to true will at the same time also cause &lt;em&gt;all other style files&lt;/em&gt; to be working as CSS modules.&lt;/p&gt;

&lt;p&gt;Note that the imported .module.scss file content is not available to the vue file component style tag, since the generated class names will differ. Also note that variables inside the imported file are not available to the component style tag. To achieve this, see the article &lt;a href="https://redonion.se/en/importing-style-files-to-component-style-tags-in-vue-js-2/"&gt;“Importing Style Files to Component Style Tags in Vue.js”&lt;/a&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Setup
&lt;/h4&gt;

&lt;p&gt;The starting code for this article is created by the Vue CLI command tool version 3.3.0 and Vue.js version 2.6.10. Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>vue</category>
      <category>css</category>
      <category>cssmodules</category>
    </item>
    <item>
      <title>Adding CSS to a Vue.js Project</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 08 Nov 2019 16:04:14 +0000</pubDate>
      <link>https://dev.to/fridanyvall/adding-css-to-a-vue-js-project-2kjk</link>
      <guid>https://dev.to/fridanyvall/adding-css-to-a-vue-js-project-2kjk</guid>
      <description>&lt;p&gt;This is part of a series of articles starting with &lt;a href="https://redonion.se/en/working-with-css-in-vue-js-2/"&gt;“Working with CSS in Vue.js”&lt;/a&gt;. Focus in this article is on different ways of adding CSS to a Vue.js project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate Global Style Files
&lt;/h2&gt;

&lt;p&gt;If you prefer to keep your style files separate from the .vue files and their style tags, similar to a workflow without a framework, this can be achieved. Setting up the style files this way means the styling will be applied to your template sections, but not available in component style tags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For example, if you create a SCSS variable in your style file, it will not be recognized if you try to use it within a component style tag. To get global styles available in component style tags, see the &lt;a href="https://redonion.se/en/importing-style-files-to-component-style-tags-in-vue-js-2/"&gt;article about importing global styles in component style tags&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Set up your folder and file structure for your style files, for example src/styles/style.scss. If you use a structure with folders, _filename.scss and SCSS imports, this will work provided you have added SCSS support. (See the article &lt;a href="https://redonion.se/en/working-with-css-in-vue-js-2/"&gt;“Working with CSS in Vue.js”&lt;/a&gt; on adding SCSS support.)&lt;/p&gt;

&lt;p&gt;In your &lt;strong&gt;main.js&lt;/strong&gt; file, import your styles below the Vue import:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./styles/style.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Style Tags in Vue Files
&lt;/h2&gt;

&lt;p&gt;The default version of handling CSS is to write your styles in the vue file style tags.&lt;/p&gt;

&lt;p&gt;Styles written in this way are global. Styles defined here are available in the project’s other .vue files.&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="c"&gt;/*…write your styles here*/&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Scoped Styletags
&lt;/h2&gt;

&lt;p&gt;Scoping means adding data attributes to the different classes to keep them from leaking styles to one another. Scoping is similar to CSS Modules (read more about this in the article &lt;a href="https://redonion.se/en/css-modules-in-vue-js-2/"&gt;“CSS Modules in Vue.js”&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Add “scoped” to the component style tag in the &lt;strong&gt;.vue&lt;/strong&gt; file:&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 &lt;/span&gt;&lt;span class="na"&gt;scoped&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;/*add scoped styles here*/&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Style Cascading
&lt;/h3&gt;

&lt;p&gt;If a child component has a class name that is shared by its parent component, the parent component style will cascade to the child. Read more about nesting with the &lt;a href="https://vue-loader.vuejs.org/guide/scoped-css.html#child-component-root-elements"&gt;deep selector in the documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To target children of scoped parents, use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.parent &amp;gt;&amp;gt;&amp;gt; child { /*styles*/ }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Which syntax works depends on which preprocessor is used. Possible syntaxes are also:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.parent /deep/ child{ /*styles*/ }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.parent ::v-deep child{ /*styles*/ }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Read more about this in the &lt;a href="https://vue-loader.vuejs.org/guide/scoped-css.html"&gt;vue-loader documentation&lt;/a&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Setup
&lt;/h4&gt;

&lt;p&gt;The starting code for this article is created by the Vue CLI command tool version 3.3.0 and Vue.js version 2.6.10. Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>vue</category>
      <category>css</category>
    </item>
    <item>
      <title>Working with CSS in Vue.js</title>
      <dc:creator>Frida Nyvall</dc:creator>
      <pubDate>Fri, 01 Nov 2019 11:56:03 +0000</pubDate>
      <link>https://dev.to/fridanyvall/working-with-css-in-vue-js-ek5</link>
      <guid>https://dev.to/fridanyvall/working-with-css-in-vue-js-ek5</guid>
      <description>&lt;p&gt;Frameworks like Vue.js offer many different ways of adding CSS to a web project. Compared to working without a framework, the framework build process is however often more complex.&lt;/p&gt;

&lt;p&gt;This is the first part in a series of articles about working with CSS in Vue.js. The article is meant as a resource when it comes to the different ways of handling CSS in Vue.js. Main topics in this article are how to add preprocessors, autoprefixing and source maps.&lt;/p&gt;

&lt;p&gt;Note that for production, Vue.js creates a separate css file that is linked in the document head. Read more about the &lt;a href="https://vuejs.org/v2/guide/deployment.html"&gt;Vue.js build process here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Setup
&lt;/h2&gt;

&lt;p&gt;The starting code for this article is created by the Vue CLI command tool v3.3.0. Run &lt;code&gt;vue --version&lt;/code&gt; in the command prompt to see the version number. The Vue version is v.2.6.10. Run &lt;code&gt;npm list vue&lt;/code&gt; in the command prompt to see the Vue version.&lt;/p&gt;

&lt;p&gt;Note that both setup, plugins and framework are evolving. &lt;strong&gt;Changes will in time most certainly happen that make the techniques described in this post less relevant.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding SASS/SCSS to Vue.js
&lt;/h2&gt;

&lt;p&gt;To add support for sass/scss, install &lt;a href="https://github.com/webpack-contrib/sass-loader"&gt;sass-loader&lt;/a&gt; as a development dependency in the project folder by running: &lt;code&gt;npm install --save-dev sass-loader&lt;/code&gt; in the command prompt.&lt;/p&gt;

&lt;p&gt;Since sass-loader depends on &lt;a href="https://github.com/sass/node-sass"&gt;node-sass&lt;/a&gt;, you will have to install that as well, by running: &lt;code&gt;npm install --save-dev node-sass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Declare the language on styletags in &lt;strong&gt;.vue files&lt;/strong&gt;: &lt;code&gt;&amp;lt;style lang=”scss”&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Autoprefixing
&lt;/h2&gt;

&lt;p&gt;Settings for autoprefixing can be found in package.json, provided you have chosen to use a package.json file when creating the project with Vue CLI. Read more about how to configure the &lt;a href="https://github.com/browserslist/browserslist"&gt;browserlist settings&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Source Maps
&lt;/h2&gt;

&lt;p&gt;Add source maps by adding the following to the &lt;strong&gt;vue.config.js file&lt;/strong&gt;. If the vue.config.js file hasn’t yet been created, you can create one and save it directly in the project folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* vue.config.js */&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;sourceMap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Separate CSS file
&lt;/h2&gt;

&lt;p&gt;A separate CSS file will be created when the project is built for production, and linked in the document head.&lt;/p&gt;

&lt;p&gt;There is a setting in the vue.config.js file to also get a separate CSS file linked to the document head during development:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* vue.config.js */&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is set to false per default because setting it to true will interfere with hot module reloading. It is possible to set it to true, but then hot module reloading will not work.&lt;/p&gt;

&lt;p&gt;Setting it to true during development also means source maps will point to the compiled CSS file, and not to the real individual sources. If you need more control over the code splitting, Vue docs points to a plugin: &lt;a href="https://github.com/webpack-contrib/mini-css-extract-plugin"&gt;mini-css-extract-plugin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Read more about the &lt;code&gt;extract&lt;/code&gt; setting in the official &lt;a href="https://cli.vuejs.org/config/#css-extract"&gt;Vue documents&lt;/a&gt;, as well as the official &lt;a href="https://vuejs.org/v2/guide/deployment.html"&gt;instructions about deployment&lt;/a&gt;.&lt;/p&gt;




</description>
      <category>vue</category>
      <category>css</category>
    </item>
  </channel>
</rss>
