CSS - Basic Selectors
Tags: css
Published: 2020-08-13 11:03:25


<h2>CSS Selectors</h2>

<p>Before we start talking about the different types of selectors, we need to understand what they are, and why do we even need them.<br>CSS is a language that dictates how elements will be rendered in the browser, for us to connect between the rendering declarations (all the CSS properties) and each element in the DOM tree, we use CSS selectors.<br>Selectors are patterns that the browser matches against elements in the DOM tree and looks for a node element or a set of node elements matching that pattern, selects them (hence the name selectors), and defines their rendering, or in simple words, defines their appearance on the browser screen.</p>

<h3>Selector Structure</h3>

<ul>
<li>A <strong>Selector</strong> is a pattern of elements in the DOM tree. It refers to the simple selector, compound selector, complex selector, or a selector list. When we create a selector we create a condition, then the browser is looking for an element that meets that condition and selects it.</li><li>The <strong>Subject of a Selector </strong>is the element that meets all the conditions of the selector, from the DOM tree.  An element matches a selector only if the Selector accurately describes that element. Two elements match a combinator when the condition of the relationship between them is met.</li><li>A <strong>Simple Selector </strong>is a single pattern on an element. It refers to type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-selectors.</li><li>A <strong>Compound Selector </strong>is a set of simple selectors that are not separated by a combinator, and create a "list" of conditions to an element.</li><li>A <strong>Combinator </strong>is a condition, like a selector, but it checks the relationship between two elements and not the element itself. It refers to the descendant combinator ( ), the child combinator ( > ), the next-sibling combinator ( + ) and the subsequent-sibling combinator ( ~ ).</li>
<li>A <strong>Complex Selector </strong>is a set of compound selectors separated by combinators. This selector creates a set of conditions on a set of elements.</li>
<li>A <strong>Selector List </strong>is a list of selectors that separated by a comma, they have no connection between each other, other than sharing the same properties.</li>
</ul>

<h2>Selector Types</h2>

<p>In this article I will go over the simple selectors CSS has to offer, pseudo-class also considered simple selectors, but because of their importance, I will write about them in their own article and exclude them from this one.<br>We can divide the selectors to Elemental Selectors and Attribute Selectors.</p>

<h3>Elemental Selectors</h3>

<p>Elemental selectors are the type selector and the universal selector, they are looking at the element type of an element.</p>

<h4>Type Selector</h4>

<p>Type selectors, often called tag name or element selectors, are the most basic selector type. The selector looks for at least one instance of that element in the DOM tree.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets all &lt;h3> HTML elements */
 h3 {
     property: value;
 }

/* Targets all &lt;span> HTML elements */
 span {
     property: value;
 }</code>
</pre>

<h4>Universal Selector</h4>

<p>The universal selector, written with an asterisk ( * ) matches an element of any type. <br>The only elements that it can't select are the featureless elements, i.e. the ::before and ::after pseudo-elements are not affected by the universal selector.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 * {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targers all elements that are the descendants
of a &lt;div> element */
 div * {
     property: value;
 }

/* Targers all &lt;span> elements that are the descendants
of any element */
 * span {
     property: value;
 }</code>
</pre>

<h3>Attribute Selectors</h3>

<p>If an element has an attribute, we can use it to select the element. Using the attribute itself with the<strong> attribute presence and value selectors</strong>, or by looking at the value with <strong>substring matching attribute selectors</strong>.</p>

<h4>Attribute Presence and Value Selectors</h4>

<p>These selectors are matching against the attributes and the values in them.</p>

<ul>
<li>Matching the element with the specified attribute, no matter the value.</li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets all &lt;input> elements that have a 
"placeholder" attribute */
 input&#91;placeholder] {
     property: value;
 }</code>
</pre>

<ul>
<li>Matching the element with the specified attribute with exactly the specified value.</li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets all &lt;a> elements with the attribute "target" 
with exactly the value "_top" */ 
 a&#91;target="_top"] {
     property: value;
 }
</code>
</pre>

<ul>
<li>Matching the element with the specified attribute, that it values is a list of strings separated by whitespace, and the specified value is one of them.</li>
</ul>

<pre id="block-fe226752-f799-43b7-adaa-d1eeef22dc31" class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute~="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;input> elements with the attribute 
"class" that has the value "must" in it */
 input&#91;class~="must"] {
     property: value;
 }</code>
</pre>

<ul>
<li>Matches the element with the specified attribute, that it values is exactly the specified value or beginning with the specified value and immediately followed by ( - ).</li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute|="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;a> elements with the attribute 
"hreflang" that the value is "en" or starts with "en-" */
 a&#91;hreflang|="en"] {
     property: value;
 }</code>
</pre>

<h4><strong>Substring Matching Attribute Selectors</strong></h4>

<p>These selectors are matching a substring of the values in the attributes.</p>

<ul>
<li>Matching an element with the specified attribute, that it's value begins with the specified value. </li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute^="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets all &lt;a> elements with the "class" attribute 
with a value starting with "foo" */
 a&#91;class^="foo"] {
     property: value;
 }</code>
