<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Tech Notes]]></title><description><![CDATA[Tech Notes]]></description><link>https://fieldsmarshall.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 19:23:27 GMT</lastBuildDate><atom:link href="https://fieldsmarshall.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Simple Encryption for .env files in Node.js]]></title><description><![CDATA[Encrypting your .env file in a Node.js project helps protect sensitive environment variables, such as API keys and database URLs, especially when sharing code or deploying applications. This approach allows you to keep these details secure while stil...]]></description><link>https://fieldsmarshall.com/simple-encryption-for-env-file-in-nodejs</link><guid isPermaLink="true">https://fieldsmarshall.com/simple-encryption-for-env-file-in-nodejs</guid><category><![CDATA[npm]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[encryption]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Thu, 07 Nov 2024 00:37:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/FnA5pAzqhMM/upload/1d126f5d9e4d5a189a706dc78f8492da.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Encrypting your <code>.env</code> file in a Node.js project helps protect sensitive environment variables, such as API keys and database URLs, especially when sharing code or deploying applications. This approach allows you to keep these details secure while still allowing others to pull down and build the project from scratch, as they can decrypt the file with a provided password.</p>
<p>Even a simple password can add a basic layer of protection if full security isn't a priority, making this method an easy yet effective way to prevent accidental exposure of sensitive information.</p>
<p>If you want to encrypt you .env files with a password with the crypto module built in to node then use this script and follow this process.</p>
<h3 id="heading-postinstalljs">postinstall.js</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>);

<span class="hljs-comment">// Check if running in production based on CONTEXT or VERCEL_ENV</span>
<span class="hljs-keyword">if</span> (
  process.env.VERCEL_ENV?.toLowerCase() === <span class="hljs-string">'production'</span> ||
  process.env.CONTEXT?.toLowerCase() === <span class="hljs-string">'production'</span>
) {
  <span class="hljs-built_in">console</span>.log(
    <span class="hljs-string">'Production environment detected. Skipping encryption/decryption.'</span>
  )
  process.exit(<span class="hljs-number">0</span>) <span class="hljs-comment">// Exit the script without running any further</span>
}

<span class="hljs-comment">// Continue with the rest of your script if not in production</span>
<span class="hljs-built_in">console</span>.log(
  <span class="hljs-string">'Not in production environment. Proceeding with encryption/decryption.'</span>
)

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'process.env'</span>)
<span class="hljs-built_in">console</span>.log(process.env)

<span class="hljs-comment">// Helper function to prompt for a password</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">promptPassword</span>(<span class="hljs-params">question</span>) </span>{
  <span class="hljs-keyword">const</span> rl = readline.createInterface({
    <span class="hljs-attr">input</span>: process.stdin,
    <span class="hljs-attr">output</span>: process.stdout,
    <span class="hljs-attr">terminal</span>: <span class="hljs-literal">true</span>
  });

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    rl.question(question, <span class="hljs-function">(<span class="hljs-params">password</span>) =&gt;</span> {
      rl.close();
      resolve(password);
    });
  });
}

<span class="hljs-comment">// Function to encrypt the .env file</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">encryptFile</span>(<span class="hljs-params">password</span>) </span>{
  <span class="hljs-keyword">const</span> envContent = fs.readFileSync(<span class="hljs-string">'.env'</span>, <span class="hljs-string">'utf8'</span>);
  <span class="hljs-keyword">const</span> iv = crypto.randomBytes(<span class="hljs-number">16</span>);
  <span class="hljs-keyword">const</span> key = crypto.scryptSync(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">32</span>);
  <span class="hljs-keyword">const</span> cipher = crypto.createCipheriv(<span class="hljs-string">'aes-256-cbc'</span>, key, iv);
  <span class="hljs-keyword">let</span> encrypted = cipher.update(envContent, <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'hex'</span>);
  encrypted += cipher.final(<span class="hljs-string">'hex'</span>);

  <span class="hljs-keyword">const</span> encryptedContent = <span class="hljs-string">`<span class="hljs-subst">${iv.toString(<span class="hljs-string">'hex'</span>)}</span>:<span class="hljs-subst">${encrypted}</span>`</span>;
  fs.writeFileSync(<span class="hljs-string">'.env.enc'</span>, encryptedContent);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'.env file encrypted and saved to .env.enc'</span>);
}

<span class="hljs-comment">// Function to decrypt the .env.enc file</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decryptFile</span>(<span class="hljs-params">password</span>) </span>{
  <span class="hljs-keyword">const</span> encryptedContent = fs.readFileSync(<span class="hljs-string">'.env.enc'</span>, <span class="hljs-string">'utf8'</span>);
  <span class="hljs-keyword">const</span> [ivHex, encryptedData] = encryptedContent.split(<span class="hljs-string">':'</span>);
  <span class="hljs-keyword">const</span> key = crypto.scryptSync(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">32</span>);
  <span class="hljs-keyword">const</span> decipher = crypto.createDecipheriv(<span class="hljs-string">'aes-256-cbc'</span>, key, Buffer.from(ivHex, <span class="hljs-string">'hex'</span>));

  <span class="hljs-keyword">let</span> decrypted = decipher.update(encryptedData, <span class="hljs-string">'hex'</span>, <span class="hljs-string">'utf8'</span>);
  decrypted += decipher.final(<span class="hljs-string">'utf8'</span>);
  fs.writeFileSync(<span class="hljs-string">'.env'</span>, decrypted);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'.env file restored from .env.enc'</span>);
}

<span class="hljs-comment">// Main logic to check file existence and perform encryption/decryption</span>
(<span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">const</span> envExists = fs.existsSync(<span class="hljs-string">'.env'</span>);
  <span class="hljs-keyword">const</span> envEncExists = fs.existsSync(<span class="hljs-string">'.env.enc'</span>);

  <span class="hljs-keyword">if</span> (envExists &amp;&amp; !envEncExists) {
    <span class="hljs-comment">// If .env exists but .env.enc does not, encrypt .env</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'.env exists but .env.enc does not. Encrypting .env...'</span>);
    <span class="hljs-keyword">const</span> password = <span class="hljs-keyword">await</span> promptPassword(<span class="hljs-string">'Enter password to encrypt .env: '</span>);
    <span class="hljs-keyword">await</span> encryptFile(password);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (envEncExists &amp;&amp; !envExists) {
    <span class="hljs-comment">// If .env.enc exists but .env does not, decrypt .env.enc</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'.env.enc exists but .env does not. Decrypting .env.enc...'</span>);
    <span class="hljs-keyword">const</span> password = <span class="hljs-keyword">await</span> promptPassword(<span class="hljs-string">'Enter password to decrypt .env.enc: '</span>);
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">await</span> decryptFile(password);
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Decryption failed. Please check the password and try again.'</span>);
    }
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'No action needed. Either both or neither .env/.env.enc files exist.'</span>);
  }
})();
</code></pre>
<p>you would then modify the package.json file to include 1 line</p>
<pre><code class="lang-javascript"><span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"build"</span>: <span class="hljs-string">"hugo &amp;&amp; node postinstall.js"</span>,
    <span class="hljs-string">"dev"</span>: <span class="hljs-string">"npm run server"</span>,
    <span class="hljs-string">"server"</span>: <span class="hljs-string">"node postinstall.js &amp;&amp; hugo server"</span>,
    <span class="hljs-string">"postinstall"</span>: <span class="hljs-string">"node postinstall.js"</span>
  }
</code></pre>
<p>now you can check in your code with your private information encrypted.</p>
<h3 id="heading-how-it-works">How it works</h3>
<p>This <code>postinstall.js</code> script helps keep sensitive information in your project safe by automatically <strong>encrypting</strong> and <strong>decrypting</strong> environment variables stored in a <code>.env</code> file.</p>
<h3 id="heading-heres-how-it-works">Here’s How It Works:</h3>
<ol>
<li><p><strong>Encryption and Decryption in One Place</strong>: The script has both functions needed to protect your <code>.env</code> file — it can either turn it into a secure, unreadable file called <code>.env.enc</code> (encryption) or turn <code>.env.enc</code> back into a readable <code>.env</code> file (decryption).</p>
</li>
<li><p><strong>Automatic Checks</strong>:</p>
<ul>
<li><p>If it finds only <code>.env</code>, it knows you need to encrypt it and will prompt you for a password. This password "locks" the file so others can’t easily read it.</p>
</li>
<li><p>If it finds only <code>.env.enc</code>, it knows you want to decrypt it and will prompt for the password again to "unlock" it.</p>
</li>
</ul>
</li>
<li><p><strong>Password Prompt</strong>: The script asks for a password whenever it encrypts or decrypts, so you keep control over your sensitive information.</p>
</li>
<li><p><strong>Integrated into Your Project</strong>: By adding this to your project’s <code>postinstall</code> script, it will run automatically after installing packages. This way, your environment files stay safe without extra manual steps.</p>
</li>
</ol>
<p>In short, the script handles everything you need to protect your environment variables with simple commands and prompts!</p>
<p>By implementing this method, developers can enhance the security of their environment variables, safeguarding sensitive information throughout the development cycle.</p>
]]></content:encoded></item><item><title><![CDATA[Typography on the Web using CSS]]></title><description><![CDATA[Font Families and Font Stacking
In CSS, fonts are categorized into five distinct families:

Serif fonts: These fonts are characterized by tiny strokes at the terminations of each letter. They exude a feeling of sophistication and classic elegance.

S...]]></description><link>https://fieldsmarshall.com/typography-on-the-web-using-css</link><guid isPermaLink="true">https://fieldsmarshall.com/typography-on-the-web-using-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[typography]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Fri, 13 Oct 2023 23:04:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697217213738/016d6c6d-c5aa-45fb-8aa5-2e3300661e51.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-font-families-and-font-stacking">Font Families and Font Stacking</h3>
<p>In CSS, fonts are categorized into five distinct families:</p>
<ol>
<li><p><strong>Serif fonts</strong>: These fonts are characterized by tiny strokes at the terminations of each letter. They exude a feeling of sophistication and classic elegance.</p>
</li>
<li><p><strong>Sans-serif fonts</strong>: Noted for their smooth lines without the additional strokes, these fonts give off a contemporary and minimalist vibe.</p>
</li>
<li><p><strong>Monospace fonts</strong>: In this font family, every letter has a consistent fixed width, offering a structured and machine-like aesthetic.</p>
</li>
<li><p><strong>Cursive fonts</strong>: These fonts are designed to resemble fluid human handwriting, giving a personal touch.</p>
</li>
<li><p><strong>Fantasy fonts</strong>: As the name suggests, these are ornamental and whimsical fonts that add a playful element to the design.</p>
</li>
</ol>
<p>Usually, serif fonts are preferred for prominent text such as headlines, due to their distinctiveness. On the other hand, sans-serif fonts are often chosen for the bulk of website content because they are simpler and easier on the eyes for reading. When showcasing computer code, monospace fonts are the go-to choice because of their consistent character width.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">There are <strong>5 distinct font-family types in css</strong>: serif, sans-serif, monospace, cursive and fantasy</div>
</div>

