<?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: Fahad Bin Faiz</title>
    <description>The latest articles on DEV Community by Fahad Bin Faiz (@fahaddevs).</description>
    <link>https://dev.to/fahaddevs</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%2F248674%2F1f0ac354-8008-4b8e-a074-1e9273027c6e.jpg</url>
      <title>DEV Community: Fahad Bin Faiz</title>
      <link>https://dev.to/fahaddevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fahaddevs"/>
    <language>en</language>
    <item>
      <title>Exploring Sass Tint and Shade Functions for Color Manipulation</title>
      <dc:creator>Fahad Bin Faiz</dc:creator>
      <pubDate>Sat, 16 Nov 2024 18:59:51 +0000</pubDate>
      <link>https://dev.to/fahaddevs/exploring-sass-tint-and-shade-functions-for-color-manipulation-4e7c</link>
      <guid>https://dev.to/fahaddevs/exploring-sass-tint-and-shade-functions-for-color-manipulation-4e7c</guid>
      <description>&lt;h3&gt;
  
  
  Exploring Sass Tint and Shade Functions for Color Manipulation
&lt;/h3&gt;

&lt;p&gt;In web design and development, colors play a vital role in creating visually appealing and accessible interfaces. Sass, a powerful CSS preprocessor, makes color manipulation effortless with custom functions like &lt;code&gt;tint&lt;/code&gt; and &lt;code&gt;shade&lt;/code&gt;. These functions allow developers to adjust colors dynamically, adding consistency and flexibility to design systems.&lt;/p&gt;

&lt;p&gt;In this blog post, we’ll dive into how the &lt;code&gt;tint&lt;/code&gt; and &lt;code&gt;shade&lt;/code&gt; functions work, how they are implemented in Sass, and why they’re essential for modern web development.&lt;/p&gt;




&lt;h3&gt;
  
  
  What are Tint and Shade in Design?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tint&lt;/strong&gt;: A tint is created by mixing a color with white, making it lighter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shade&lt;/strong&gt;: A shade is created by mixing a color with black, making it darker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tints and shades help create a cohesive color palette by providing variations of base colors, ideal for themes, hover effects, and UI components.&lt;/p&gt;




&lt;h3&gt;
  
  
  Implementing Tint and Shade in Sass
&lt;/h3&gt;

&lt;p&gt;Below are simple yet effective Sass functions to create tints and shades:&lt;br&gt;
&lt;strong&gt;Tint Function&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;tint&lt;/code&gt; function lightens a color by mixing it with white.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;tint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$percentage&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="nd"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;white&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$percentage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mix(white, $color, $percentage)&lt;/code&gt; blends the given &lt;code&gt;$color&lt;/code&gt; with &lt;code&gt;white&lt;/code&gt; based on the &lt;code&gt;$percentage&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;A higher &lt;code&gt;$percentage&lt;/code&gt; results in a lighter color.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Shade Function&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;shade&lt;/code&gt; function darkens a color by mixing it with black.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;shade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$percentage&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="nd"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;black&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;$percentage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mix(black, $color, $percentage)&lt;/code&gt; blends the given &lt;code&gt;$color&lt;/code&gt; with &lt;code&gt;black&lt;/code&gt; based on the &lt;code&gt;$percentage&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;A higher &lt;code&gt;$percentage&lt;/code&gt; results in a darker color.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Practical Example
&lt;/h3&gt;

&lt;p&gt;Let’s create a base color palette and use the &lt;code&gt;tint&lt;/code&gt; and &lt;code&gt;shade&lt;/code&gt; functions to generate variations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sass Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;primary-color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;#3498&lt;/span&gt;&lt;span class="nt"&gt;db&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;.light-tint&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;tint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;primary-color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="o"&gt;%);&lt;/span&gt;
&lt;span class="nc"&gt;.darker-shade&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;shade&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;primary-color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;40&lt;/span&gt;&lt;span class="o"&gt;%);&lt;/span&gt;