</pre>

<ul>
<li>Matching an element with the specified attribute, that it's value ends with the specified value.</li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute$="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;a> elements with the "href" attribute
with the value ending with ".com" */
 a&#91;href$=".com"] {
     font-family: sans-serif;
 }
</code>
</pre>

<ul>
<li>Matching an element with the specified attribute, that has at least one instance of the specified value, in the attribute's value.</li>
</ul>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element&#91;attribute$="value"] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;a> elements with the "href" attribute
 with at least one instance of "%20" string */
 a&#91;href*="%20"] {
     property: value;
 }</code>
</pre>

<h3>Class Selector</h3>

<p>The class selector is actually a special attribute selector. Because of its common use, it has a unique syntax.<br>We create a class attribute selector with the dot notation ( . ), immediately followed by the value of the class attribute in the element.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 .class_name {
     property: value;
 }

/* This syntax is equivalent to the first one */ 
 Element&#91;class~=class_name] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets all the elements with the class "red-text"
 .red-text{
     property: value;
 } 

/* Targets only the elements that have both "left" 
and "small" classes */
 .left.small {
     property: value;
 }</code>
</pre>

<h3>ID Selector</h3>

<p>The ID selector, like the class selector, is actually a special attribute selector and it has a unique syntax, we create an ID selector using the number sign ( # ), immediately followed by the id name.<br>The id selector has a special feature that it can be only once in a single document, no matter the element type, for example, we can't write the same id name to a span and an h3 element on the same document.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 #id_name {
     property: value;
 }

/* This syntax is equivalent to the first one */ 
 Element&#91;id~=id_name] {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets the element that has "red-error-text" 
value in it's ID attribute */
 #red-error-text {
     property: value;
 }</code>
</pre>

<h2>Combinators</h2>

<p>As part of the Selector structure, we use combinators to build more complex Selectors and have more control over how we select elements from the DOM, by using combinators we can create selectors that match nested elements that may have no specified attribute, like an id or class attributes.</p>

<h3>Descendant Combinator (   )</h3>

<p>If we want to select a descendant of an element, we can use the Descendant Combinator, this combinator will help us match elements having to create them a specified id or class attribute.<br>The Descendant Combinator is whitespace " " that separates two compound selectors.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element_selector1 Element_selector2 {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;li> elements that are the descendants of 
the .nav-bar element */ 
 .nav-bar li {
     property: value;
 } 

/* Targets &lt;span> elements with the class "important"
which are the descendants of &lt;p> elements which are 
the descendsnts of &lt;div> elements which are the 
descendants of &lt;section> element */
 section div p span.important {
     property: value;
 }</code>
</pre>

<h3>Child Combinator ( > )</h3>

<p>If we want to select the direct children without the descendants of an element we can use the Child Combinator. The Child Combinator is the "greater-than sign" ( > ) character that separates two compound selectors.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element_selector1 > Element_selector2 {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets &lt;div> elements that are the direct children
of the &lt;section> element */
 section > div {
     property: value;
 }</code>
</pre>

<h3>Next-Sibling Combinator ( + )</h3>

<p>If we want to select an element that is immediately following some other element, we can use the Next-sibling combinator.<br>The elements must share the same parent, and the first element in the selector has to be immediately before the second one.<br>The next-sibling combinator is the "plus sign" ( + ) character that separates two compound selectors.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element_selector1 + Element_selector2 {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets the &lt;p> element that comes right after
&lt;figure> element inside the &lt;div> element */
 div figure + p {
     property: value;
 }</code>
</pre>

<h3>Subsequent-Sibling Combinator ( ~ )</h3>

<p>The subsequent-sibling combinator is the "Tilda" ( ~ ) character that separates two compound selectors.<br> The selector is represented by two sequences of simple selectors separated by the subsequent-sibling combinator, and they both share the same parent element, and unlike the next-sibling combinator, the second element doesn't have to come immediately after the first element.</p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 Element_selector1 + Element_selector2 {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets the &lt;p> element that comes not necasseraly
immideatelly after &lt;figure> element inside the 
&lt;div> element */
 div figure ~ p {
     property: value;
 }</code>
</pre>

<h3>Column Combinator ( || )</h3>

<p>The column combinator is written by two pipes ( || ) selects the elements of a column element by looking at the relationship between it and the column it belongs to.<br><strong>THIS COMBINATOR IS STILL ONLY AT THE EXPERIMENTAL STAGE AND MAY NOT WORK.</strong></p>

<pre class="wp-block-code">
<code>/* ---------- Selector Syntax ---------- */
 column_selector || cell_selector {
     property: value;
 }

/* ---------- Selector Examples ---------- */
/* Targets the &lt;td> elements that belong to
&lt;col> element with the class "blue-cell"  */
 col.blue-cell || td {
     property: value;
 }</code>
</pre>

<p>This concludes my Basic Selectors article, all the pseudo-selectors will be explained in the next article.<br>Hope you enjoyed and leave a comment so I can improve and learn more!<br>Thanks for reading!</p>