<?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: Ali-MEmam</title>
    <description>The latest articles on DEV Community by Ali-MEmam (@alimemam).</description>
    <link>https://dev.to/alimemam</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%2F577305%2F40ccd9bd-ea33-4c51-b1fb-19dedb85c9bf.png</url>
      <title>DEV Community: Ali-MEmam</title>
      <link>https://dev.to/alimemam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alimemam"/>
    <language>en</language>
    <item>
      <title>CSS Selectors</title>
      <dc:creator>Ali-MEmam</dc:creator>
      <pubDate>Sun, 21 Feb 2021 00:39:54 +0000</pubDate>
      <link>https://dev.to/alimemam/css-selectors-4agl</link>
      <guid>https://dev.to/alimemam/css-selectors-4agl</guid>
      <description>&lt;p&gt;A lot of us use &lt;strong&gt;&lt;em&gt;Classes / Id / Elements&lt;/em&gt;&lt;/strong&gt; without knows there are other Selectors so, let's take an overview of CSS selector and how it helps us.&lt;/p&gt;

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

&lt;p&gt;CSS selectors let you apply CSS styles to a specific HTML element. They let you differentiate between HTML elements of the same type, like when we had two div elements but only wanted to style one of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Types of CSS Selector?
&lt;/h3&gt;

&lt;p&gt;There is a lot of selectors provided from CSS to give you an enormous level of flexibility  So let's dive into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic selectors
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Universal selector&lt;/strong&gt;&lt;br&gt;
universal selector gives you control of all elements like if you want to apply red color to all your elements, Many developers will use this trick to zero out the margins and padding.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--prVq-DnG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbv8vnzy4y8hjuubk9lg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--prVq-DnG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbv8vnzy4y8hjuubk9lg.png" alt="Universal Selector"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ID selector&lt;/strong&gt;&lt;br&gt;
id selector is Prefixing the hash symbol and it gives us the ability to select a specific element with id.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6llv1lrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktvzmscpc1ih0zc1erls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6llv1lrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktvzmscpc1ih0zc1erls.png" alt="ID selector"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;There should be only one element with a given ID in a document&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Class selector&lt;/strong&gt;&lt;br&gt;
The class selector is prefixing with a dot and it gives us the ability to select all elements with this class name and this is the most common selector because it gives us the flexibility to create reusable classes to use over the project&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JE2EgHXu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w7i6079v80vuwy53ivb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JE2EgHXu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w7i6079v80vuwy53ivb.png" alt="Class selector"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Element selector&lt;/strong&gt;&lt;br&gt;
What if you want to target all elements on a page, according to their type it's simple you can just type the element type.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nxwhXm2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jraiohvv8bsy9b0pey8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nxwhXm2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jraiohvv8bsy9b0pey8b.png" alt="Element selector"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attribute selector&lt;/strong&gt;&lt;br&gt;
in my opinion, this selector really helpful because it gives you the flexibility to select specific elements from their attribute&lt;br&gt;
like if we have 2 inputs one of them is text and the second is password input we can select the text only by this selector.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nQXsDCmp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5j39rkfwv51ut22abrl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nQXsDCmp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5j39rkfwv51ut22abrl.png" alt="Attribute selector"&gt;&lt;/a&gt;&lt;br&gt;
We can select a specific element depended on the start letter or end letter or contain a string and here list of these selectors.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--umvjsR0C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6ym6z5y8ztquejw0vc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--umvjsR0C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6ym6z5y8ztquejw0vc6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Combinator selectors
&lt;/h2&gt;

&lt;p&gt;A combinator is something that explains the relationship between the selectors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Descendant combinator&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;this selector is one of the most common selectors too and it's helpful when we want to select all (children) in our case is p inside a specific container in our case is div (parent). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_NjIoM1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw4w8rr2l2ygjj7ol6y7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_NjIoM1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw4w8rr2l2ygjj7ol6y7.png" alt="Descendant combinator"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a b{
 // code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If your selector looks like &lt;strong&gt;a b c d&lt;/strong&gt;, you're doing it wrong. Always ask yourself if it's absolutely necessary to apply all of that weight&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Adjacent sibling combinator&lt;/strong&gt; &lt;br&gt;
this selector will select only the first element that is immediately preceded by the former element in our case here the p is the selected element&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YbHr6VGf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yoapd79tpg5amyvpkeu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YbHr6VGf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yoapd79tpg5amyvpkeu.png" alt="Adjacent sibling combinator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;General sibling combinator&lt;/strong&gt; &lt;br&gt;
This General sibling combinator is similar to Adjacent sibling combinator, but it's less strict. because it'll select all p after h1 even it's not the first element.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AQJUMC6A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nquqyrvpvvpbpfadn7v4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AQJUMC6A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nquqyrvpvvpbpfadn7v4.png" alt="General sibling combinator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Child combinator&lt;/strong&gt; &lt;br&gt;
this Selector only looks for the first level inside the container and applies the style on it so if we have a span inside span inside the div the style will apply on the first span only because it's the first level.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dHWY1hXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9npqdazygo5zfn01ye88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dHWY1hXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9npqdazygo5zfn01ye88.png" alt="Child combinator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pseudo Selectors
&lt;/h2&gt;

&lt;p&gt;pseudo allows the selection of elements based on state information like if it's hovered or visited or checked etc... and we use : symbol to refer to it and here a list of the Pseudo Selectors.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AX_3oo7a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o62yezeq9vre5h44l4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AX_3oo7a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o62yezeq9vre5h44l4z.png" alt="Pseudo Selectors"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;I hope that I explained the summary of CSS selectors and  If You have any queries or any mistakes that you think I have made would be more than happy If you could give suggestions to further improve it And if you have any question pm me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/cssref/css_selectors.asp"&gt;https://www.w3schools.com/cssref/css_selectors.asp&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors"&gt;https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/css/css_selectors.asp"&gt;https://www.w3schools.com/css/css_selectors.asp&lt;/a&gt;&lt;/p&gt;

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