<p>A <strong>font stack</strong> or font stacking is a list of fonts in the CSS font-family declaration. The fonts are listed in order of preference that you would like them to appear in case of a problem, such as a font not loading.</p>
<p>Here's an example:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Helvetica Neue"</span>, Helvetica, Arial, sans-serif;
}
</code></pre>
<p>In the above example:</p>
<ol>
<li><p>The browser will first try to use "Helvetica Neue".</p>
</li>
<li><p>If "Helvetica Neue" is not available, it will try to use "Helvetica".</p>
</li>
<li><p>If "Helvetica" is not available either, it will try "Arial".</p>
</li>
<li><p>If none of the above fonts are available, it will default to the generic <code>sans-serif</code> font available on the system.</p>
</li>
</ol>
<p>Font stacking ensures that the webpage appears as consistent as possible across different devices and browsers by providing fallback options.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697230140306/db63fef6-c0a3-46dc-b964-1012c7fd2e32.jpeg" alt class="image--center mx-auto" /></p>
<h3 id="heading-font-pairing">Font Pairing</h3>
<p>Font pairing is the strategy of merging two or more fonts in a design for harmony and readability. Often, contrasting fonts, such as a serif for headings (H1-H6) and a more legible sans-serif for body text, are chosen to distinguish different textual elements effectively. This is more art and can take some experimenting but think serif for the heading and sans-serif for normal text.</p>
<h3 id="heading-leading-kerning-and-tracking">Leading, Kerning and Tracking</h3>
<p>The term "leading" in typography is pronounced like "ledding" (rhymes with "wedding"). It originates from the days of hand-typesetting when strips of lead were placed between lines of type to increase the vertical distance between them, hence increasing the line spacing. The use of these lead strips allowed typographers to adjust the visual space between lines of text, making it easier to read and more aesthetically pleasing.</p>
<p>In modern typography, especially in digital design, "leading" refers to the distance between the baselines of consecutive lines of text. Even though we no longer use physical strips of lead in typesetting, the term has persisted and is now used to describe line spacing in both print and digital media. Adjusting the leading is crucial for ensuring readability and achieving the desired visual effect in textual content.</p>
<h3 id="heading-quick-definitions">Quick Definitions</h3>
<ol>
<li><p><strong>Leading</strong>: Refers to the vertical spacing between lines of text.</p>
</li>
<li><p><strong>Kerning</strong>: Adjusts the space between specific letter pairs to improve visual appeal.</p>
</li>
<li><p><strong>Tracking</strong>: Modifies the uniform spacing between all characters in a block of text.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697233803308/334ce17b-e982-4631-a8bd-b37136e4fa56.jpeg" alt="leading old school style" class="image--center mx-auto" /></p>
<p><strong>Relevant CSS Property for leading</strong>: <code>line-height</code></p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">18px</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.5</span>;  <span class="hljs-comment">/* 1.5 times the font size */</span>
}
</code></pre>
<p>This means the space from one baseline to the next will be 1.5 times the size of the current font. While you can use rems and pixels as a unit for line-height, it best to keep in unitless.</p>
<h3 id="heading-kerning-vs-tracking">Kerning vs Tracking</h3>
<p><a target="_blank" href="https://helpx.adobe.com/indesign/using/kerning-tracking.html">According to Adobe</a></p>
<blockquote>
<p><strong>Kerning is the process of adding or subtracting space between specific pairs of characters.</strong> <strong>Tracking is the process of loosening or tightening a block of text</strong>.</p>
</blockquote>
<p>Looking more into this around the Internet, it seems most people get both terms confused and use them interchangeably perhaps in part because with CSS we have limited control of the space between characters.</p>
<p><strong>The letter-spacing css property</strong></p>
<ul>
<li><p><strong>Description</strong>: letter-spacing specifies the spacing between individual characters in text.</p>
</li>
<li><p><strong>Example</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">h1</span> {
    <span class="hljs-attribute">letter-spacing</span>: <span class="hljs-number">2px</span>;
  }
</code></pre>
<p>  This will add a spacing of 2 pixels between each character in <code>&lt;h1&gt;</code> elements.</p>
</li>
</ul>
<p><strong>Quick Summary CSS Spacing Adjustments</strong></p>
<p>In conclusion, there are two primary methods to modify character spacing:</p>
<ol>
<li><p>Adjust the horizontal spacing between characters with the <code>letter-spacing</code> property.</p>
</li>
<li><p>Modify the vertical spacing between lines using the <code>line-height</code> property.</p>
</li>
</ol>
<h3 id="heading-quiz-on-kerning-tracking-and-leading">Quiz on Kerning, Tracking and Leading</h3>
<iframe src="https://quizlet.com/588363372/flashcards/embed?i=5ertiu&amp;x=1jj1" height="500" width="100%" style="border:0"></iframe>

<p><strong>Web Safe Fonts vs. Web Fonts: Understanding the Distinction</strong></p>
<p>Arial, Verdana, Georgia, Times New Roman, and Courier are all examples of "web safe fonts". These fonts are pre-installed on almost every computer, device, and operating system.</p>
<p>Going by a slightly different name; <strong><em>Web fonts</em></strong> are hosted online and are downloaded by your browser when displaying a webpage. Web fonts, are either uploaded to your site for self-hosting or you could rely on a third-party services such as Google Fonts or Adobe Fonts.</p>
<h3 id="heading-web-font-example-google-fonts">Web font example - Google Fonts</h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://fonts.googleapis.com/css2?family=Roboto&amp;display=swap"</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span>&gt;</span>
</code></pre>
<p><strong>Use the Font in Your CSS</strong>:</p>
<ul>
<li>Now, in your CSS, you can specify "Roboto" as the font-family for any element.</li>
</ul>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">font-family</span>: <span class="hljs-string">'Roboto'</span>, sans-serif;
}
</code></pre>
<p>The result is that when someone visits your website, their browser will download the "Roboto" font from Google Fonts and display the text in that font. This ensures that even if the visitor doesn't have "Roboto" installed on their machine, they'll still see the text in the desired font.</p>
<h3 id="heading-css-commands-for-common-text-formatting">CSS commands for common text formatting</h3>
<p>Bolding, italizicing, and underling are the three most common ways to manipulate text on the web. Here is a quick run down of how these operation work in CSS.</p>
<ol>
<li><p><strong>Bold</strong>:</p>
<ul>
<li><p><strong>CSS Property</strong>: <code>font-weight</code></p>
</li>
<li><p><strong>Values</strong>:</p>
<ul>
<li><p><code>normal</code>: Default weight (often equivalent to 400).</p>
</li>
<li><p><code>bold</code>: Bold weight (often equivalent to 700).</p>
</li>
<li><p>Numeric values ranging from <code>100</code> (thinnest) to <code>900</code> (thickest). Not every font will have all weights, but commonly used weights include <code>300</code> (light), <code>400</code> (regular/normal), <code>500</code> (medium), <code>700</code> (bold), and <code>900</code> (extra bold).</p>
</li>
</ul>
</li>
<li><p><strong>Example</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span><span class="hljs-selector-class">.light-text</span> {
      <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">300</span>;
  }
  <span class="hljs-selector-tag">p</span><span class="hljs-selector-class">.bold-text</span> {
      <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">700</span>;
  }
</code></pre>
<p>  The first rule will render the text in <code>&lt;p class="light-text"&gt;</code> with a light weight, and the second rule will render the text in <code>&lt;p class="bold-text"&gt;</code> as bold.</p>
</li>
</ul>
</li>
<li><p><strong>Italic</strong>:</p>
<ul>
<li><p><strong>CSS Property</strong>: <code>font-style</code></p>
</li>
<li><p><strong>Value</strong>: <code>italic</code></p>
</li>
<li><p><strong>Example</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
      <span class="hljs-attribute">font-style</span>: italic;
  }
</code></pre>
<p>  This will make the text inside the <code>&lt;p&gt;</code> element italicized.</p>
</li>
</ul>
</li>
<li><p><strong>Underline</strong>:</p>
<ul>
<li><p><strong>CSS Property</strong>: <code>text-decoration</code></p>
</li>
<li><p><strong>Value</strong>: <code>underline</code></p>
</li>
<li><p><strong>Example</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
      <span class="hljs-attribute">text-decoration</span>: underline;
  }
