12 Types of Advanced CSS Selectors!! | (Part-1)
Tags: css, webdev, html, todayilearned
Published: 2021-12-18 06:28:22

Hey Gang, Really glad to see your Curiosity about learning of advanced concept. So, I'm care to save your time and get this show on the road🎉.

> Don't worry, Today i<b> didn't explain about common CSS selectors like <code>class</code>, <code>id</code>, <code>Element</code>.</b>

Start to exploring about

- <code>></code> Descendant children 
- <code>+</code> Adjacent sibling
- <code>~</code> General Sibling
- <code>*</code> Universal(<em>UnKnown behaviour</em>)

<h2>7 Different type of Attribute Selector</h2>

- [attr]
- [attr=value]
- [attr~=value]
- [attr|=value]
- [attr^=value]
- [attr$=value]
- [attr*=value]

Seems a little bit weird😅. Uh, it's not much complex like you think about that. I'm here to make it easier for you😉. So, let's get started.

<h2><code>></code> Descendant children Selector</h2>

```html
<div class="parent-container">
  <h1>> DIRECT CHILDREN</h1>
    <p>I am the direct child of parent😁</p>
        <div class="child-container">
            <p>Oh no😣, I can't be the direct child of parent.</p>
        </div>
</div>
```
```css
.parent-container > p {
  color: #ff8847;
}
```
In above HTML template, there is a two <code>p</code>tag right. But, The 1st <code>p</code> tag will be the direct child. unfortunately, 2nd <code>p</code> tag will not be direct child. 

<b>Can you guess why??</b> Hm, May be 2nd <code>p</code> tag under child-container which is not directly under <code>.parent-container</code>...

But, That's the right answer🎉. 2nd <code>p</code> tag is not directly under <code>.parent-container</code> which is under <code>.child-container</code>. If you feel not well, Just play around in codepen get clear with concept.

{% codepen https://codepen.io/preethi-dev/pen/YzrVYNL %}

Guess the difference below👇🏻👇🏻.

```css
.parent-container > p {
  color: #ff8847;
}

.parent-container  p {
  color: #ff8847;
}
```
If you guessed, That's stunning. If can't, Not a big problem and we will sort it out.

```css
.parent-container  p {
  color: #ff8847;
}
```
The above one will select all the <code>p</code> tag inside the .parent-container, which doesn't care about the <code>p</code> tag is direct child or indirect child. Just apply the styles if it's under the <code>.parent-container</code>.

<h2><code>+</code> Adjacent sibling</h2>

> The adjacent sibling combinator (<code>+</code>) separates two selectors and <b>matches the second element only if it immediately follows the first element</b>, and both are children of the same parent element.

```html
<div>
    <p>No one before me😭</p>
    <p>hurray one before me🥰</p>
    <p>hurray one before me🥰</p>
</div>
```

```css
/*General sibling*/
p + p{
  background-color: lightgreen;
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfps3ml9j76pk6nelcth.png) 

Above Definition is the best one to define adjacent sibling. If you feel need some practice. Why wait, go ahead and practice in codepen below through tiny piece example.

{% codepen https://codepen.io/preethi-dev/pen/KKXmQyv %}

<h2><code>~</code> General Sibling</h2>
> The general sibling combinator (~) separates two selectors and matches all iterations of the second element, that are following the first element (though not necessarily immediately), and are children of the same parent element.

Seems little bit confusing. Don't worry, it's not a big deal. I will explain it through example below. Then, again read the above definition and i am sure you can understand what it means.

```html
<div>
    <p class="panda">Panda here🐼</p>
    <p class="cat">No one before like me😾</p>
    <p class="panda">One before like me🐼</p>
    <p class="rat">no one before like me🐀</p>
    <p class="panda">one before like me🐼</p>
</div>
```
```css
.panda ~ .panda{
  background-color: lightgreen;
}
```
In above HTML Template, we use <code>~</code> general sibling in <code>.panda</code> class. Unlike adjusent sibling, The general sibling combinator (<code>~</code>) separates two selectors and <b>matches the second element(<code>.panda</code>), if it follows similar element(<code>.panda</code>) before that (it's not necessary immediately before the element)</b>, and both are children of the same parent element.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d485zkcy6zu45pogw876.png)

If you feel need practice, feel relax and see below.

{% codepen https://codepen.io/preethi-dev/pen/LYzydVE %}

<h2><code>*</code> Universal(<em>UnKnown behaviour</em>)</h2>

The CSS universal selector (<code>*</code>) matches elements of any type.

For instance: 
```css
*.success{ background-color: green }
```
is exactly same as,
```css
.success{background-color: green}
```
In CSS world, Both are same which both guys are doing the same job.
<b>(The asterisk is optional with simple selectors)</b>

Hey still travel with me, Really your accountability is unbelievable😍. So, I wish to give a special info for you😉.

<h3>UnKnown Behaviour</h3>

You have probably already seen<code>*</code> are used for resetting things like,

```css
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
 }
```

> <em><code>*</code> selects EVERYTHING. Every single thing
<b>(except ::before and ::after...)</b>.</em>

Above note is surprising right. Which means you can't apply those resetting things in <code>::before</code> content and <code>::after</code> content. Hey why get frowned up for this behavior. Chill, There is a solution for every problem. Trust me and see below.

```css
*, *::before, *::after{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
 }
```

><em>I already promised you, I will explain 12 different types of advanced CSS selectors. still, I explained the 5 selectors. I can understand your feeling If I continue this blog which makes you feel overwhelmed. So, Hope I do not leave you with an empty hand and we will continue remaining selectors in <b>part-2</b>.</em>

If you loved this blog, Then give a endearing heart💝 which really lot to me. I love the discussion with you, If you feel not good at styling concept or any doubts.

Thanks for Reading!!
<b>Preethi</b>
<em>- Make your CSS life easier</em>