<?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: Francisco Hernandez</title>
    <description>The latest articles on DEV Community by Francisco Hernandez (@franciscoh017).</description>
    <link>https://dev.to/franciscoh017</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%2F829381%2F8b3db60f-d79b-4712-8cde-09685c4566ab.png</url>
      <title>DEV Community: Francisco Hernandez</title>
      <link>https://dev.to/franciscoh017</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/franciscoh017"/>
    <language>en</language>
    <item>
      <title>Understanding CSS Selectors</title>
      <dc:creator>Francisco Hernandez</dc:creator>
      <pubDate>Sun, 13 Mar 2022 01:55:43 +0000</pubDate>
      <link>https://dev.to/franciscoh017/understanding-css-selectors-10bn</link>
      <guid>https://dev.to/franciscoh017/understanding-css-selectors-10bn</guid>
      <description>&lt;p&gt;Recently, I was chatting and mentoring a colleague, a Frontend programmer who consults me frequently whenever he is working with user interfaces, where some CSS is required. As long as I've known him this has always been a frequent query throughout our career, and during our last few interactions, I decided to pay a little more attention to the reasons why he wasn't getting the rules he wanted to apply to elements on the page to work for him.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjkpb8n6wcx47cts530m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjkpb8n6wcx47cts530m.gif" alt="Frustrated Programming GIF" width="640" height="480"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;After several questions and answers, I realized that, despite being a very competent Frontend programmer, handling multiple JavaScript Frameworks such as Vue.js and React.js, and being able to use CSS libraries such as Bootstrap, Bulma and TailwindCSS, he seemed to possess a certain lack of knowledge of the basics of CSS, even though he claimed to know about it.&lt;/p&gt;

&lt;p&gt;As I contemplated this, and began to include comments with lessons and information that my colleague could understand and see as new learning, his inquiries decreased and his comments of victory in being able to do it on his own began to become more frequent.&lt;/p&gt;

&lt;p&gt;So, I have decided to make this &lt;strong&gt;brief guide&lt;/strong&gt; for all those who, for one reason or another, feel identified with my colleague's situation, or simply want to learn and/or refresh their CSS knowledge.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are CSS selectors?
&lt;/h2&gt;

&lt;p&gt;If you've ever worked with Javascript or CSS, chances are you've come across one of these in the past. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A CSS selector is the initial part of a CSS rule. It is a pattern of elements or other terms that lets the browser know which HTML elements should be selected to apply the CSS properties found in the rule applied to them. &lt;br&gt;
&lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors" rel="noopener noreferrer"&gt;MDN Web Docs - CSS Selectors&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3htw2so9zf9q8um1zl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3htw2so9zf9q8um1zl8.png" alt="Anatomy of a CSS Declaration by MDN Web Docs" width="377" height="213"&gt;&lt;/a&gt;Anatomy of a CSS Declaration by MDN Web Docs&lt;/p&gt;

&lt;p&gt;That's right, as the image above describes, selectors are the first part of a CSS rule, and will always precede the opening braces of a CSS rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of selectors
&lt;/h2&gt;

&lt;p&gt;There are different groups of CSS selectors, and knowing which type of CSS selector you need in each situation will help you to always select the HTML elements you require in the right way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Universal Selector
&lt;/h3&gt;

&lt;p&gt;The CSS universal selector &lt;code&gt;"*"&lt;/code&gt; matches elements of any type. It selects everything in the document (or inside the parent element if it is being chained together with another element and a descendant combinator).&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="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Type, class, and ID selectors
&lt;/h3&gt;

&lt;p&gt;This group includes selectors that select an HTML element such as a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&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="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, it includes selectors that select a &lt;code&gt;class&lt;/code&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;.card&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or an ID:&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="nf"&gt;#login-modal&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Attribute selectors
&lt;/h3&gt;

&lt;p&gt;They provide various ways to select elements based on the presence of a certain attribute or an attribute with a certain value.&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="nt"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;target&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"_blank"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pseudo-classes and pseudo-elements
&lt;/h3&gt;

&lt;p&gt;This group of selectors includes pseudo-classes, which apply styles to certain states of an element:&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="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:visited&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:disabled&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For the set of supported pseudo-classes, see the full specification on &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements#pseudo-classes" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And pseudo-elements, which select a specific part of an element, rather than the element itself.&lt;/p&gt;

&lt;p&gt;For example, if we wanted to select the first line of a p element, we could use the &lt;code&gt;::first-line&lt;/code&gt; pseudo-element, which would always select the first line within a text element:&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="nt"&gt;p&lt;/span&gt;&lt;span class="nd"&gt;::first-line&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if we wanted to apply a rule to the marker or number of an item &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; in a list, we could use the &lt;code&gt;::marker&lt;/code&gt; pseudo-element to accomplish this task:&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="nt"&gt;li&lt;/span&gt;&lt;span class="nd"&gt;::marker&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For the set of supported pseudo-elements, see the full specification on &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements#pseudo-classes" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Combinators
&lt;/h3&gt;

&lt;p&gt;Finally, there are the combinators which, as the name suggests, are used to combine other selectors in order to precisely select elements in the document.&lt;/p&gt;

&lt;h4&gt;
  
  
  Descendant Combinator
&lt;/h4&gt;

&lt;p&gt;This is represented as a space &lt;code&gt;“ “&lt;/code&gt;, and combines two selectors, where those elements match where the second selector matches an element that has an ancestor (parent, parent's parent, parent's parent's parent, etc.) that matches with the first selector.&lt;br&gt;
It is similar to the Child Combinator, but has the peculiarity that it does not require a strictly parent-child relationship for the declared rule to be applied.&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="nt"&gt;selector1&lt;/span&gt; &lt;span class="nt"&gt;selector2&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Child Combinator
&lt;/h4&gt;

&lt;p&gt;Presented as a greater than symbol &lt;code&gt;“&amp;gt;”&lt;/code&gt; that is placed between two selectors. Matches only those elements where the second selector matches an element that is a direct child of the element that matches the first selector.&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="nt"&gt;selector1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;selector2&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Adjacent Sibling Combinator
&lt;/h4&gt;

&lt;p&gt;Presented as a plus sign &lt;code&gt;“+”&lt;/code&gt; that is placed between two selectors. It matches only those elements matched by the second selector that are the next sibling element of the first selector.&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="nt"&gt;selector1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;selector2&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  General Sibling Combinator
&lt;/h4&gt;

&lt;p&gt;The general sibling combinator &lt;code&gt;“~”&lt;/code&gt; separates two selectors and matches all iterations of the second element, that are following the first element (though not necessarily immediately), and are children of the same parent element.&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="nt"&gt;selector1&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="nt"&gt;selector2&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Analyzing my colleage's situation, and evaluating the occasions where other colleagues have resorted to my CSS skills to meet some requirement in the past, I can say that many programmers actually claim to know CSS, but very few really master concepts beyond of the &lt;code&gt;type, class, id, and descendant selectors&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After learning, practicing, and putting this selector to good use, writing CSS rulesets will be a piece of cake!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92ofzudvgbefcon155to.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92ofzudvgbefcon155to.gif" alt="piece of cake, huh?" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, I leave this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors" rel="noopener noreferrer"&gt;CSS selectors cheat sheet&lt;/a&gt; that links to the MDN Docs reference of each CSS selector.&lt;/p&gt;

</description>
      <category>css</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