</code></pre>
<p>  This will underline the text inside the <code>&lt;p&gt;</code> element.</p>
</li>
</ul>
</li>
</ol>
<p>When using numeric values for <code>font-weight</code>, it's important to ensure that the specific font you're using supports the desired weight. Some fonts might only offer a limited range of weights.</p>
<iframe src="https://quizlet.com/839007916/flashcards/embed?i=5ertiu&amp;x=1jj1" height="500" width="100%" style="border:0"></iframe>

<h3 id="heading-andltstrongandgt-and-andltemandgt-html-tags-explained">&lt;strong&gt; and &lt;em&gt; HTML tags explained</h3>
<p>In the early days of web design, visual formatting was often mixed with content. Tags like (for bold) and (for italic) were used. However, as the web evolved, there was a shift towards separating content from its presentation. This led to the development of tags that conveyed the semantic meaning or intent behind the formatting, rather than just the visual style.</p>
<p><strong>&lt;strong&gt; vs. &lt;b&gt;</strong></p>
<p>The &lt;strong&gt; tag was introduced to emphasize the importance of a piece of text, not just to make it bold. While visually, it often appears similar to the &lt;b&gt; tag (bold), the semantic meaning is different. implies that the enclosed text has strong importance within its context.</p>
<p><strong>&lt;em&gt; vs. &lt;i&gt;</strong></p>
<p>Similarly, the &lt;em&gt; tag was designed to indicate emphasis, whereas the &lt;i&gt; tag was purely for italicizing text. The &lt;em&gt; tag conveys that the text should be emphasized in its context, often making it stand out for emphasis.</p>
<h3 id="heading-text-formatting-with-text-transform"><strong>Text Formatting with</strong> <code>text-transform</code></h3>
<p>The <code>text-transform</code> property allows us to adjust the casing of our text:</p>
<ul>
<li><p><strong>For UPPERCASE</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">text-transform</span>: <span class="hljs-selector-tag">uppercase</span>;
</code></pre>
</li>
<li><p><strong>To Capitalize Initial Letters</strong>:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">text-transform</span>: <span class="hljs-selector-tag">capitalize</span>;
</code></pre>
</li>
</ul>
<p>Why opt for <code>text-transform</code> instead of editing the HTML directly? By using CSS, we maintain the original text casing. Should we decide to revert from ALL CAPS in the future, we'd face a tedious task if we modified the HTML—having to edit each occurrence. With CSS, a single declaration change does the trick!</p>
<h3 id="heading-adjusting-text-alignment-with-text-align"><strong>Adjusting Text Alignment with text-align</strong></h3>
<p>How do we handle the alignment of our text?</p>
<p>The <code>text-align</code> property lets us set horizontal positioning for characters. Here's how it's implemented in CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">p</span><span class="hljs-selector-class">.left</span> {
  <span class="hljs-attribute">text-align</span>: left;
}
<span class="hljs-selector-tag">p</span><span class="hljs-selector-class">.right</span> {
  <span class="hljs-attribute">text-align</span>: right;
}
<span class="hljs-selector-tag">p</span><span class="hljs-selector-class">.center</span> {
  <span class="hljs-attribute">text-align</span>: center;
}
</code></pre>
<p>Using this CSS, the alignment of paragraphs in HTML would be:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"left"</span>&gt;</span>
  This paragraph defaults to "left" alignment.
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"right"</span>&gt;</span>
  Contrarily, this one shifts to the right, reversing the default.
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"center"</span>&gt;</span>
  And this centers the content.
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>Although <code>text-align</code> can be used for aligning elements like images, it's typically best suited for text. For aligning non-text elements, other CSS techniques are more appropriate.</p>
<p><strong>Final Test on text-transform, text-align and semantics meanings</strong></p>
<iframe src="https://quizlet.com/839012167/flashcards/embed?i=5ertiu&amp;x=1jj1" height="500" width="100%" style="border:0"></iframe>

<h3 id="heading-relevant-links">Relevant Links</h3>
<ul>
<li><p>Game to learn about Kerning - <a target="_blank" href="https://type.method.ac/#">https://type.method.ac/#</a></p>
</li>
<li><p>Abode on Kerning and Tracking - <a target="_blank" href="https://helpx.adobe.com/indesign/using/kerning-tracking.html">https://helpx.adobe.com/indesign/using/kerning-tracking.html</a></p>
</li>
<li><p><a target="_blank" href="https://fonts.google.com/">https://fonts.google.com/</a> (Web Fonts)</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Units in CSS]]></title><description><![CDATA[CSS has many different units for measuring and expresssing length but in an effort to be quick and accurate - they fall into two categories: Absolute and Relative.

Absolute Lengths - where the units are fixed

px for pixels ( Note: pixels are relati...]]></description><link>https://fieldsmarshall.com/units-in-css</link><guid isPermaLink="true">https://fieldsmarshall.com/units-in-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[css units]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Thu, 12 Oct 2023 18:35:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697132135255/5a0bdead-c407-4464-b22d-c1a26151c8ad.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>CSS has many different units for measuring and expresssing length but in an effort to be quick and accurate - they fall into two categories: Absolute and Relative.</p>
<ul>
<li><p>Absolute Lengths - where the units are fixed</p>
<ul>
<li><p><code>px</code> for pixels ( Note: pixels are relative to the viewing device. )</p>
</li>
<li><p>cm for centimeters</p>
</li>
</ul>
</li>
<li><p>Relative Lengths - where units are expressed relative to another length property.</p>
<ul>
<li><p>em - relative to the font-size of the element itself</p>
</li>
<li><p>rem - relative to the root element.</p>
</li>
<li><p>vw - relative to 1% of width of viewport ( browser window size )</p>
</li>
<li><p>vh - relative to 1% of height of viewport (browser window size)</p>
</li>
<li><p>vmin - relative to 1% of the smaller of the viewport's width and height. Look at both the width and the height and use whichever is shorter.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-vmin-in-more-detail">vmin In More Detail</h3>
<p><code>vmin</code> is a unit in CSS that is used to specify a size in terms of the viewport dimensions. It represents the smaller of <code>vw</code> (viewport width) and <code>vh</code> (viewport height).</p>
<p>Here's how it works:</p>
<ul>
<li>1 <code>vmin</code> is equivalent to 1% of the smaller of the viewport's width and height.</li>
</ul>
<p>For example, if the viewport (browser window) is 800px wide and 600px tall, then:</p>
<ul>
<li><p>1 <code>vw</code> (1% of viewport width) = 8px</p>
</li>
<li><p>1 <code>vh</code> (1% of viewport height) = 6px</p>
</li>
</ul>
<p>In this case, <code>1vmin</code> would be equal to 6px since it's the smaller value between <code>vw</code> and <code>vh</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697134297420/652163ee-6baf-48a9-b01a-16c5e5d46255.png" alt="Rainbow Cat" class="image--center mx-auto" /></p>
<h3 id="heading-practical-examples">Practical Examples</h3>
<ol>
<li><p><strong>Text resizing based on viewport</strong>: If you want text to resize based on the viewport dimensions but you always want to use the smaller value, you can use <code>vmin</code>.</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">h1</span> {
     <span class="hljs-attribute">font-size</span>: <span class="hljs-number">10vmin</span>;  <span class="hljs-comment">/* The font size will be 10% of the smaller viewport dimension */</span>
 }
</code></pre>
<p> So, if your viewport was 1000px by 700px, the font size would be 70px (since 700px is the smaller dimension and 10% of 700 is 70).</p>
</li>
<li><p><strong>Creating a responsive square</strong>: If you want to create a square that maintains its shape (equal width and height) while resizing based on the viewport dimensions, you can use <code>vmin</code> for both width and height.</p>
<pre><code class="lang-css"> <span class="hljs-selector-class">.responsive-square</span> {
     <span class="hljs-attribute">width</span>: <span class="hljs-number">50vmin</span>;
     <span class="hljs-attribute">height</span>: <span class="hljs-number">50vmin</span>;
     <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ff5733</span>;
 }
</code></pre>
<p> In a 1200px by 800px viewport, the square would be 400px by 400px.</p>
</li>
<li><p><strong>Responsive background icon</strong>: If you have a background icon and you want it to resize with the viewport, but never get too big relative to the screen size, you can use <code>vmin</code>:</p>
<pre><code class="lang-css"> <span class="hljs-selector-class">.icon-background</span> {
     <span class="hljs-attribute">background</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'icon.png'</span>) no-repeat center/<span class="hljs-number">20vmin</span>;
 }
</code></pre>
<p> Here, the background icon size will be 20% of the smaller viewport dimension.</p>
</li>
</ol>
<p>In general, <code>vmin</code> is useful in responsive designs where you want to ensure that elements resize proportionally to the smallest viewport dimension. Caveat: it's important to be cautious as using <code>vmin</code> extensively might lead to design issues on extreme aspect ratios (very wide or very tall viewports).</p>
<h3 id="heading-rem-vs-em-units">Rem vs Em Units</h3>
<p>Both <code>rem</code> and <code>em</code> are relative font-size units in CSS. Here's a brief overview of each:</p>
<ol>
<li><p><strong>em</strong>:</p>
<ul>
<li><p>The value of <code>1em</code> is equal to the font-size of the element on which it's used.</p>
</li>
<li><p>If you use <code>em</code> on nested elements, it can compound, as <code>em</code> will be relative to the font-size of its direct parent.</p>
</li>
</ul>
</li>
</ol>
<p>    Example:</p>
<pre><code class="lang-css">    <span class="hljs-selector-tag">div</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>; 
    }

    <span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2em</span>;  <span class="hljs-comment">/* This will be 32px, as it's 2 times the font-size of its parent (div) */</span>
    }
