<?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: Edozie Onyeanusi</title>
    <description>The latest articles on DEV Community by Edozie Onyeanusi (@fynbarr).</description>
    <link>https://dev.to/fynbarr</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%2F341376%2F8a6082b3-84c2-4f5e-abfa-39a0e38f6fcf.jpg</url>
      <title>DEV Community: Edozie Onyeanusi</title>
      <link>https://dev.to/fynbarr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fynbarr"/>
    <language>en</language>
    <item>
      <title>Understanding CSS Selectors: Attribute Selectors</title>
      <dc:creator>Edozie Onyeanusi</dc:creator>
      <pubDate>Sat, 06 Feb 2021 21:41:26 +0000</pubDate>
      <link>https://dev.to/fynbarr/understanding-css-selectors-attribute-selectors-2gj9</link>
      <guid>https://dev.to/fynbarr/understanding-css-selectors-attribute-selectors-2gj9</guid>
      <description>&lt;p&gt;Hello World!😀 Welcome to the fifth article in my CSS series, Understanding CSS Selectors. In this article, we would be delving into attribute selectors; what they are and how to use them. Alright, let’s head right into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Attribute Selectors?&lt;/strong&gt;&lt;br&gt;
Attribute Selectors are a class of CSS selectors which functions by matching or selecting elements based on the presence of an attribute or the value of an attribute.&lt;/p&gt;