&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;primary-color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="err"&gt;&amp;amp;:hover&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;tint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;primary-color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;shade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;primary-color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output CSS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.light-tint&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#69b7ef&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.darker-shade&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#216387&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#3498db&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#4aa3e1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="nd"&gt;:active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#297bb1&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;
  
  
  Benefits of Using Tint and Shade Functions
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: Generate consistent color variations throughout your design.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Avoid manually calculating or selecting colors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Easily adapt to changes in the base color for themes or branding updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: Code is more descriptive and easier to maintain.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  When to Use Tint and Shade
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hover and Active States&lt;/strong&gt;: Use tints for hover effects and shades for active states.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Theming&lt;/strong&gt;: Create light and dark mode themes dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradients&lt;/strong&gt;: Generate gradient backgrounds with subtle tints and shades.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility&lt;/strong&gt;: Ensure sufficient color contrast for improved readability.&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;Sass functions like &lt;code&gt;tint&lt;/code&gt; and &lt;code&gt;shade&lt;/code&gt; provide an elegant way to manage colors in your projects. They streamline your workflow, ensure consistency, and give you the flexibility to create stunning designs with minimal effort.&lt;/p&gt;

&lt;p&gt;So, the next time you’re working on a UI, try these Sass functions to add that perfect touch of color!&lt;/p&gt;

&lt;p&gt;Have you used &lt;code&gt;tint&lt;/code&gt; and &lt;code&gt;shade&lt;/code&gt; in your projects? Let me know how they’ve enhanced your workflow in the comments below!&lt;/p&gt;

</description>
      <category>scss</category>
      <category>tintshade</category>
      <category>css</category>
      <category>sass</category>
    </item>
    <item>
      <title>Dot Notation vs Bracket Notation for Object Properties – What's the Difference?</title>
      <dc:creator>Fahad Bin Faiz</dc:creator>
      <pubDate>Sat, 16 Nov 2024 18:42:16 +0000</pubDate>
      <link>https://dev.to/fahaddevs/dot-notation-vs-bracket-notation-for-object-properties-whats-the-difference-2i22</link>
      <guid>https://dev.to/fahaddevs/dot-notation-vs-bracket-notation-for-object-properties-whats-the-difference-2i22</guid>
      <description>&lt;h2&gt;
  
  
  Dot Notation
&lt;/h2&gt;

&lt;p&gt;Dot notation is simpler and more readable. It's used when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The property name is a valid identifier (contains only letters, digits, $, or _, and doesn’t start with a digit).&lt;/li&gt;
&lt;li&gt;You know the property name ahead of time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: 'alice', age: 30 };
console.log(person.name); // 'alice'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bracket Notation
&lt;/h2&gt;

&lt;p&gt;Bracket notation is more flexible and allows you to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use property names stored in variables.&lt;/li&gt;
&lt;li&gt;Access properties with special characters, spaces, or numbers that aren’t valid identifiers.&lt;/li&gt;
&lt;li&gt;Dynamically build property names at runtime.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Examples:&lt;br&gt;
**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using variables to access properties:
**
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: 'alice', age: 30 };
const prop = 'name';
console.log(person[prop]); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;**&lt;br&gt;
2.Properties with special characters or spaces:&lt;br&gt;
**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { 'first name': 'alice', age: 30 };
console.log(person['first name']); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;br&gt;
3.Dynamically generated property names:&lt;br&gt;
**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const property = 'name';
console.log(person[property]); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to Use Bracket Notation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If the property name is dynamic or stored in a variable.&lt;/li&gt;
&lt;li&gt;If the property name has spaces, special characters, or starts with a number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;For most other cases, dot notation is preferred because it’s more readable and concise.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dotnotation</category>
      <category>bracketnotation</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Dot Notation vs Bracket Notation for Object Properties – What's the Difference?</title>
      <dc:creator>Fahad Bin Faiz</dc:creator>
      <pubDate>Tue, 12 Nov 2024 16:30:33 +0000</pubDate>
      <link>https://dev.to/fahaddevs/dot-notation-vs-bracket-notation-for-object-properties-whats-the-difference-5hh0</link>
      <guid>https://dev.to/fahaddevs/dot-notation-vs-bracket-notation-for-object-properties-whats-the-difference-5hh0</guid>
      <description>&lt;h2&gt;
  
  
  Dot Notation