</code></pre>
<ol>
<li><p><strong>rem</strong>:</p>
<ul>
<li><p>Stands for "Root em".</p>
</li>
<li><p>The value of <code>1rem</code> is always equal to the font-size of the root element, usually the <code>&lt;html&gt;</code> element.</p>
</li>
<li><p>It doesn't compound the way <code>em</code> does, because it always refers to the root element's font size, not any parent element.</p>
</li>
</ul>
</li>
</ol>
<p>    Example:</p>
<pre><code class="lang-css">    <span class="hljs-selector-tag">html</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
    }

    <span class="hljs-selector-tag">div</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;  <span class="hljs-comment">/* This will always be 32px regardless of its parent's font-size, because it's based on the root font-size */</span>
    }
</code></pre>
<h3 id="heading-which-is-preferred-and-why">Which is preferred and why?</h3>
<p>Both units have their places in design and development, but <code>rem</code> has become more popular for a few reasons:</p>
<ol>
<li><p><strong>Consistency</strong>: As <code>rem</code> is always relative to the root element's font size, it offers a consistent and predictable scaling across the website. With <code>em</code>, it's easy to accidentally compound sizes in nested elements, leading to unexpected results.</p>
</li>
<li><p><strong>Ease of Global Resizing</strong>: By changing the font-size on the root element, you can easily scale the entire website's sizing (when <code>rem</code> units are used), which can be handy for theming or accessibility features.</p>
</li>
<li><p><strong>Clarity</strong>: When other developers inspect or collaborate on the code, using <code>rem</code> offers clarity as they can always refer to the root's font-size to understand the unit's actual pixel value.</p>
</li>
</ol>
<p>However, <code>em</code> is still valuable in certain scenarios:</p>
<ol>
<li><p><strong>Component-Level Scaling</strong>: If you're creating a reusable component (like a widget or a module) and you want its inner elements to scale based on the component's base size, <code>em</code> is very handy. Adjusting the component's font-size will proportionally scale all its inner elements set with <code>em</code> units.</p>
</li>
<li><p><strong>Vertical Rhythm and Typography</strong>: <code>em</code> can be useful for maintaining vertical rhythm in typography or setting spacing related directly to font size, like margins or paddings.</p>
</li>
</ol>
<p>In conclusion, while both <code>rem</code> and <code>em</code> have their use cases, most developers prefer <code>rem</code> for general layout and component sizing because of its consistency and predictability. However, its good to know the ins and outs of both rem and em.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697134943631/d75ef2d3-c807-46e3-ae47-19f220bd5192.jpeg" alt class="image--center mx-auto" /></p>
<h3 id="heading-percentage-unit-in-css">Percentage Unit in CSS</h3>
<p>In CSS, the percentage (<code>%</code>) unit is a relative unit that represents a portion of a particular reference value. The meaning of the percentage value and its reference point can vary depending on the property with which it's used.</p>
<p>Here's how the percentage unit is commonly interpreted with various CSS properties:</p>
<ol>
<li><p><strong>Width &amp; Height</strong>:</p>
<ul>
<li><p>The percentage value is relative to the width or height of the parent element.</p>
<pre><code class="lang-css">  <span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
  }

  <span class="hljs-selector-class">.child</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;  <span class="hljs-comment">/* This will be 150px */</span>
      <span class="hljs-attribute">height</span>: <span class="hljs-number">50%</span>; <span class="hljs-comment">/* This will be 100px */</span>
  }
</code></pre>
</li>
</ul>
</li>
<li><p><strong>Padding &amp; Margin</strong>:</p>
<ul>
<li><p>For <code>padding-top</code>, <code>padding-bottom</code>, <code>margin-top</code>, and <code>margin-bottom</code>: The percentage is relative to the width of the containing element.</p>
</li>
<li><p>For <code>padding-left</code>, <code>padding-right</code>, <code>margin-left</code>, and <code>margin-right</code>: The percentage is also relative to the width of the containing element.</p>
<p>  <em>This behavior might seem counterintuitive, especially for top and bottom paddings/margins, but it ensures consistent spacing in responsive designs.</em></p>
<pre><code class="lang-css">  <span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
  }

  <span class="hljs-selector-class">.child</span> {
      <span class="hljs-attribute">padding-top</span>: <span class="hljs-number">10%</span>;  <span class="hljs-comment">/* This will be 30px */</span>
  }
</code></pre>
</li>
</ul>
</li>
<li><p><strong>Font-Size</strong>:</p>
<ul>
<li><p>The percentage value is relative to the parent element's font size.</p>
<pre><code class="lang-css">  <span class="hljs-selector-class">.parent</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  }

  <span class="hljs-selector-class">.child</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">150%</span>;  <span class="hljs-comment">/* This will be 24px (150% of 16px) */</span>
  }
</code></pre>
</li>
</ul>
</li>
<li><p><strong>Line-Height</strong>:</p>
<ul>
<li><p>When used with <code>line-height</code>, the percentage is relative to the current element's font size.</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
      <span class="hljs-attribute">line-height</span>: <span class="hljs-number">150%</span>;  <span class="hljs-comment">/* This will be 24px (150% of 16px) */</span>
  }
</code></pre>
</li>
</ul>
</li>
<li><p><strong>Background Position</strong>:</p>
<ul>
<li><p>Specifies the position of a background image. A value of <code>0%</code> aligns the top or left side of the image with the corresponding top or left side of the container. <code>100%</code> aligns the bottom or right side of the image with the corresponding bottom or right side of the container.</p>
</li>
<li><p>See more detail here --&gt; <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-position">https://developer.mozilla.org/en-US/docs/Web/CSS/background-position</a></p>
</li>
</ul>
</li>
<li><p><strong>Others</strong>:</p>
<ul>
<li>Percentage values in other properties, such as <code>background-size</code> or <code>transform</code>, have their specific reference points, and you'd need to refer to the respective property definition to understand the behavior.</li>
</ul>
</li>
</ol>
<h3 id="heading-advantages-of-using-percentages">Advantages of using percentages:</h3>
<ol>
<li><p><strong>Responsiveness</strong>: Percentage units are inherently responsive. They adapt based on the size of their reference, which often is a parent or containing element.</p>
</li>
<li><p><strong>Fluid Layouts</strong>: They're essential for creating fluid grid systems, especially before the widespread use of CSS Grid and Flexbox.</p>
</li>
<li><p><strong>Scalability</strong>: Makes it easier to scale elements uniformly, like in zooming or theming scenarios.</p>
</li>
</ol>
<p>When designing with percentages, it's crucial to understand the context and the reference point from which the percentage derives its actual computed value.</p>
<h3 id="heading-helpful-links">Helpful links</h3>
<ul>
<li><p><a target="_blank" href="https://gist.github.com/basham/2175a16ab7c60ce8e001">https://gist.github.com/basham/2175a16ab7c60ce8e001</a> ( CSS Units Best Practices)</p>
</li>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-position">https://developer.mozilla.org/en-US/docs/Web/CSS/background-position</a> (Background Position)</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Colors in CSS]]></title><description><![CDATA[Using the CSS Language There are 6 Common Ways to Specify Colors:

Named Colors: CSS includes a predefined set of named colors, like red, blue, yellow, etc. There are approximately 140 standard color names.