&lt;p&gt;For some of us who might not be too familiar with HTML, attributes are special words used in the opening tags to define additional or special properties or characteristics of the element. Attributes consist of a name and value pair, &lt;code&gt;name=”value”&lt;/code&gt;. The value is always contained in quotation marks, but there are some attributes which don’t have the name-value pair, these attributes are called Boolean attributes. Examples of some common ones are &lt;code&gt;checked&lt;/code&gt;, &lt;code&gt;disabled&lt;/code&gt;, &lt;code&gt;readonly&lt;/code&gt;, &lt;code&gt;required&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;Just like we saw in the last article where we discussed pseudo-class selectors, attribute selectors can be further divided into different types with each differing on how the selector is written, whilst still following the general syntax for declaring stylings which is the selector coming first followed by the property and value in the curly brackets. Let’s discuss these different types of attribute selectors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select all the elements that have the specified attribute and applies the CSS property to that attribute.&lt;br&gt;
The example below selects every &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; element with &lt;code&gt;href&lt;/code&gt; attribute in the HTML file and colours it red:&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;href&lt;/span&gt;&lt;span class="o"&gt;]&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute = “value”]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select all the elements which have a specific attribute which has a value that is exactly the same as the specified value and applies the CSS property to that attribute.&lt;br&gt;
The example below selects every &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; element  with &lt;code&gt;href=”#”&lt;/code&gt; attribute in the HTML file:&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;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"#"&lt;/span&gt;&lt;span class="o"&gt;]&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;purple&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute~=”value”]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select all the elements that have the specified attribute which, in turn, has a value which is exactly the same as one of the values among a list of values separated by whitespace.&lt;br&gt;
The example below selects every element with &lt;code&gt;class&lt;/code&gt; attribute containing a whitespace-separated list with the word &lt;strong&gt;dogs&lt;/strong&gt; in the HTML file:&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;div&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;~=&lt;/span&gt;&lt;span class="s1"&gt;"dogs"&lt;/span&gt;&lt;span class="o"&gt;]&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;brown&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;The example above will match elements with &lt;code&gt;class = “dogs”&lt;/code&gt;, &lt;code&gt;class = “animals dogs”&lt;/code&gt; and &lt;code&gt;class = “dogs animals”&lt;/code&gt; but not &lt;code&gt;class = “dogs-animals”&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute|="value"]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select any element which has the specified attribute whose value is exactly the same as the value or it is followed immediately by a hyphen.&lt;br&gt;
The example below selects every element with &lt;code&gt;class&lt;/code&gt; attribute whose values begin with the word &lt;strong&gt;dogs&lt;/strong&gt; or is followed by a hyphen(-), &lt;strong&gt;"dogs-purebred"&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="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;|=&lt;/span&gt;&lt;span class="s1"&gt;"dogs"&lt;/span&gt;&lt;span class="o"&gt;]&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="n"&gt;royal&lt;/span&gt; &lt;span class="no"&gt;blue&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute^=”value”]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select any element which has the specified attribute whose value begins with the value that was specified in the selector. The value does not have to be the whole word or a whole word.&lt;br&gt;
The example below selects every element with a &lt;code&gt;class&lt;/code&gt; attribute which has a value that begins with &lt;strong&gt;“seek”&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="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;^=&lt;/span&gt;&lt;span class="s1"&gt;"seek"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;italic&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute$=”value”]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is the opposite of the [attribute^=”value”] selector and is used to select any element whose specified attribute has a value which ends with the value specified in the selector. &lt;br&gt;
The example below selects every element with a &lt;code&gt;class&lt;/code&gt; attribute which has a value that ends with &lt;strong&gt;“er”&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="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"er"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;oblique&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute*=”value”]&lt;/strong&gt; Selector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is used to select elements whose attribute has the specified value in the selector anywhere in the attribute value in the HTML file.&lt;br&gt;
The example below selects every element that has a class attribute which contains &lt;strong&gt;“and”&lt;/strong&gt; anywhere in the string which is the value of the attribute.&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;div&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;&lt;span class="s1"&gt;"and"&lt;/span&gt;&lt;span class="o"&gt;]&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute &lt;em&gt;operator&lt;/em&gt; value &lt;em&gt;i&lt;/em&gt;]&lt;/strong&gt; Selector &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This type of attribute selector is similar to any of the selectors that have already been mentioned with an added feature of adding an i or I just before the closing brackets which causes the value specified in the selector to be compared irrespective of its case i.e. it is not case-sensitive. This attribute selector is supported by all browsers except Internet Explorer.&lt;br&gt;
In the example below, all &lt;strong&gt;&lt;a&gt;&lt;/a&gt;&lt;/strong&gt; tags with &lt;strong&gt;href=”anon”&lt;/strong&gt; will be selected regardless of capitalization. So, if the href value was Anon, aNon or anything else with the same letters but different capitalization, it would be valid.&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;href&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;&lt;span class="s1"&gt;"anon"&lt;/span&gt; &lt;span class="nt"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&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;yellow&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;[attribute &lt;em&gt;operator&lt;/em&gt; value &lt;em&gt;s&lt;/em&gt;]&lt;/strong&gt; Selector
This type of attribute selector is the opposite of the [attribute operator value i] selector in that it compares the value specified in the selector as regards to its exact case, i.e. it is case-sensitive. This attribute selector is however still in experimental stages and is not compatible with most browsers except with the current version of the Firefox Browser on Laptop and Mobile (Firefox 66.0).
In the example below, all &lt;em&gt;&lt;a&gt;&lt;/a&gt;&lt;/em&gt; tags with exactly &lt;strong&gt;href*=”AnOn”&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&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;href&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;&lt;span class="nt"&gt;AnOn&lt;/span&gt; &lt;span class="nt"&gt;s&lt;/span&gt;&lt;span class="o"&gt;]&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;pink&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;Why use Attribute Selectors?&lt;/strong&gt;&lt;br&gt;
If you are a bit familiar with CSS and you may have looked at some CSS code of other people, you would come to notice that the use of attribute selectors is not really common. Many people who write CSS do not feel the need to use the attribute selectors arguing that the addition of another class or ID  would just implement the same styling, but what this would go on to do is create a bogus CSS file with a lot of class or ID selectors which intention one would not be able to readily decipher from just looking at the codebase. &lt;br&gt;
One area where attribute selectors are very useful is in styling similar elements which have repeating attributes, let’s see the example below, we have an unordered list of anchor tags with similar &lt;strong&gt;href&lt;/strong&gt; values but which have been differentiated by using the &lt;strong&gt;rel&lt;/strong&gt; attribute.&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;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href = &lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;rel = &lt;/span&gt;&lt;span class="s"&gt;"asean"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Myanmar&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href = &lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;rel = &lt;/span&gt;&lt;span class="s"&gt;"ecowas"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Mali&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href = &lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;rel = &lt;/span&gt;&lt;span class="s"&gt;"asean"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Cambodia&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href = &lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Kurdistan&lt;span class="nt"&gt;&amp;lt;/a&amp;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;p&gt;We can then go on style the list items based on the &lt;strong&gt;rel&lt;/strong&gt; attrribute&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="nt"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"asean"&lt;/span&gt;&lt;span class="o"&gt;]&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="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;li&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;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"ecowas"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;italics&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;One tricky part is that if the &lt;strong&gt;rel&lt;/strong&gt; attribute involved has more than one attribute value, the conventional way of styling an attribute won’t work but you’ll have to use the &lt;strong&gt;‘~=’&lt;/strong&gt; combinator instead.&lt;/p&gt;

&lt;p&gt;With all of these, we have finally come to the end of this CSS series and I believe it was very beneficial to you. It means a lot to me that you read all the articles to the very end, I am very grateful. I believe this series handled the fundamentals for navigating CSS, with all the knowledge here, you can go to create magic with CSS.&lt;/p&gt;

&lt;p&gt;Go Legend. Ciao! ❤️&lt;/p&gt;

