<?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: jboada</title>
    <description>The latest articles on DEV Community by jboada (@jboada).</description>
    <link>https://dev.to/jboada</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%2F636088%2F65910283-132e-4542-8157-e027a41b45b4.jpg</url>
      <title>DEV Community: jboada</title>
      <link>https://dev.to/jboada</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jboada"/>
    <language>en</language>
    <item>
      <title>Nuxt 3, UnoCSS, and Preset rem to px</title>
      <dc:creator>jboada</dc:creator>
      <pubDate>Sat, 18 Mar 2023 21:15:18 +0000</pubDate>
      <link>https://dev.to/jboada/nuxt-3-unocss-and-preset-rem-to-px-1gpo</link>
      <guid>https://dev.to/jboada/nuxt-3-unocss-and-preset-rem-to-px-1gpo</guid>
      <description>&lt;p&gt;Hello everybody! 👋&lt;/p&gt;

&lt;p&gt;This post is about generating px units by default in &lt;a href="https://github.com/unocss/unocss"&gt;UnoCSS&lt;/a&gt; when used in &lt;a href="https://nuxt.com/"&gt;Nuxt 3&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;u&gt;Preamble&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;Working on a project that uses Nuxt 3 and &lt;a href="https://vuetifyjs.com/"&gt;Vuetify&lt;/a&gt;; UnoCSS was the choice to handle the CSS tweaks/adjustments because it offers many advantages in the Utility Classes engines/frameworks universe.&lt;/p&gt;

&lt;h4&gt;
  
  
  UnoCSS and length units: a little bump in the road for the project?
&lt;/h4&gt;

&lt;p&gt;However, there was one thing: UnoCSS generates rem units for length units by default if a length unit is not indicated. What does this mean? Let's see the following example:&lt;/p&gt;

&lt;p&gt;If &lt;strong&gt;&lt;em&gt;m-2&lt;/em&gt;&lt;/strong&gt; is used (a length unit is &lt;strong&gt;&lt;em&gt;NOT&lt;/em&gt;&lt;/strong&gt; indicated) either as an attribute or CSS class in an HTML element, UnoCSS would generate the following CSS class:&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;.m-2&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;0.5rem&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;Now, if &lt;strong&gt;&lt;em&gt;m-8px&lt;/em&gt;&lt;/strong&gt; is used (a length unit &lt;strong&gt;&lt;em&gt;IS&lt;/em&gt;&lt;/strong&gt; indicated) either as an attribute or CSS class in an HTML element, UnoCSS would generate the following CSS class:&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;.m-8px&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;8px&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;
  
  
  &lt;u&gt;Solution&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;To achieve the goal of generating px units by default in UnoCSS, the &lt;a href="https://github.com/unocss/unocss/tree/main/packages/preset-rem-to-px"&gt;@unocss/preset-rem-to-px&lt;/a&gt; package was used because its purpose is to convert the rem units to px units.&lt;/p&gt;




&lt;h4&gt;
  
  
  Installation guide for the @unocss/preset-rem-to-px package in Nuxt 3
&lt;/h4&gt;

&lt;p&gt;Follow this installation guide to install and configure the @unocss/preset-rem-to-px package in Nuxt 3:&lt;/p&gt;

&lt;p&gt;Step 1. Install @unocss/preset-rem-to-px&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; @unocss/preset-rem-to-px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2. Add the following references to the &lt;em&gt;&lt;strong&gt;nuxt.config.ts&lt;/strong&gt;&lt;/em&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;presetUno&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@unocss/preset-uno&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;presetRemToPx&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@unocss/preset-rem-to-px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;presetAttributify&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@unocss/preset-attributify&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;presetIcons&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@unocss/preset-icons&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;Step 3. Add the &lt;em&gt;&lt;strong&gt;unocss config&lt;/strong&gt;&lt;/em&gt; to the &lt;em&gt;&lt;strong&gt;nuxt.config.ts&lt;/strong&gt;&lt;/em&gt; file this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineNuxtConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="na"&gt;unocss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="nf"&gt;presetUno&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="nf"&gt;presetAttributify&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="nf"&gt;presetIcons&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="nf"&gt;presetRemToPx&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="na"&gt;shortcuts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
        &lt;span class="na"&gt;rules&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;You can look at the code in this repo &lt;a href="https://github.com/jboada/nuxt3-vuetify-unocss"&gt;nuxt3-vuetify-unocss&lt;/a&gt; at GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;By using the @unocss/preset-rem-to-px package we can achieve the goal of generating px units by default in UnoCSS.&lt;/p&gt;




&lt;p&gt;I hope you enjoy this post and find it helpful. If you have any questions or you want to point out some errors, I appreciate it, and I'll do my best to follow up. &lt;/p&gt;

&lt;p&gt;If you enjoy it and would like to support me, consider giving a like and sharing it on your favorite social networks.&lt;/p&gt;

&lt;p&gt;Stay safe and see you soon! 😎&lt;/p&gt;

</description>
      <category>unocss</category>
      <category>nuxt</category>
      <category>nuxt3</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