Here are the colors sorted by their names:...]]></description><link>https://fieldsmarshall.com/colors-in-css</link><guid isPermaLink="true">https://fieldsmarshall.com/colors-in-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[color]]></category><category><![CDATA[color wheel]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Fri, 06 Oct 2023 22:20:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1696629431097/0f54724d-3052-48d2-918a-4fff195b9bcc.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Using the CSS Language There are 6 Common Ways to Specify Colors</strong>:</p>
<ol>
<li><p><strong>Named Colors</strong>: CSS includes a predefined set of named colors, like <code>red</code>, <code>blue</code>, <code>yellow</code>, etc. There are approximately 140 standard color names.</p>
<ul>
<li><p>Here are the colors sorted by their names: <a target="_blank" href="https://www.w3schools.com/colors/colors_names.asp">https://www.w3schools.com/colors/colors_names.asp</a></p>
</li>
<li><p>The popular CSS library Tailwind also includes its own set of named colors listed here: <a target="_blank" href="https://v2.tailwindcss.com/docs/customizing-colors">https://v2.tailwindcss.com/docs/customizing-colors</a></p>
</li>
</ul>
</li>
<li><p><strong>Hexadecimal (Hex) Notation</strong>: Colors can be defined using a <code>#</code> followed by a 6-digit hexadecimal value. For example: <code>#FF0000</code> is red. Shortened 3-digit hex codes can also be used, such as <code>#F00</code> for red or <code>#FFF</code> for white.</p>
</li>
<li><p><strong>RGB</strong>: Stands for Red, Green, and Blue. Colors can be defined using the <code>rgb()</code> function, for instance: <code>rgb(255,0,0)</code> is red and <code>rgb(255,255,255)</code> is white</p>
<ul>
<li><p>RGB notation is decimal notation as compared to Hexadecimal notation</p>
</li>
<li><p>255 decimal is equivalent to FF Hexadecimal</p>
</li>
</ul>
</li>
<li><p><strong>RGBA</strong>: The 'A' stands for Alpha (opacity). The rgba() function lets you specify colors with opacity. For instance: <code>rgba(255,0,0,0.5)</code> is semi-transparent red.</p>
</li>
<li><p><strong>HSL</strong>: Stands for Hue, Saturation, and Lightness. An example is <code>hsl(0, 100%, 50%)</code> which represents red.</p>
<ol>
<li>The HSL color model, which stands for Hue, Saturation, and Lightness, provides a more intuitive way to represent and manipulate colors compared to the RGB model. In HSL, 'Hue' determines the type of color and is represented as an angle from 0° to 360°, where common values like 0° signify red, 120° for green, and 240° for blue. 'Saturation' defines the intensity or purity of that hue, with values ranging from 0% (a shade of gray) to 100% (the fullest color). Lastly, 'Lightness' describes the brightness of the color, where 0% is black, 100% is white, and 50% exhibits the most authentic hue. Together, these three components allow for an intuitive understanding of color.</li>
</ol>
</li>
<li><p><strong>HSLA</strong>: Similar to HSL but with an Alpha (opacity) channel, for example: <code>hsla(0, 100%, 50%, 0.5)</code></p>
</li>
</ol>
<h3 id="heading-the-color-wheel">The Color Wheel</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696629344867/059a3308-a564-4b65-bcaf-43502eba2b21.jpeg" alt class="image--center mx-auto" /></p>
<p>The concept of the color wheel dates back to Sir Isaac Newton's experiments in 1666. Using a prism, he discovered that white light disperses into various colors of the visible spectrum. To illustrate this, he arranged these colors in a circular manner, laying the foundation for the modern color wheel. Over the centuries, artists have adapted this concept to better understand color mixing, defining primary colors, from which all other shades could be derived, and secondary colors, the result of blending two primaries. For example, mixing red and blue gives the color purple.</p>
<h3 id="heading-hsl-model-of-understanding-color">HSL Model of Understanding Color</h3>
<p>Going back to the HSL model we can think of Hue as a circular color wheel:</p>
<ul>
<li><p>Red sits at 0° (or 360°, as it's a circle).</p>
</li>
<li><p>Green is at 120°.</p>
</li>
<li><p>Blue is at 240°.</p>
</li>
</ul>
<p>Saturation tells us how vibrant or muted the color is, with 0% being gray and 100% being the most vibrant.</p>
<p>Finally, Lightness adjusts the shade from black (0%) to the true hue (50%) to white (100%). So, by playing with HSL values, you can pinpoint any color you want on this wheel.</p>
<p>Given this information on HSL and remembering mixing red and blue make purple. We can intuitively guess that purple would be between 240 and 360 degrees on the Hue color wheel. However, the exact hue value can vary depending on the specific shade of purple you're referring to, as "purple" can encompass a range from a more bluish-violet to a reddish-magenta.</p>
<h2 id="heading-visual-color-picker">Visual Color Picker</h2>
<p>W3schools website has a nice chart for easily picking out colors as well as translating colors between the various syntaxes. Take a look at it and play around.</p>
<p><a target="_blank" href="https://www.w3schools.com/colors/colors_picker.asp?colorhex=34568B">https://www.w3schools.com/colors/colors_picker.asp?colorhex=34568B</a></p>
<h3 id="heading-top-7-usages-of-the-keyword-color-in-css">Top 7 usages of the keyword <code>color</code> in CSS</h3>
<p>The keyword color in CSS is primarily used to define text color, but its influence doesn't stop there. Here are the top 7 usages of the keyword <code>color</code> in CSS:</p>
<ol>
<li><p><strong>Text Color</strong>:</p>
<ul>
<li><p>The <code>color</code> property is used to define the color of text. For instance:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">color</span>: blue;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would set the color of the text inside all <code>&lt;p&gt;</code> elements to blue.</p>
<ol>
<li><p><strong>Background Color</strong>:</p>
<ul>
<li><p>The <code>background-color</code> property is used to define the color of the background. For instance:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">background-color</span>: green;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would set the background color of the text inside all <code>&lt;p&gt;</code> elements to green.</p>
<ol>
<li><p><strong>Border Color</strong>:</p>
<ul>
<li><p>The <code>border-color</code> property is used to define the color of a border. For instance:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">border-color</span>: green;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would set the background color of the text inside all <code>&lt;p&gt;</code> elements to green.</p>
<ol>
<li><p><strong>currentColor</strong>:</p>
<ul>
<li><p><code>currentColor</code> is a special keyword in CSS that represents the computed value of the <code>color</code> property for a given element. This can be used with other properties to maintain consistency. For example:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">button</span> {
    <span class="hljs-attribute">color</span>: red;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid currentColor;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    Here, the border of the button will be the same color as the text (red).</p>
<ol>
<li><p><strong>SVG Styling</strong>:</p>
<ul>
<li><p>SVG (Scalable Vector Graphics) often uses the <code>color</code> property to influence certain parts of its styling. For example, the <code>fill</code> and <code>stroke</code> properties in SVG can inherit the color set by the <code>color</code> property:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">svg</span> {
    <span class="hljs-attribute">color</span>: green;
  }

  <span class="hljs-selector-tag">path</span> {
    <span class="hljs-attribute">fill</span>: currentColor;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would set the fill color of the SVG path elements to green.</p>
<ol>
<li><p><strong>List Marker Color</strong>:</p>
<ul>
<li><p>The <code>color</code> property can influence the color of list markers (bullets or numbers) in unordered and ordered lists.</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">ul</span> {
    <span class="hljs-attribute">color</span>: purple;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would set the color of the list markers in an unordered list to purple.</p>
<ol>
<li><p><strong>Pseudo-elements</strong>:</p>
<ul>
<li><p>The <code>color</code> property can also be applied to pseudo-elements like <code>::before</code> and <code>::after</code>, influencing the color of content generated by these pseudo-elements.</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">::before</span> {
    <span class="hljs-attribute">content</span>: <span class="hljs-string">"★"</span>;
    <span class="hljs-attribute">color</span>: gold;
  }
</code></pre>
</li>
</ul>
</li>
</ol>
<p>    This would prepend a gold star symbol to the content of a <code>&lt;div&gt;</code>.</p>
<p>While the primary usage of the <code>color</code> property is for text styling, its influence extends to various other areas, making it an integral part of CSS.</p>
<h3 id="heading-css-variables-and-colors">CSS Variables and Colors</h3>
<p>With the advent of CSS custom properties, it's now possible to define color variables for reuse throughout your stylesheets. For example: <code>--main-bg-color: #FF5733;</code> and then use <code>var(--main-bg-color)</code> to apply it.</p>
<h3 id="heading-color-functions-sass-and-less">Color Functions, SASS and LESS</h3>
<p>CSS has introduced color-modifying functions like <code>lighten()</code>, <code>darken()</code>, and <code>saturate()</code> but currently, these are mostly used with CSS preprocessors like SASS or LESS. However, in the future expect to see more direct color manipulation within CSS itself with CSS Color Module Level 4. Info here --&gt; <a target="_blank" href="https://www.w3.org/TR/css-color-4/">https://www.w3.org/TR/css-color-4/</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696630731556/e748ee22-4f90-4672-95b9-d564f69631af.jpeg" alt="Isaac Newton Dark Mode" class="image--center mx-auto" /></p>
<h3 id="heading-dark-mode-css-color-and-more">Dark Mode, CSS color and more</h3>
<p>Adapting your website's colors for dark mode involves responding to a user's system preference with the <code>prefers-color-scheme</code> media query in CSS. Here's a brief explanation:</p>
<ol>
<li><p><strong>Detecting Dark Mode</strong>: Use the <code>prefers-color-scheme</code> media query to detect if a user has chosen a dark or light interface theme in their system settings.</p>
<pre><code class="lang-css"> <span class="hljs-comment">/* Light mode styles (default) */</span>
 <span class="hljs-selector-tag">body</span> {
     <span class="hljs-attribute">background-color</span>: white;
     <span class="hljs-attribute">color</span>: black;
 }

 <span class="hljs-comment">/* Dark mode styles */</span>
 <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
     <span class="hljs-selector-tag">body</span> {
         <span class="hljs-attribute">background-color</span>: black;
         <span class="hljs-attribute">color</span>: white;
     }
 }
</code></pre>
</li>
<li><p><strong>CSS Variables for Flexibility</strong>: To make switching between color schemes smoother and more manageable, consider using CSS custom properties (variables).</p>
<pre><code class="lang-css"> <span class="hljs-comment">/* Define default (light mode) colors */</span>
 <span class="hljs-selector-pseudo">:root</span> {
     <span class="hljs-attribute">--background-color</span>: white;
     <span class="hljs-attribute">--text-color</span>: black;
 }

 <span class="hljs-comment">/* Override for dark mode */</span>
 <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
     <span class="hljs-selector-pseudo">:root</span> {
         <span class="hljs-attribute">--background-color</span>: black;
         <span class="hljs-attribute">--text-color</span>: white;
     }
 }

 <span class="hljs-selector-tag">body</span> {
     <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">var</span>(--background-color);
     <span class="hljs-attribute">color</span>: <span class="hljs-built_in">var</span>(--text-color);
 }