</description>
      <category>css</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>UNDERSTANDING CSS SELECTORS: PSEUDO-CLASS SELECTORS</title>
      <dc:creator>Edozie Onyeanusi</dc:creator>
      <pubDate>Sat, 12 Dec 2020 21:28:26 +0000</pubDate>
      <link>https://dev.to/fynbarr/understanding-css-selectors-pseudo-class-selectors-e6g</link>
      <guid>https://dev.to/fynbarr/understanding-css-selectors-pseudo-class-selectors-e6g</guid>
      <description>&lt;p&gt;Hello, World! Welcome to the fourth article in my CSS series, &lt;strong&gt;Understanding CSS Selectors&lt;/strong&gt;. In this article, we would be delving into the pseudo-class selectors; what they are, how to use them and differentiating them from their close cousins, pseudo-elements. Alright, let’s jump right in.&lt;/p&gt;

&lt;p&gt;A CSS pseudo-class is a keyword added to a selector so as to style a specific characteristic or state of that selector. Some of the pseudo-class are &lt;code&gt;:hover&lt;/code&gt;, &lt;code&gt;:focus&lt;/code&gt;, &lt;code&gt;:nth-child&lt;/code&gt; and &lt;code&gt;:last-of-type&lt;/code&gt;. It can be used to style an element when the mouse hovers over it, to style an element on focus or style visited and unvisited links. As we can see from the examples are above, it appears that these keywords all seem to be preceded by a colon (:). In most case scenarios, the pseudo-class is used along with a selector, the selector comes first and then the pseudo-class along with the colon. Below is the general syntax for its use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;General Syntax for pseudo-class selectors&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4kishlrema3uzeu376g8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4kishlrema3uzeu376g8.png" alt="General Syntax for pseudo-class selectors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pseudo-class selectors can be divided into 5 or 6 types, depending on if you don’t regard the pseudo-elements as a pseudo-class at all. These types are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Link related pseudo-class selectors&lt;/strong&gt;&lt;br&gt;
The most frequently used ones which are concerned with styling anchor tags at their different states, these pseudo-classes are :link, &lt;code&gt;:visited&lt;/code&gt;, &lt;code&gt;:hover&lt;/code&gt; and &lt;code&gt;:active&lt;/code&gt;. To style the link appropriately, you should go with the &lt;em&gt;LVHA&lt;/em&gt; order which is to put the &lt;code&gt;:link&lt;/code&gt; pseudo-class first, followed by the &lt;code&gt;:visited&lt;/code&gt; then &lt;code&gt;:hover&lt;/code&gt; and &lt;code&gt;:active&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Input and link related pseudo-class selectors&lt;/strong&gt;&lt;br&gt;
This allows styling for the input html elements by selecting the input elements based on some state or attribute of the element. Some of them include &lt;code&gt;:focus&lt;/code&gt;, &lt;code&gt;:checked&lt;/code&gt;, &lt;code&gt;:target&lt;/code&gt;, &lt;code&gt;:required&lt;/code&gt;, &lt;code&gt;:disabled&lt;/code&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Position and Number-based pseudo-class selectors&lt;/strong&gt;&lt;br&gt;
This allows for styling based off of their order or position on the document tree rather than just its type, id or attribute. They include &lt;code&gt;:root&lt;/code&gt; which targets the root element of the document tree, the siblings pseudo-selectors which include the &lt;code&gt;:first-child&lt;/code&gt;, &lt;code&gt;:last-child&lt;/code&gt;, &lt;code&gt;:nth-child()&lt;/code&gt;, &lt;code&gt;:nth-last-child()&lt;/code&gt;, &lt;code&gt;:only-child&lt;/code&gt;, the &lt;code&gt;:first-of-type&lt;/code&gt;, &lt;code&gt;:last-of-type&lt;/code&gt;, &lt;code&gt;:nth-of-type&lt;/code&gt; and &lt;code&gt;:only-of-type&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Relational pseudo-class selectors&lt;/strong&gt;&lt;br&gt;
This category includes the :empty pseudo-class and the negation pseudo-class, &lt;code&gt;:not()&lt;/code&gt; which takes an argument which is filtered out from the selection to be made. Unlike the general syntax for pseudo-class selectors, the :not() pseudo-class may be chained or not be chained but it may not have another negation pseudo-class as its argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Text related pseudo-class selectors/pseudo-element&lt;/strong&gt;&lt;br&gt;
This category as the name implies is concerned with text. Here we have the &lt;code&gt;::first-letter&lt;/code&gt;, &lt;code&gt;::first-line&lt;/code&gt;, &lt;code&gt;::selection&lt;/code&gt; and then the &lt;code&gt;:lang()&lt;/code&gt; which styles elements if it matches the language attribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content related pseudo-elements&lt;/strong&gt;&lt;br&gt;
This category of pseudo-class selectors are used to create new elements that are not specified in the markup of the document and can be manipulated much like a regular element. The &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo-elements belong to this class. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pseudo-elements behave in a similar way with pseudo-classes, however they act as if you had added a whole new HTML element into the markup, rather than applying a class to existing elements. Pseudo-elements start with a double colon (::). This :: notation (double colon notation) was introduced by the W3C in order to establish a distinction between pseudo-classes and pseudo-elements. For backwards compatibility, the single-colon syntax is acceptable for pre-CSS3 selectors. So, :after is a pseudo-class and ::after is a pseudo-element. You can use only one pseudo-element in a selector. It must appear after the simple selectors in the statement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;General Syntax for pseudo-elements selectors&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frqzj4zdhs68f5x13p9ip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frqzj4zdhs68f5x13p9ip.png" alt="General Syntax for Pseudoelements"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are lots of other psuedo-classes and psuedo-elements which are not listed here because of they can’t easily fit into the categories and I did not explain them individually since we are not going into much details but a good resource to read more about these selectors would be to check the MDN Web Docs and read up on CSS pseudo-classes, the website explains in details each selector and how it should be used.&lt;/p&gt;