&lt;/h2&gt;

&lt;p&gt;Dot notation is simpler and more readable. It's used when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The property name is a valid identifier (contains only letters, digits, $, or _, and doesn’t start with a digit).&lt;/li&gt;
&lt;li&gt;You know the property name ahead of time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: 'alice', age: 30 };
console.log(person.name); // 'alice'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bracket Notation
&lt;/h2&gt;

&lt;p&gt;Bracket notation is more flexible and allows you to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use property names stored in variables.&lt;/li&gt;
&lt;li&gt;Access properties with special characters, spaces, or numbers that aren’t valid identifiers.&lt;/li&gt;
&lt;li&gt;Dynamically build property names at runtime.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;strong&gt;1. Using variables to access properties:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: 'alice', age: 30 };
const prop = 'name';
console.log(person[prop]); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.Properties with special characters or spaces:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { 'first name': 'alice', age: 30 };
console.log(person['first name']); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.Dynamically generated property names:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const property = 'name';
console.log(person[property]); // 'alice'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to Use Bracket Notation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If the property name is dynamic or stored in a variable.&lt;/li&gt;
&lt;li&gt;If the property name has spaces, special characters, or starts with a number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For most other cases, dot notation is preferred because it’s more readable and concise.&lt;/p&gt;

</description>
      <category>dotnotation</category>
      <category>bracketnotation</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>CSS Combinators</title>
      <dc:creator>Fahad Bin Faiz</dc:creator>
      <pubDate>Sat, 20 Jun 2020 18:26:09 +0000</pubDate>
      <link>https://dev.to/fahaddevs/css-combinators-n1k</link>
      <guid>https://dev.to/fahaddevs/css-combinators-n1k</guid>
      <description>&lt;p&gt;In this article, I will discuss CSS Combinators.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Table Of Contents&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Descendant combinator&lt;/li&gt;
&lt;li&gt;Child combinator&lt;/li&gt;
&lt;li&gt;Adjacent sibling combinator&lt;/li&gt;
&lt;li&gt;General sibling combinator&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Descendant combinator &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The descendant combinator — typically represented by a single space ( ) character — combines two selectors such that elements matched by the second selector are selected if they have an ancestor (parent, parent's parent, parent's parent's parent, etc) element matching the first selector. Selectors that utilize a descendant combinator are called descendant selectors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Text in .box&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Text not in .box&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* This is descendant combinator example */&lt;/span&gt;
&lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;
  
  
  Child combinator &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The child combinator (&amp;gt;) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Descendent elements further down the hierarchy don't match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Unordered item&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Unordered item
        &lt;span class="nt"&gt;&amp;lt;ol&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Item 1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Item 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ol&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* this is child combinator example */&lt;/span&gt;
&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&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;
  
  
  Adjacent sibling combinator &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The adjacent sibling selector (+) is used to select something if it is right next to another element at the same level of the hierarchy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;article&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;A heading&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo
            melon azuki bean garlic.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* this is adjacent sibling combinator example */&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5em&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;
  
  
  General sibling combinator &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;If you want to select siblings of an element even if they are not directly adjacent, then you can use the general sibling combinator (~). To select all img elements that come anywhere after p tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;article&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;A heading&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;I am a paragraph.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;I am a div&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;I am another paragraph.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* this is general sibling combinator example*/&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.5em&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;h1&gt;
  
  
  Thanks for reading...
&lt;/h1&gt;

&lt;p&gt;Original article &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators" rel="noopener noreferrer"&gt;Click here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>csscombinators</category>
      <category>cssadvance</category>
      <category>fahaddevs</category>
    </item>
  </channel>
</rss>