</code></pre>
</li>
<li><p><strong>Additional Considerations for dark mode</strong>:</p>
<ul>
<li><p>When designing for dark mode, it's not just about inverting colors. It's essential to ensure that colors maintain proper contrast for readability.</p>
</li>
<li><p>Consider other elements like images, borders, and shadows. They might also need adjustments for dark mode to ensure a harmonious appearance.</p>
</li>
<li><p>Some websites provide a toggle button for users to switch between light and dark modes manually, overriding the system preference. Implementing this requires a bit of JavaScript along with CSS.</p>
</li>
</ul>
</li>
</ol>
<p>With these basics, you can start adapting your website to be more user-friendly in both light and dark environments.</p>
<p><strong>The Tailwind CSS Frameworks &amp; Dark Mode</strong>:</p>
<p>Modern CSS frameworks, such as the popular Tailwind CSS, have significantly simplified the process of implementing dark mode. Tailwind, for instance, provides utility classes that can be easily toggled based on user preferences without writing custom media queries. By configuring your Tailwind setup and adding a simple <code>dark</code> variant, you can prepend your utility classes with <code>dark:</code> to style elements specifically for dark mode. This streamlined approach not only reduces the amount of custom CSS required but also ensures a more consistent and maintainable codebase.</p>
]]></content:encoded></item><item><title><![CDATA[CSS Combinators - An Overview]]></title><description><![CDATA[First, a quick review of selectors. CSS selectors define which elements you want to style. There are three types of CSS selectors: element, id and class.
nav {
  /* Here we have an element selector - making text blue in
    this section */
   color: ...]]></description><link>https://fieldsmarshall.com/css-combinators-an-overview</link><guid isPermaLink="true">https://fieldsmarshall.com/css-combinators-an-overview</guid><category><![CDATA[CSS]]></category><category><![CDATA[combinators]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Wed, 04 Oct 2023 19:00:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/iftBhUFfecE/upload/3394eaa763e878d170d1c8c440008494.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>First, a quick review of selectors. CSS selectors define which elements you want to style. There are three types of CSS selectors: element, id and class.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">nav</span> {
  <span class="hljs-comment">/* Here we have an element selector - making text blue in
    this section */</span>
   <span class="hljs-attribute">color</span>: blue;
}

<span class="hljs-selector-id">#standout</span> {
    <span class="hljs-comment">/* here we have an ID selector making the element 
    tagged with the id of standout  have a dotted red border */</span>
    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> dotted red;
}

<span class="hljs-selector-class">.pink</span> {
     <span class="hljs-comment">/* here we have a class selector making all elements
     with class="pink" have a pink background */</span>
    <span class="hljs-attribute">background-color</span>: pink;
}
</code></pre>
<p>CSS combinator selectors help us to specify the exact element we would like to select based on the specific relationship between each selector. The term "combinator" refers to the character we use to combine CSS selectors. A few examples are in order.</p>
<p>Note: there are four different combinators in CSS as of this writing.</p>
<ul>
<li><p>descendant selector (space)</p>
</li>
<li><p>child selector (&gt;)</p>
</li>
<li><p>adjacent sibling selector (+)</p>
</li>
<li><p>general sibling selector (~)</p>
</li>
</ul>
<pre><code class="lang-css"><span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">p</span> { <span class="hljs-attribute">background-color</span>: green; }
<span class="hljs-comment">/* descendant selector (space)   target all elements that are
descendants of a specified element. all p tags found in a nav*/</span>

<span class="hljs-comment">/* child selector  (&gt;)   target all elements that are the 
 children of a specified element.    */</span>
<span class="hljs-selector-tag">nav</span> &gt; <span class="hljs-selector-tag">p</span> { <span class="hljs-attribute">background-color</span>: purple;}

<span class="hljs-comment">/* Adjacent sibling selector (+) target all elements 
immediately following another element.
Siblings have the same parent element  */</span> 
<span class="hljs-selector-tag">nav</span> + <span class="hljs-selector-tag">p</span> { <span class="hljs-attribute">background-color</span>: red;}