&lt;p&gt;To check for browser compatibility of the selectors, you can always use &lt;a href="https://caniuse.com/" rel="noopener noreferrer"&gt;caniuse.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading, if you found this article helpful, please do well to share, it would probably be beneficial to someone else too.&lt;/p&gt;

&lt;p&gt;Thank you once more for reading to the end, see in you in the next article where we would be talking about the Attribute Selectors. &lt;/p&gt;

&lt;p&gt;Ciao! ❤️&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>css</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>UNDERSTANDING CSS SELECTORS: COMBINATOR SELECTORS</title>
      <dc:creator>Edozie Onyeanusi</dc:creator>
      <pubDate>Wed, 08 Jul 2020 11:57:33 +0000</pubDate>
      <link>https://dev.to/fynbarr/understanding-css-selectors-combinator-selectors-2c17</link>
      <guid>https://dev.to/fynbarr/understanding-css-selectors-combinator-selectors-2c17</guid>
      <description>&lt;p&gt;Hello, World! Welcome to the third article in my CSS series, Understanding CSS Selectors, a series with aim to give a more enlightening explanation about the use of selectors as selectors are pivotal in carrying out a good project with CSS. In the previous articles, I have given explanations about what selectors are, talked about the 5 types of selectors and we have talked about the four types of the basic CSS selectors  and how they are used and how to navigate specificity with them.&lt;/p&gt;

&lt;p&gt;In this article, we would be looking at the &lt;strong&gt;combinator selectors&lt;/strong&gt; and how they are utilized in adding styles to the webpage. As we have stated in the previous articles, selectors are patterns used to select the elements you are trying to style. A selector can be used singularly as a simple selector or as a complex selector, in situations, where styling becomes less straightforward which is where combinators now come in.  A combinator is a character that combines two or more selectors in a way that gives them a useful relationship to each other and the location of content in the HTML document. For example, we can target only a particular child element in a parent element or we can target a certain element and it’s adjacent sibling.&lt;/p&gt;

