<?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: Prashanth P Wagle</title>
    <description>The latest articles on DEV Community by Prashanth P Wagle (@prashanthwagle).</description>
    <link>https://dev.to/prashanthwagle</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%2F353298%2Ff48f052b-c7d1-4416-a385-261ba8d931a0.jpeg</url>
      <title>DEV Community: Prashanth P Wagle</title>
      <link>https://dev.to/prashanthwagle</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prashanthwagle"/>
    <language>en</language>
    <item>
      <title>CSS Pseudo Selectors: What are they?</title>
      <dc:creator>Prashanth P Wagle</dc:creator>
      <pubDate>Sun, 15 Nov 2020 12:07:22 +0000</pubDate>
      <link>https://dev.to/prashanthwagle/css-pseudo-selectors-what-are-they-479f</link>
      <guid>https://dev.to/prashanthwagle/css-pseudo-selectors-what-are-they-479f</guid>
      <description>&lt;p&gt;Cover Photo by Tirachard Kumtanom from Pexels&lt;/p&gt;

&lt;h1&gt;
  
  
  Basics
&lt;/h1&gt;

&lt;p&gt;Selectors are patterns used to identify select the elements in the DOM for which styles would be applied. In fact, they are the first part of any CSS rule. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h1{
  background-color: magenta;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;h1 is the selector in the above example&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Out of various patterns used to select a particular DOM element from the tree, pseudo-elements and pseudo-classes &lt;em&gt;(obviously!)&lt;/em&gt; are one of its kind which is drastically different than the common ones. (FYI: Type, class, and ID selectors, Attribute selectors, Attribute selectors are the other types).&lt;/p&gt;

&lt;h1&gt;
  
  
  Pseudo-Classes
&lt;/h1&gt;

&lt;p&gt;Pseudo-classes are identified with a single colon according to the CSS3 specification. A pseudo-class is a selector that selects elements that are in a specific state. For instance, they may be used to change the properties of the first letter or a line of an element. &lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;element:name{
   //properties
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;li:first-child{
  color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the first-child selector selects the li element which is the first child among its siblings. This selector would be helpful in styling without having to add/delete classes via Javascript if more sibling elements are added.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Pseudo-Classes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudo-classes which are user-action based&lt;/strong&gt;&lt;br&gt;
The styling will be based on the action carried out by the users.&lt;br&gt;
Eg: &lt;code&gt;:hover&lt;/code&gt; - applies when the user hovers over the element, &lt;code&gt;:focus&lt;/code&gt; - applies when the user selects the input element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudo-Classes which are based on the position&lt;/strong&gt;&lt;br&gt;
The styling will be based on the position of the element in the DOM tree relative to its sibling/parents.&lt;br&gt;
Eg: &lt;code&gt;:first-child&lt;/code&gt; – selects the first element within a parent. &lt;code&gt;:nth-child()&lt;/code&gt; – selects elements based on a simple provided algebraic expression. A few instances are &lt;code&gt;:nth-child(2n)&lt;/code&gt; wherein all even children are selected and &lt;code&gt;:nth-child(2n+1)&lt;/code&gt; wherein all odd children are selected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Other Pseudo-Classes&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;:not()&lt;/code&gt; – removes elements from an existing matched set that match the selector inside the parameter of :not().&lt;br&gt;
&lt;code&gt;:empty&lt;/code&gt; - selects the pseudo-classes which have no text or child elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Pseudo-Elements
&lt;/h1&gt;

&lt;p&gt;Pseudo-Elements are used to style specific parts of the element or elements. According to the CSS3 specification they are identified with double-colon (::). For example, they may be used to change the properties of the first letter or a line of an element.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;element::name{
   //properties
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p::first-line {
  color: #5e5e5e;
}

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

&lt;/div&gt;



&lt;p&gt;The commonly used pseudo-elements include:&lt;br&gt;
&lt;code&gt;::first-line&lt;/code&gt; to style the first line of the element.&lt;br&gt;
&lt;code&gt;::first-letter&lt;/code&gt; to style the first letter of the element.&lt;br&gt;
&lt;code&gt;::before&lt;/code&gt; to insert some content before the element.&lt;br&gt;
&lt;code&gt;::after&lt;/code&gt; to insert some content after the element.&lt;br&gt;
&lt;code&gt;::selection&lt;/code&gt; to style the content selected by the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Examples of Psuedo-Elements
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;::file-selector-button&lt;/code&gt;, &lt;code&gt;::backdrop&lt;/code&gt;, &lt;code&gt;::cue&lt;/code&gt;, &lt;code&gt;::cue-region&lt;/code&gt;, &lt;code&gt;::grammar-error&lt;/code&gt;, &lt;code&gt;::part()&lt;/code&gt;, &lt;code&gt;::placeholder&lt;/code&gt;, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Note:
&lt;/h1&gt;

&lt;p&gt;The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.&lt;/p&gt;

&lt;p&gt;The double-colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt from W3C to distinguish between pseudo-classes and pseudo-elements.&lt;/p&gt;

&lt;p&gt;For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-elements.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