<span class="hljs-comment">/* General sibling selector (~) target all elements that 
share the same parent and come after the first specified element
*/</span>
<span class="hljs-selector-tag">nav</span>  ~ <span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">background-color</span>: yellow;
}
</code></pre>
<h3 id="heading-4-types-of-css-combinators">4 Types of CSS Combinators</h3>
<ul>
<li><p><strong>Descendant Combinator (space)</strong></p>
<ul>
<li><p><strong>Definition and Symbol Representation</strong>: A descendant combinator, represented by a space, selects all elements that are descendants of a specified element.</p>
</li>
<li><p><strong>Example of Usage</strong>: <code>div p { color: red; }</code> — This rule selects all <code>&lt;p&gt;</code> elements inside <code>&lt;div&gt;</code> elements and colors them red.</p>
</li>
<li><p><strong>Practical Applications and Scenarios</strong>: Use this when you want to target elements nested at any level within another element.</p>
</li>
</ul>
</li>
<li><p><strong>Child Combinator (&gt;)</strong></p>
<ul>
<li><p><strong>Definition and Symbol Representation</strong>: A child combinator, symbolized by <code>&gt;</code>, selects all elements that are the direct children of a specified element.</p>
</li>
<li><p><strong>Example of Usage</strong>: <code>div &gt; p { font-weight: bold; }</code> — This rule targets only the <code>&lt;p&gt;</code> elements that are direct children of <code>&lt;div&gt;</code> elements, making their text bold.</p>
</li>
<li><p><strong>Practical Applications and Scenarios</strong>: Ideal for when you want to affect only the immediate child elements and not elements nested further.</p>
</li>
</ul>
</li>
<li><p><strong>Adjacent Sibling Combinator (+)</strong></p>
<ol>
<li><p><strong>Definition and Symbol Representation</strong>: This combinator, indicated by <code>+</code>, targets an element that is directly after (and at the same level as) the first specified element.</p>
</li>
<li><p><strong>Example of Usage</strong>: <code>h2 + p { margin-top: 20px; }</code> — This gives a top margin to a <code>&lt;p&gt;</code> element that directly follows an <code>&lt;h2&gt;</code> element.</p>
</li>
<li><p><strong>Practical Applications and Scenarios</strong>: Useful when you want to style an element based on its preceding sibling, such as adding spacing or modifying typography.</p>
</li>
</ol>
</li>
<li><p><strong>General Sibling Combinator (~)</strong></p>
<ol>
<li><p><strong>Definition and Symbol Representation</strong>: Represented by <code>~</code>, this combinator targets all elements that share the same parent and that come after the first specified element.</p>
</li>
<li><p><strong>Example of Usage</strong>: <code>h2 ~ p { text-indent: 50px; }</code> — This indents all <code>&lt;p&gt;</code> elements that follow an <code>&lt;h2&gt;</code> under the same parent.</p>
</li>
<li><p><strong>Practical Applications and Scenarios</strong>: When you have multiple siblings and you wish to style all those that follow a specific element.</p>
</li>
</ol>
</li>
</ul>
<h3 id="heading-css-combinator-considerations-and-tips">CSS Combinator Considerations and Tips</h3>
<ul>
<li><p><strong>You are not limited to using a single combinator</strong>. They can be chained together for a more detailed selection. For example, <code>div + p &gt; span</code> selects a <code>&lt;span&gt;</code> inside a <code>&lt;p&gt;</code> that directly follows a <code>&lt;div&gt;</code>.</p>
</li>
<li><p><strong>Keep specificity in mind</strong>: More specific selectors have higher priority. Ensure your styles don't get overridden unintentionally. This ties into the next tip. Most likely you will want to keep combinators simple.</p>
</li>
<li><p><strong>Keep It Simple and Avoid overusing or overcomplicating selectors</strong>: Simplify for maintainability and better performance.</p>
</li>
</ul>
<h3 id="heading-resources-for-css-combinators">Resources for CSS Combinators</h3>
<ul>
<li><p><a target="_blank" href="https://www.w3schools.com/css/css_combinators.asp">https://www.w3schools.com/css/css_combinators.asp</a></p>
</li>
<li><p><a target="_blank" href="https://www.scaler.com/topics/css/css-combinators/">https://www.scaler.com/topics/css/css-combinators/</a></p>
</li>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators">https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Understanding Pseudo-classes and Pseudo-elements in CSS]]></title><description><![CDATA[Pseudo-classes are used to define a special state of an element using CSS. There are (in late 2023) around 30 pseudo-classes. Pseudo-elements are called pseudo-elements because they do not exist in the DOM. While there are too many to go into in deta...]]></description><link>https://fieldsmarshall.com/understanding-pseudo-classes-and-pseudo-elements-in-css</link><guid isPermaLink="true">https://fieldsmarshall.com/understanding-pseudo-classes-and-pseudo-elements-in-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[pseudo elements]]></category><category><![CDATA[Pseudo-classes ]]></category><category><![CDATA[Tailwind CSS]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Mon, 02 Oct 2023 19:00:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/D5nh6mCW52c/upload/f8a3e1da90ca3938ee31cbbaee73b985.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Pseudo-classes are used to define a special state of an element using CSS. There are (in late 2023) <a target="_blank" href="https://www.w3schools.com/css/css_pseudo_classes.asp">around 30 pseudo-classes</a>. Pseudo-elements are called pseudo-elements because they do not exist in the DOM. While there are too many to go into in detail, we do give a top ten list of examples below. However, to quickly understand the idea, say for example we would like to change the background color of a button when we hover on it or while the button is active then we would use a pseudo-class. We could change the color of a button while hovering programmatically with Javascript but pseudo-classes make this process much easier using CSS.</p>
<h2 id="heading-top-ten-most-popular-pseudo-classes-and-what-they-do">Top ten most popular pseudo-classes and what they do</h2>
<ol>
<li><p><code>: hover</code></p>
<ul>
<li><p>Applies styles when the mouse pointer is over an element.</p>
</li>
<li><p>Useful for creating interactive and responsive hover effects.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/zYyaPXK">https://codepen.io/fieldsmarshall/pen/zYyaPXK</a></p>
</li>
</ul>
</li>
<li><p><code>: active</code></p>
<ul>
<li><p>Applies styles to an element when it's being actively clicked or selected.</p>
</li>
<li><p>Often used to give visual feedback when a user clicks a button or link.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/NWezXKV">https://codepen.io/fieldsmarshall/pen/NWezXKV</a></p>
</li>
</ul>
</li>
<li><p><code>: focus</code></p>
<ul>
<li><p>Applies styles to an element that currently has a keyboard focus.</p>
</li>
<li><p>Commonly used for styling form elements like input fields when they are selected or focused.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/NWezXWV">https://codepen.io/fieldsmarshall/pen/NWezXWV</a></p>
</li>
</ul>
</li>
<li><p><code>:nth-child(n)</code></p>
<ul>
<li><p>Selects elements based on their position within a parent element.</p>
</li>
<li><p>Allows for targeting specific child elements in a list or group.</p>
</li>
<li><p>Could use "even" or "odd" to select only even or odd items in an unordered list</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/RwEJxoe">https://codepen.io/fieldsmarshall/pen/RwEJxoe</a></p>
</li>
</ul>
</li>
<li><p><code>:not(selector)</code></p>
<ul>
<li><p>Selects elements that do not match a specified selector.</p>
</li>
<li><p>Useful for applying styles to elements that meet certain criteria while excluding others.</p>
</li>
<li><p>Example : <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/qBLKpmx">https://codepen.io/fieldsmarshall/pen/qBLKpmx</a></p>
</li>
</ul>
</li>
<li><p><code>:first-child</code></p>
<ul>
<li><p>Targets the first child element of its parent.</p>
</li>
<li><p>Helpful for styling the first item in a list or a specific element at the beginning of a container.</p>
</li>
</ul>
</li>
<li><p><code>:last-child</code></p>
<ul>
<li><p>Targets the last child element of its parent.</p>
</li>
<li><p>Useful for styling the last item in a list or a specific element at the end of a container.</p>
</li>
</ul>
</li>
<li><p><code>:nth-of-type(n)</code></p>
<ul>
<li><p>Selects elements based on their position within their parent element, considering only elements of the same type.</p>
</li>
<li><p>Allows for more precise targeting of specific child elements.</p>
</li>
</ul>
</li>
<li><p><code>:checked</code></p>
<ul>
<li><p>Applies styles to radio buttons or checkboxes that are checked.</p>
</li>
<li><p>Enables customization of the appearance of selected options in forms.</p>
</li>
</ul>
</li>
<li><p><code>:enabled</code> and :<code>disabled</code></p>
<ul>
<li><p><code>:enabled</code> selects form elements that are currently interactive or enabled for user input.</p>
</li>
<li><p><code>:disabled</code> select form elements that are currently inactive or disabled.</p>
</li>
<li><p>Useful for changing the appearance of form fields based on their state.</p>
</li>
</ul>
</li>
</ol>
<p><strong>Reminder</strong>: These pseudo-classes offer a way to apply styles to elements based on their state, position, or relationship to other elements, enhancing the styling and behavior of web pages.</p>
<h2 id="heading-pseudo-classes-vs-pseudo-elements">Pseudo-Classes vs Pseudo-elements</h2>
<ul>
<li><p>Pseudo-elements are like pseudo-classes except they don't target a specific state instead they target a sub-element.</p>
</li>
<li><p>Pseudo-elements use a double colon</p>
</li>
<li><p>Pseudo-elements can inject content and style that content ( before and after pseudo-elements)</p>
</li>
<li><p>There are only 6 pseudo-elements versus around 30 pseudo-classes</p>
</li>
</ul>
<h2 id="heading-all-pseudo-elements-and-their-common-usage">All Pseudo-Elements and their common usage</h2>
<ol>
<li><p><code>::before</code></p>
<ul>
<li><p>Allows you to insert content before the content of the selected element.</p>
</li>
<li><p>Commonly used for decorative elements or icons before the text.</p>
</li>
<li><p>Can be used for background image overlays</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/poqKpQR">https://codepen.io/fieldsmarshall/pen/poqKpQR</a></p>
</li>
</ul>
</li>
<li><p><code>::after</code></p>
<ul>
<li><p>Similar to <code>::before</code>, but it inserts content after the content of the selected element.</p>
</li>
<li><p>Often used for adding additional content or icons after text.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/YzdvYbM">https://codepen.io/fieldsmarshall/pen/YzdvYbM</a></p>
</li>
</ul>
</li>
<li><p><code>::first-line</code></p>
<ul>
<li><p>Targets the first line of text within a block-level element.</p>
</li>
<li><p>Useful for applying styles like font size or color to the initial line of text in a paragraph.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/NWezyGb">https://codepen.io/fieldsmarshall/pen/NWezyGb</a></p>
</li>
</ul>
</li>
<li><p><code>::first-letter</code></p>
<ul>
<li><p>Targets the first letter of the text within a block-level element.</p>
</li>
<li><p>Often used for creating drop caps or applying unique styles to the first letter of a paragraph.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/NWezyGb">https://codepen.io/fieldsmarshall/pen/NWezyGb</a></p>
</li>
</ul>
</li>
<li><p><code>::selection</code></p>
<ul>
<li><p>Selects and styles the portion of text that a user highlights with their cursor.</p>
</li>
<li><p>Useful for customizing the appearance of selected text on a webpage.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/dywKdGE">https://codepen.io/fieldsmarshall/pen/dywKdGE</a></p>
</li>
</ul>
</li>
<li><p><code>::placeholder</code></p>
<ul>
<li><p>Applies styles to the placeholder text inside an input or textarea element.</p>
</li>
<li><p>Useful for changing the color, font, or other properties of the input field's placeholder text.</p>
</li>
<li><p>Example: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/dywKdGE">https://codepen.io/fieldsmarshall/pen/dywKdGE</a></p>
</li>
</ul>
</li>
</ol>
<h2 id="heading-debugging-pseudo-elements-and-pseudo-classes-with-chrome-and-firefox">Debugging Pseudo Elements and Pseudo-Classes with Chrome and Firefox</h2>
<p>Debugging CSS pseudo-classes and pseudo-elements can be tricky unless you use DevTools on Chrome or Firefox.</p>
<h3 id="heading-google-chrome-devtools-and-pseudo-classes"><strong>Google Chrome, DevTools and Pseudo Classes</strong></h3>
<ol>
<li><p>Open DevTools:</p>
<ul>
<li>Right-click on an element on a page, and select 'Inspect' or press <code>Ctrl + Shift + I</code> (Windows/Linux) or <code>Cmd + Opt + I</code> (Mac) to open the DevTools.</li>
</ul>
</li>
<li><p><strong>Select an element</strong>:</p>
<ul>
<li>Click on the 'Select an element' icon or press <code>Ctrl + Shift + C</code> (Windows/Linux) or <code>Cmd + Shift + C</code> (Mac) to activate the element selection mode, then click on the element you are interested in on the page.</li>
</ul>
</li>
<li><p><strong>Inspect Pseudo-classes</strong>:</p>
<ul>
<li><p>In the "Elements" panel, you can find the "Styles" tab on the right side.</p>
</li>
<li><p>Here, you will find a <code>:hov</code> button, clicking this button will allow you to toggle various pseudo-classes like <code>:hover</code>, <code>:active</code>, <code>:focus</code>, etc.</p>
</li>
</ul>
</li>
<li><p><strong>Inspect Pseudo-elements</strong>:</p>
<ul>
<li><p>In the same "Styles" tab, you can scroll down to find the pseudo-elements <code>::before</code> and <code>::after</code>.</p>
</li>
<li><p>Here you can see the styles applied to these pseudo-elements and edit them to see changes in real time.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-tailwind-bootstrap-pseudo-classes-and-pseudo-elements">Tailwind, Bootstrap, pseudo-classes and pseudo-elements</h3>
<p>Tailwind makes using pseudo-classes and pseudo-elements easy whereas Bootstrap does pretty much nothing in this regard.</p>
<h3 id="heading-tailwind-and-pseudo-classes">Tailwind and pseudo-classes</h3>
<p>Tailwind provides explicit control over pseudo-classes directly within your markup. By using utility classes, developers can apply styles for different states like <code>:hover</code>, <code>:focus</code>, <code>:active</code>, etc., directly to the elements. This explicit control makes it clear at a glance what styles are being applied and under which circumstances.</p>
<pre><code class="lang-xml"><span class="hljs-comment">&lt;!-- Tailwind CSS --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hover:bg-blue-500 focus:outline-none"</span>&gt;</span>Click me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Above we have an example where hovering will make the background blue and focusing (clicking on the button) will remove the outline.</p>
<p>Tailwind includes modifiers for just about everything you’ll ever need, including:</p>
<ul>
<li><p><a target="_blank" href="https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes"><strong>Pseudo-classes</strong></a>, like <code>:hover</code>, <code>:focus</code>, <code>:first-child</code>, and <code>:required</code></p>
</li>
<li><p><a target="_blank" href="https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-elements"><strong>Pseudo-elements</strong></a>, like <code>::before</code>, <code>::after</code>, <code>::placeholder</code>, and <code>::selection</code></p>
</li>
</ul>
<h3 id="heading-special-links-for-pseudo-classes-and-pseudo-elements">Special Links for pseudo-classes and pseudo-elements</h3>
<ul>
<li><p>W3 Schools links</p>
<ul>
<li><p><a target="_blank" href="https://www.w3schools.com/css/css_pseudo_classes.asp">pseudo-classes</a></p>
</li>
<li><p><a target="_blank" href="https://www.w3schools.com/css/css_pseudo_elements.asp">pseudo-elements</a></p>
</li>
</ul>
</li>
<li><p>Helpful YouTube videos</p>
<ul>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=FMu2cKWD90g">https://www.youtube.com/watch?v=FMu2cKWD90g</a></p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=kpXKwDGtjGE">https://www.youtube.com/watch?v=kpXKwDGtjGE</a></p>
</li>
</ul>
</li>
<li><p>Tailwind</p>
<ul>
<li><p><a target="_blank" href="https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes">https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes</a></p>
</li>
<li><p><a target="_blank" href="https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-elements">https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-elements</a></p>
</li>
</ul>
</li>
<li><p>Code Pen Examples</p>
<ul>
<li><a target="_blank" href="https://codepen.io/collection/RzrQba">https://codepen.io/collection/RzrQba</a></li>
</ul>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[CSS Media Query Basics]]></title><description><![CDATA[Media queries are very similar to Javascript or any other programming language in that they programmatically express an if condition.
As expressed in the pseudo-code below we have a default green background which becomes red at the larger screen widt...]]></description><link>https://fieldsmarshall.com/css-media-query-basics</link><guid isPermaLink="true">https://fieldsmarshall.com/css-media-query-basics</guid><category><![CDATA[CSS]]></category><category><![CDATA[media queries]]></category><dc:creator><![CDATA[Fields Marshall]]></dc:creator><pubDate>Wed, 27 Sep 2023 00:24:18 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/ubIWo074QlU/upload/e6200cbefa7328cc22140772d48e365b.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Media queries are very similar to Javascript or any other programming language in that they programmatically express an if condition.</p>
<p>As expressed in the pseudo-code below we have a default green background which becomes red at the larger screen width</p>
<pre><code class="lang-plaintext">background-color: green;
if (screen is bigger than 1024 pixels)  
    then with CSS make the background-color: red