&lt;p&gt;CSS combinators are subdivided into four classes; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Descendant Combinator ( )&lt;/li&gt;
&lt;li&gt;Child Combinator (&amp;gt;)&lt;/li&gt;
&lt;li&gt;Adjacent Sibling Combinator (+)&lt;/li&gt;
&lt;li&gt;General Sibling Combinator (~)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We would be looking at each other individually to appropriately explain them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Descendant Combinator&lt;/strong&gt; ( )&lt;br&gt;
    The descendant selector is typically represented by a single space ( ) character and is used between the two selectors such that the elements of the second selector will be styled if the first selector corresponds with their ancestor name (a parent, parent’s parent, parent’s parent’s parent, etc.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7katMLky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/7cbnftjgbdgfj3uktmcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7katMLky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/7cbnftjgbdgfj3uktmcf.png" alt="Alt Text" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ancestor selector represents any structurally superior element in the hierarchy, this can be a parent or the parent of a parent and so on. The descendant selector represents any of the descendant element in the hierarchy, it doesn't have to be a direct child.&lt;/p&gt;

&lt;p&gt;Let's look at an example of the descendant combinator usage&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gw5WezFh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/00scr7daiq4viigg5uud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gw5WezFh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/00scr7daiq4viigg5uud.png" alt="Alt Text" width="800" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---ljxNxE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/7efvc589hyh5h07tkhdb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---ljxNxE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/7efvc589hyh5h07tkhdb.png" alt="Alt Text" width="800" height="371"&gt;&lt;/a&gt;&lt;br&gt;
So from the CSS code above, we would expect only list items with an &lt;em&gt;a&lt;/em&gt; tag to be coloured blue as the other list items do not have the &lt;em&gt;a&lt;/em&gt; tag even though they are children of the &lt;em&gt;ul&lt;/em&gt; tag.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Child Combinator&lt;/strong&gt; (&amp;gt;)&lt;br&gt;
    The child selector is represented by a greater than (&amp;gt;) character placed between two selectors. It is used to show that the second selector is an immediate child of the first selector.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q3aQRB9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2ulomxff2txcm6mockhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q3aQRB9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2ulomxff2txcm6mockhw.png" alt="Alt Text" width="800" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The child selector is quite stricter from the descendant combinator because it would not style the elements of descendant tags that are further down the family tree even though they are the same with the second selector.&lt;/p&gt;

&lt;p&gt;Let's look at an example of the child combinator usage&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v7JBFcLJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/f02ettxrurjmw517ob0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v7JBFcLJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/f02ettxrurjmw517ob0e.png" alt="Alt Text" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rVHR8YV---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3gcinylye56x6zr28r55.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rVHR8YV---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3gcinylye56x6zr28r55.png" alt="Alt Text" width="800" height="312"&gt;&lt;/a&gt;&lt;br&gt;
If you run the code above, we would get the first paragraph in blue validating what we have been saying the child combinator but the second paragraph will be coloured red. It would seem that the addition of the combinators increases the specificity of the style properties but this is not necessarily the truth but the reason that style from the &lt;em&gt;p&lt;/em&gt; selector wasn't effected by the direct descendant &lt;em&gt;p&lt;/em&gt; elements was because the specificity of the &lt;em&gt;div &amp;gt; p&lt;/em&gt; was higher because it involves two selectors in contrast to the second selector which involves just one selector.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Adjacent Sibling Combinator&lt;/strong&gt; (+)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dtChPdML--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/18w34mje0q43i92krs99.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dtChPdML--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/18w34mje0q43i92krs99.png" alt="Alt Text" width="800" height="290"&gt;&lt;/a&gt;&lt;br&gt;
 The adjacent sibling combinator is represented by the plus (+) character placed between two selectors. When the adjacent sibling combinator is used, it targets the second selector which is coming after the first selector. It is used to select the element that is placed right next to another element at the same level of the hierarchy, it's from this property that the adjective  ‘adjacent’ is used which means ‘next to’ or ‘adjoining’.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j7FLnj8k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/mnogqmvol03635bwgoc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j7FLnj8k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/mnogqmvol03635bwgoc4.png" alt="Alt Text" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wo54ojEo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/k3haohoxqmbcht8eyk10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wo54ojEo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/k3haohoxqmbcht8eyk10.png" alt="Alt Text" width="800" height="268"&gt;&lt;/a&gt;&lt;br&gt;
From the stylesheet, the styling that would only be effected on the &lt;em&gt;p&lt;/em&gt; tags are those ones that right next to the &lt;em&gt;h3&lt;/em&gt; tags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The General Sibling Combinator&lt;/strong&gt; (~)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rrUEfF4y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/4d5f5avy53f81q2m2xrh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rrUEfF4y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/4d5f5avy53f81q2m2xrh.png" alt="Alt Text" width="800" height="290"&gt;&lt;/a&gt;&lt;br&gt;
    The fourth combinator is the general sibling combinator which is represented by the Tilde operator (~) character. Like the adjacent sibling combinator, the general sibling combinator doesn’t involve the parent but rather sibling tags which are on the same level in the hierarchy. The general sibling combinator selects siblings anywhere within the parent container, it necessarily doesn’t have to be adjacent siblings but the target selector has to come after the first selector.  In other words, if you want to select siblings of an element even if they are not directly adjacent, then you can use the general sibling combinator (~).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fW-ecB6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ez1whuo9ll95uqv26rnx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fW-ecB6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ez1whuo9ll95uqv26rnx.png" alt="Alt Text" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FOzEoixo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vsj5iojbkudintlf1cjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FOzEoixo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vsj5iojbkudintlf1cjn.png" alt="Alt Text" width="800" height="268"&gt;&lt;/a&gt;&lt;br&gt;
If you run the code, we have from above you would be able to see that all the &lt;em&gt;p&lt;/em&gt; tag elements were styled except from the first one although it is at the same level with the &lt;em&gt;h3&lt;/em&gt; tags but it doesn't come after it. &lt;/p&gt;

&lt;p&gt;There is also a somewhat fifth combinator called the &lt;strong&gt;column combinator&lt;/strong&gt; which represented by || between two CSS selectors. It matches only those elements matched by the second selector that belong to the column elements matched by the first. This technology is still in the working draft and is not supported by any browser yet. As for the other combinators, they can supported by almost every version of browsers currently in use. To see the versions that support its use, you can visit &lt;a href="http://www.caniuse.com"&gt;www.caniuse.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;According to the MDN Web Docs on Specificity, the Universal selector (*), &lt;strong&gt;combinators (+, &amp;gt;, -, ' ', ||)&lt;/strong&gt; and the negation pseudoclass(:not()) have no effect on specificity. (The selectors declared inside :not() do, however.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are Combinators really necessary when we can use ID or Class selectors to directly target some elements?&lt;/strong&gt;🤷🏾‍♂️🤷🏾‍♂️&lt;br&gt;
The truth is that IDs and Class selectors can directly target elements, which gives us a functional website but they are not efficient enough to handle a complete design. Using the combinators will also help us achieve more flexibility with less code compared to using just the ID or class selectors.&lt;/p&gt;

&lt;p&gt;CSS Combinators are great for reducing the amount of extra classes and ID’s in your HTML. The least amount of extra attributes you have in the HTML the better. It will not only reduce the page size but also make maintenance easier.&lt;/p&gt;

&lt;p&gt;Thank you once more for reading to the end, see in you in the next article where we would be talking about the &lt;strong&gt;Pseudoclass Selectors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ciao! ❤️&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>css</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>UNDERSTANDING CSS SELECTORS: CLASS SELECTORS AND ID SELECTORS</title>
      <dc:creator>Edozie Onyeanusi</dc:creator>
      <pubDate>Thu, 18 Jun 2020 15:05:07 +0000</pubDate>
      <link>https://dev.to/fynbarr/understanding-basic-css-selectors-class-selectors-and-id-selectors-cbd</link>
      <guid>https://dev.to/fynbarr/understanding-basic-css-selectors-class-selectors-and-id-selectors-cbd</guid>
      <description>&lt;p&gt;Hello, World! Welcome to the second article in my CSS series, Understanding CSS Selectors, a series with aim to help explain more about the use of selectors as know that selectors are pivotal in creating a good project with CSS. If you missed the first article in this series, which was on the basic selectors; the universal selector and the type selectors, check it out in this link, &lt;a href="https://dev.to/fedonye/understanding-basic-css-selectors-universal-selectors-and-tag-selectors-3nd1"&gt;UNDERSTANDING CSS SELECTORS: UNIVERSAL SELECTORS AND TYPE SELECTORS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;From the last article, we looked at Universal and Type selectors, as earlier stated, and in this article we would be proceeding with class and ID selectors. So let’s jump right into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class Selectors&lt;/strong&gt;, like the name suggests, is a selector that works with the corresponding value or class name of the class attribute. The class attribute which is one of the global attributes, specifies one or more class names or values which is then used in the stylesheet to style the elements with the corresponding class name. Class selectors are represented with a period or full-stop (.) before the class name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--72NwG83I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/mbx4yzfktkcwc5a6px70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--72NwG83I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/mbx4yzfktkcwc5a6px70.png" alt="Alt Text" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the class selector, we just have to give a value to the class attribute and make sure that our class name in the stylesheet corresponds to the name in the HTML tag. Note that the class name is principally for linking elements in your HTML file so that they could be styled as a group in the stylesheet and they do not in any way influence the semantic structure of the HTML document. &lt;/p&gt;

&lt;p&gt;Class names are also case-sensitive and allow for the use of hyphens or underscore in the place of whitespaces. Class selectors have a specificity value of (0, 0, 1, 0) which means that they will be prioritized ahead of type selectors but would have ID selectors prioritized before them. Specificity increases when the class selectors are combined or when the selector is limited to a specific element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ID selectors&lt;/strong&gt; (pronounced as &lt;em&gt;eye-dee&lt;/em&gt;) are quite similar to how classes are denoted, except that they are represented by an octothorpe, a pound sign or hash character(#), attached to its id name rather than a period and an id attribute attached to the HTML element instead of the class attribute.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a_Lq6cpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/sc6zqcfb17anzzau08ev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a_Lq6cpK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/sc6zqcfb17anzzau08ev.png" alt="Alt Text" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like the class selector, the ID selector is case-sensitive, must not start with a number and must exactly match across HTML, CSS and Javascript but in contrast to the class selector which can be used to select more than one element, the ID selector can only be used to select one unique element which means an HTML element with two or more ids would not be validated.&lt;/p&gt;

&lt;p&gt;On the specificity scale, ID selectors rank very high, they have a specificity which is written as (0, 1, 0, 0). This means that their styles would override other selectors written with just tags or classes. However, considering CSS best practices, many devs would advise that you try not to use ID selectors as much as possible, this article, &lt;a href="https://dev.to/clairecodes/reasons-not-to-use-ids-in-css-4ni4"&gt;Reasons not to use IDs in CSS&lt;/a&gt; by &lt;a href="https://twitter.com/ClaireParkerPen"&gt;Claire Parker-Jones&lt;/a&gt; sheds more light on it.&lt;/p&gt;

&lt;p&gt;Let's look at some practical examples of class and ID selectors, below are the HTML and CSS codes for a simple example I created.&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eWWmxQDA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/txtzvd3jyi1ig01rlfwk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eWWmxQDA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/txtzvd3jyi1ig01rlfwk.jpg" alt="Alt Text" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oT-Jb-PT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/tzdtotwiiam76bl55h2p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oT-Jb-PT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/tzdtotwiiam76bl55h2p.jpg" alt="Alt Text" width="800" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and the &lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MQ28dHIL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/jxjuyymgdrih2g9htxc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MQ28dHIL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/jxjuyymgdrih2g9htxc1.png" alt="Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the source codes above, we would be able to see that I styled the title and story divs separately and together for the styles that they would share in common. If you look at line 11 on the HTML, you would notice it also had a ID attribute along with the class attribute, then go on to the stylesheet and compare the style properties on lines 21 &amp;amp; 25. On line 21, we see a &lt;em&gt;margin&lt;/em&gt; property for the title class but in the output, we see that this property was overridden and that of line 25 which is contained within an ID selector is taken into effect as it bears a &lt;em&gt;margin-top&lt;/em&gt; property, this is due to higher specificity of the ID selectors (and because the latter styles in CSS override the former even if they have the same specificity).&lt;/p&gt;

&lt;p&gt;Well, we have come to the end of today's article on class and ID selectors. In the next article in this series, we would be looking at the combinator selectors and how they are used.&lt;/p&gt;

&lt;p&gt;Thank you for reading to the end, if you found this article helpful, please do well to share, it would probably be beneficial to someone else too. If you are interested in reading more articles, you can follow me to receive notifications when I publish more.&lt;/p&gt;

&lt;p&gt;Thank you once more, see you in the next article where we would talking about the &lt;strong&gt;Combinator Selectors&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Ciao! ❤️&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>css</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>UNDERSTANDING CSS SELECTORS: UNIVERSAL SELECTORS AND TYPE SELECTORS.</title>
      <dc:creator>Edozie Onyeanusi</dc:creator>
      <pubDate>Tue, 09 Jun 2020 21:56:47 +0000</pubDate>
      <link>https://dev.to/fynbarr/understanding-basic-css-selectors-universal-selectors-and-tag-selectors-3nd1</link>
      <guid>https://dev.to/fynbarr/understanding-basic-css-selectors-universal-selectors-and-tag-selectors-3nd1</guid>
      <description>&lt;p&gt;Hello World! Welcome to the first article in my CSS series, &lt;strong&gt;Understanding CSS Selectors&lt;/strong&gt;, the aim of this series is to give an in-depth explanation of the basic CSS selectors as understanding them properly is key to understanding and navigating CSS easily.&lt;/p&gt;

&lt;p&gt;CSS stands for Cascading Style Sheets which is used to add styles to documents written in a markup language. CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript. Selectors are integral to CSS stylesheet, they are a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3qu2v88oitjp4xsk7wtq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3qu2v88oitjp4xsk7wtq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="//www.w3schools.com"&gt;w3schools.com&lt;/a&gt;, CSS selectors are divided into five different categories:&lt;br&gt;
&lt;strong&gt;1. Basic or Simple Selectors&lt;/strong&gt;&lt;br&gt;
They select elements based on element name, id or class. They include:&lt;br&gt;
a. Universal selector (*) that basically selects all the HTML elements on a page.&lt;br&gt;
b. Type selectors that select an HTML element with the specified tag name.&lt;br&gt;
c. Class selectors (.className) that select a group of HTML elements having the same class name.&lt;br&gt;
d. Id selectors (#id) that select an HTML element with a unique id.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Combinator Selectors&lt;/strong&gt;&lt;br&gt;
These selectors make use of a combinator when used to add styles. There are four different combinators. They are:&lt;br&gt;
a. descendant selector (&lt;em&gt;space&lt;/em&gt;)&lt;br&gt;
b. child selector (&amp;gt;)&lt;br&gt;
c. adjacent sibling selector (+)&lt;br&gt;
d. general sibling selector (~)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Pseudo-class Selectors&lt;/strong&gt;&lt;br&gt;
These selectors are used to select and style an HTML element and define its special behaviour/state. These states cannot be defined by basic or combinator selectors. These states are defined with pseudo-classes such as &lt;em&gt;:hover&lt;/em&gt;, &lt;em&gt;:visited&lt;/em&gt;, &lt;em&gt;:active&lt;/em&gt;, &lt;em&gt;:focus&lt;/em&gt;, &lt;em&gt;:nth-child(n)&lt;/em&gt;, &lt;em&gt;:root&lt;/em&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Pseudo-elements Selectors&lt;/strong&gt;&lt;br&gt;
These selectors are used to style specified parts of an element or to define elements that are not part of the markup. They are  &lt;em&gt;::before&lt;/em&gt;, &lt;em&gt;::after&lt;/em&gt;, &lt;em&gt;::first-letter&lt;/em&gt;, &lt;em&gt;::first-line&lt;/em&gt; and &lt;em&gt;::selection&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Attribute Selectors&lt;/strong&gt;&lt;br&gt;
These allow us to style HTML elements with certain attributes. For example&lt;br&gt;
a. an [input = ”type”] will select an input element of type “text”.&lt;br&gt;
b. an [title ~= ”flower”] will select all elements with a title attribute that contains a space-separated list of words, one of which is “flower”.&lt;br&gt;
c. an [class |= ”top”] will select all elements with a class attribute value that begins with “top”.&lt;br&gt;
d. an [class ^= ”top”] will select all elements with a class attribute value that begins with “top”. The value does not have to be a whole word.&lt;br&gt;
e. an [class $= ”test”] will select all elements with a class attribute value that ends with “test”. The value does not have to be a whole word.&lt;br&gt;
f. an [class *= ”te”] will select all elements with a class attribute value that contains “te”. The value does not have to be a whole word.&lt;/p&gt;

&lt;p&gt;In this first article, we are going to be looking at one of the basic selectors called the Universal Selectors and Type Selectors and I’d try to make it as simple as possible.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Universal Selector&lt;/strong&gt; is represented by the asterisk (*) character. It is used as a wildcard to select any and all types of elements in an HTML page. If we use just the asterisk, then all the elements get selected irrespective of the parent-child relation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frwy8ie2zwvp9gtj11g4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frwy8ie2zwvp9gtj11g4x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we use the asterisk while selecting a parent tag like the example below, then all the child elements will be selected&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fldct11vt1hgt3uf432pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fldct11vt1hgt3uf432pp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Type selector&lt;/strong&gt; (sometimes referred to as an Element Selector) is represented by the corresponding node name of the element such as p, div, span, a and h1 tags. It is used to select every instance of the element attached to it on the HTML page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjunp4014q2pirag9wcdk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjunp4014q2pirag9wcdk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
It is worthy of note that the Type selectors are on the lowest level on the specificity cascade; Specificity is the means by which a browser decides which CSS property values are the most relevant to an element and therefore will be applied. This means that almost any other styling will override the style applied via a Type Selector alone and for the Universal selector, it doesn’t hold any specificity value at all. You can read up more on CSS specificity rules by visiting &lt;a href="https://css-tricks.com/specifics-on-css-specificity/" rel="noopener noreferrer"&gt;Specifics on CSS Specificity&lt;/a&gt; by Chris Coyier and &lt;a href="https://pawelgrzybek.com/css-specificity-explained/" rel="noopener noreferrer"&gt;CSS Specificity explained&lt;/a&gt; by Paweł Grzybek.&lt;/p&gt;

&lt;p&gt;Let's look at some practical examples of universal and type selectors, below are the HTML and CSS codes for a simple example I created.&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsdfwb8eu7vtk8lxnl77c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsdfwb8eu7vtk8lxnl77c.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpn2n2hkob7fvpfclsof3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpn2n2hkob7fvpfclsof3.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and the &lt;strong&gt;output&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feq2oxp54vksys06p1mk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feq2oxp54vksys06p1mk2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
From the output, we see that the text-alignment and the black border are a result of the style from the Universal (*) selector on line 1 since we see the effects on all the elements. The h3, div and span stylings are all type selector as we see the stylings are only particular to the element name from the HTML document; the text in the h3 tag were all made uppercase, the background colour for the div was made blue and the text colour for both p and span tag was made green and red respectively. If you look at the CSS code critically, you would come to see that the combination of the universal selector and the p tag selector could be replaced solely with the p tag as a selector.&lt;/p&gt;

&lt;p&gt;Well, that’s all for this article, I really hope I helped you understand more about the Universal and Type selectors, we would be looking at class and ID selectors in the next article.&lt;br&gt;
Thank you for reading, if you found this article helpful, please do well to share, it would probably be beneficial to someone else too.&lt;/p&gt;

&lt;p&gt;See you in the next article. Byebye. 🤗&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>css</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