</code></pre>
<p>Now that you get the idea here we have some sample CSS code that uses a media query code:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">background-color</span>: green;
}

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">min-width:</span> <span class="hljs-number">800px</span>){
    <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: red
    }
}
</code></pre>
<p>Media queries can be used to check many things including but not limited to</p>
<ul>
<li><p>viewport width</p>
</li>
<li><p>viewport height</p>
</li>
<li><p>type of media device - is it a printer or is it a screen - for example</p>
</li>
</ul>
<h2 id="heading-mobile-first-and-media-queries">Mobile first and media queries</h2>
<p>Mobile first is the idea/design that suggests we prioritize designing for mobile devices. This is a popular concept used by both Tailwind CSS and Bootstrap ( the two most popular CSS frameworks in 2023). To keep your media queries in line with this paradigm, simply use the min-width directive and only use the min-width directive. See the example below</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-comment">/* default green background */</span>
    <span class="hljs-attribute">background-color</span>: green;
}
<span class="hljs-comment">/* red background 600px and up */</span>
<span class="hljs-keyword">@media</span>(min-width:600px) {
    <span class="hljs-selector-tag">body</span> { 
        <span class="hljs-attribute">background-color</span>: red;
    }
}
<span class="hljs-comment">/* purple background 800px and up */</span>
<span class="hljs-keyword">@media</span>(min-width:800px){
    <span class="hljs-selector-tag">body</span> { 
        <span class="hljs-attribute">background-color</span>: purple;
    }
}
</code></pre>
<p>Once the browser reaches a width of 800px all three CSS rules will apply but because of the order only the last rule (background color: purple) will be active.</p>
<p>Observe this concept in action here: <a target="_blank" href="https://codepen.io/fieldsmarshall/pen/ExGLWGe">https://codepen.io/fieldsmarshall/pen/ExGLWGe</a></p>
<h2 id="heading-hiding-content-with-media-queries">Hiding Content with Media Queries</h2>
<p>Another common use of media queries is to hide content using the display:none CSS directive.</p>
<h2 id="heading-dark-mode-using-media-queries">Dark mode using media queries</h2>
<p>Media queries are a powerful tool in CSS for applying styles based on certain conditions. One application is to detect whether a user has set their preferences to dark mode on their device or browser, and then apply a corresponding dark theme to your webpage. The <code>prefers-color-scheme</code> media feature is used for this purpose. Below are a few quick examples:</p>
<ol>
<li><p><strong>Detection of Color Scheme Preference</strong>:</p>
<ul>
<li><strong>Best Practice</strong> Use the <code>prefers-color-scheme</code> media feature in a media query to check if the user has a preference for a dark or light color scheme.</li>
</ul>
</li>
</ol>
<pre><code class="lang-css">    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
        <span class="hljs-comment">/* styles for dark mode */</span>
    }
    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> light) {
        <span class="hljs-comment">/* styles for light mode */</span>
    }
</code></pre>
<ol>
<li><p><strong>Define Dark and Light Mode Styles</strong>:</p>
<ul>
<li>Within the curly braces <code>{ }</code> of each media query, define the styles that should be applied for dark mode and light mode respectively. This could be as simple as setting different background and text colors, or more complex styling adjustments.</li>
</ul>
</li>
</ol>
<pre><code class="lang-css">    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
        <span class="hljs-selector-tag">body</span> {
            <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#121212</span>;
            <span class="hljs-attribute">color</span>: white;
        }
    }
    <span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> light) {
        <span class="hljs-selector-tag">body</span> {
            <span class="hljs-attribute">background-color</span>: white;
            <span class="hljs-attribute">color</span>: black;
        }
    }
</code></pre>
<ol>
<li><p><strong>Fallback Styles</strong>:</p>
<ul>
<li>It’s also a good practice to define a default set of styles outside of the media queries, to act as a fallback in case the <code>prefers-color-scheme</code> media feature is not supported or if the user has not set a preference.</li>
</ul>
</li>
</ol>
<pre><code class="lang-css">    <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: white;
        <span class="hljs-attribute">color</span>: black;
    }
</code></pre>
<p>With these steps, you can set up basic detection of dark mode preferences using media queries, and provide a more comfortable viewing experience for your users. As you get more comfortable with media queries, you can explore more complex styling adjustments to better accommodate dark mode preferences.</p>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme">https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme</a></p>
<h2 id="heading-testing-media-queries-programmatically">Testing Media Queries Programmatically</h2>
<p>Testing media queries programmatically can be done in several ways depending on the environment and the tools at your disposal. Here are some common methods used to test media queries programmatically:</p>
<ol>
<li><p><strong>JavaScript:</strong></p>
<ul>
<li>You can use the <code>window.matchMedia()</code> method in JavaScript to test whether a particular media query matches the current state of the document.</li>
</ul>
</li>
</ol>
<pre><code class="lang-javascript">    <span class="hljs-keyword">const</span> mediaQuery = <span class="hljs-built_in">window</span>.matchMedia(<span class="hljs-string">'(prefers-color-scheme: dark)'</span>);

    <span class="hljs-keyword">if</span> (mediaQuery.matches) {
      <span class="hljs-comment">// Media query matches</span>
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'User prefers dark mode'</span>);
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// Media query does not match</span>
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'User does not prefer dark mode or preference is unknown'</span>);
    }

    <span class="hljs-comment">// You can also add an event listener to respond to changes</span>
    mediaQuery.addEventListener(<span class="hljs-string">'change'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (event.matches) {
        <span class="hljs-comment">// The media query now matches</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'User prefers dark mode'</span>);
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// The media query no longer matches</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'User does not prefer dark mode or preference is unknown'</span>);
      }
    });
</code></pre>
<ol>
<li><p><strong>Automated Testing with a Framework:</strong></p>
<ul>
<li>If you are using an automated testing framework like Jest, you might leverage a library such as <code>jest-matchmedia-mock</code> to mock and test media queries.</li>
</ul>
</li>
</ol>
<pre><code class="lang-javascript">    <span class="hljs-keyword">import</span> MatchMediaMock <span class="hljs-keyword">from</span> <span class="hljs-string">'jest-matchmedia-mock'</span>;

    <span class="hljs-keyword">let</span> matchMedia;

    beforeEach(<span class="hljs-function">() =&gt;</span> {
      matchMedia = <span class="hljs-keyword">new</span> MatchMediaMock();
    });

    <span class="hljs-comment">// ...</span>

    test(<span class="hljs-string">'matches dark mode media query'</span>, <span class="hljs-function">() =&gt;</span> {
      matchMedia.useMediaQuery(<span class="hljs-string">'(prefers-color-scheme: dark)'</span>);
      <span class="hljs-comment">// Now, your test environment behaves as if the media query matches</span>
      <span class="hljs-comment">// ...</span>
    });
</code></pre>
<ol>
<li><p><strong>CSS Testing Libraries:</strong></p>
<ul>
<li>Libraries such as "CSSOM" or "jsdom" can be used to simulate a DOM environment and inspect computed styles as a result of media queries. You can then write assertions based on the computed styles to verify the effect of your media queries.</li>
</ul>
</li>
</ol>
<pre><code class="lang-javascript">    <span class="hljs-keyword">const</span> { JSDOM } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'jsdom'</span>);

    <span class="hljs-keyword">const</span> dom = <span class="hljs-keyword">new</span> JSDOM(<span class="hljs-string">`&lt;!DOCTYPE html&gt;&lt;p&gt;Hello world&lt;/p&gt;`</span>, {
      <span class="hljs-attr">pretendToBeVisual</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">media</span>: <span class="hljs-string">'(prefers-color-scheme: dark)'</span>,
    });

    <span class="hljs-comment">// Now you can inspect computed styles as a result of your media queries</span>
</code></pre>
<p>These are some of the methods you can use to test media queries programmatically either in the browser or in an automated testing environment. Each method has its own set of considerations, so choose the one that fits your needs and the technologies you are working with.</p>
<p>In the end, the best resource for learning about media queries would be the mozilla documentation <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Testing_media_queries">https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Testing_media_queries</a> or <a target="_blank" href="https://www.w3schools.com/css/css3_mediaqueries.asp">https://www.w3schools.com/css/css3_mediaqueries.asp</a></p>
]]></content:encoded></item></channel></rss>