Level up your CSS selector skills
Tags: frontenddev, css, webdev, webdesign
Published: 2018-06-06 21:38:46

---
title: Level up your CSS selector skills
published: true
tags: front-end-developmen,css,web-development,web-design
canonical_url: https://blog.logrocket.com/level-up-your-css-selector-skills-5d7bb45ddd37
---

![](https://cdn-images-1.medium.com/max/1024/1*c-SHpWHyrJ2ozRbqI2U5sQ.jpeg)

I’ve been using CSS for many years now but one thing I’ve not revisited in depth until recently is the topic of CSS selectors.

Why would I need to do this? We all know selectors inside out by now, right?  
The trouble is that (at least in my case) over time it’s easy to get used to using the same trusted set of selectors on every project to achieve what you need to do.

So I took it upon myself to do an in-depth review of CSS selectors and came across some interesting ones that were either new to me or were used in a way that hadn’t occurred to me before.

I also discovered some cool new selectors that will be available in the future but aren’t yet widely available.

I invite you to join me as I take a look at various types of CSS selectors. How many of these are you already using in your day-to-day work? I’d be interested to know.

Ready to level up your CSS selector skills? OK then, let’s go.

### Combinator selectors

Let’s start in familiar territory. Combinator selectors are used to select child elements as well as siblings and have been around for a quite a while now.

- General child selector (space). e.g. A B
- Direct child selector. e.g. A > B
- Adjacent sibling selector. e.g. A + B
- General sibling selector. A ~ B

The adjacent selector A + B should be familiar to you and selects the element B which immediately follows A. But what about the general sibling selector A ~ B? This selects all sibling elements B that follow A.

Here’s an example of them both in action:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FRyvYXd%3Fheight%3D600%26slug-hash%3DRyvYXd%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FRyvYXd&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.RyvYXd.small.9dd2bbce-afa8-446f-894b-00326656bfbf.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/62c0884db0465a2fafb41d8f3a16a665/href">https://medium.com/media/62c0884db0465a2fafb41d8f3a16a665/href</a></iframe>

The New York row is selected because it immediately follows the first row, and the last two cities are highlighted as the general sibling selector matches all cities after the 4th one.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/?cid=banner_a)

### Attribute selectors

I really like attribute selectors. They’re just so flexible when you need to match elements containing attributes with specific values.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FMGLZEK%3Fheight%3D600%26slug-hash%3DMGLZEK%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FMGLZEK&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.MGLZEK.small.89db2374-f9c1-4915-8975-f69a2097a179.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/3df63743e5520581894d5df62e87d715/href">https://medium.com/media/3df63743e5520581894d5df62e87d715/href</a></iframe>

This example demonstrates selecting all checkbox input elements and applying styles to their associated labels to make them bold and colored blue.

We then override the style for a checkbox with the specific name chk2 and color its associated label red. Notice how the other form element labels are unaffected and don’t have label styles applied.

Attribute selectors aren’t just for form elements though, they can target attributes on any element. And you can match any attribute not just officially supported ones. Furthermore, you can just check for the existence of an attribute as follows:

```
button[icon]
```

This matches <button> elements that contain an icon attribute. It will match the attribute whether it’s blank or if set to a specific value.

Some more examples:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FpVGGye%3Fheight%3D600%26slug-hash%3DpVGGye%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FpVGGye&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.pVGGye.small.b293ea89-1333-45ce-a7ca-2d421ec5b360.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/c15a68ed07c93e48ceca45d618772c10/href">https://medium.com/media/c15a68ed07c93e48ceca45d618772c10/href</a></iframe>

The first link doesn’t have a target attribute so isn’t matched. The next two links are matched because they either have a blank target attribute or one with a specific value. Finally, the last link is set to pink as it matches the fluffy attribute. Its value is irrelevant and just has to exist to match the a[fluffy] selector.

A practical example of this could be to highlight images that don’t have an alt attribute. This attribute is required for accessibility so its important for SEO purposes that you make sure all image elements contain this attribute.

We can use the following rule to achieve this:

```
img:not([alt]) {
 border: 2px red dashed;
}
```
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FOZdqLp%3Fheight%3D600%26slug-hash%3DOZdqLp%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FOZdqLp&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.OZdqLp.small.df99ef84-2722-4671-8b60-e55bc785b405.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/87765a30b26a1316cb829e604af99432/href">https://medium.com/media/87765a30b26a1316cb829e604af99432/href</a></iframe>

If you want to match a specific part of an attribute value then there are some very useful selectors available.

A[attr^=val] — Attribute begins with value.  
A[attr|=val] — Attribute begins with value OR is first in a dash separated list.  
A[attr$=val] — Attribute ends with value.  
A[attr\*=val] — Value occurs anywhere in attribute.  
A[attr~=val] — Value matches attribute in space separated list.

Here’s an example of each one:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FMGLdvE%3Fheight%3D600%26slug-hash%3DMGLdvE%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FMGLdvE&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.MGLdvE.small.76f6ac44-517d-4a89-8e50-250c13273363.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b38466141b43ade5415217ee13f309cb/href">https://medium.com/media/b38466141b43ade5415217ee13f309cb/href</a></iframe>

The first two examples are very similar except A[attr|=val] also matches the value followed by a dash-separated string. This can be useful for matching language attributes. e.g. <p lang="en-us">.

Matching file extensions is made easy with A[attr$="val"] and coupled with ::after you can easily display the matched file too. Note the use of attr() and concatenation to join it with a static string.

The A[attr\*=val] shows how you can match a specific domain no matter what protocol or subdomain is used.

Finally, we have A[attr~=val] which is great for matching a value in an attribute made up of a list of values separated by spaces. This only matches the whole word not word fragments as the \*= operator does so word plurals won’t match.

All of the above examples of attribute selectors are case sensitive. But we have a trick up our sleeve. If we insert an i before the closing square brackets we can switch on case-sensitive matching.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FLmaYzp%3Fheight%3D600%26slug-hash%3DLmaYzp%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FLmaYzp&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.LmaYzp.small.75f1370e-35d7-43a4-a4e9-33e1f495d077.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f29fd8e85539763d32c07f0aa3c3a71f/href">https://medium.com/media/f29fd8e85539763d32c07f0aa3c3a71f/href</a></iframe>

Most of the major browsers support case insensitive matching except Internet Explorer and Microsoft Edge.

### User interface selectors

If you’ve worked on styling forms then you’ve undoubtedly encountered these type of pseudo-classes before:

- :enabled
- :disabled
- :checked

For example we can use :checked to style a simple to-do list.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FBxbjoO%3Fheight%3D600%26slug-hash%3DBxbjoO%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FBxbjoO&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.BxbjoO.small.4ffb8e73-cdc0-4f72-be0a-73abf5c0c476.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f99000113072f13e53f88b347886757e/href">https://medium.com/media/f99000113072f13e53f88b347886757e/href</a></iframe>

This is pretty standard but there are some other interesting pseudo-classes that we have at our disposal.

:default matches one or more elements that is the default in a group of related elements. This can be combined with the reset button type too.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FELMoNK%3Fheight%3D600%26slug-hash%3DELMoNK%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FELMoNK&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.ELMoNK.small.c72f0542-72e8-4d55-9125-09dabe362b1c.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/bae750c17c2114ff9af8a408c896e66c/href">https://medium.com/media/bae750c17c2114ff9af8a408c896e66c/href</a></iframe>

We can use pseudo classes to match whether input values are valid or not directly with CSS, as well as checking if any elements are required before the form can be submitted.

- :valid
- :invalid
- :required
- :optional (i.e. not required)
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FqYvpMP%3Fheight%3D600%26slug-hash%3DqYvpMP%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FqYvpMP&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.qYvpMP.small.824b989c-3bae-4185-b2a5-c0f296827a56.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/92285467f37051d8c7a82d8482ea35b5/href">https://medium.com/media/92285467f37051d8c7a82d8482ea35b5/href</a></iframe>

If you start typing into the personal email input field then it has to be valid. However, the work email address is always required and needs to be valid so can’t be left empty. Notice too how we can chain pseudo classes (e.g. :required:invalid) to achieve what we need.

Next we have two pseudo-classes that can match if a form element (that supports min and max attributes) is in range or not.

- :in-range
- :out-of-range
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FJvzpgz%3Fheight%3D600%26slug-hash%3DJvzpgz%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FJvzpgz&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.Jvzpgz.small.e54c8553-68fe-4d68-8e89-1c9d10b717c2.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f64b6bed4ea9c51acf9e55a75e86b25d/href">https://medium.com/media/f64b6bed4ea9c51acf9e55a75e86b25d/href</a></iframe>

Again, we can use the reset button type to reset the default value of the number input element.

To round off this section let’s take a look at the :read-only, :read-write, and :placeholder-shown pseudo classes.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FZoPREB%3Fheight%3D600%26slug-hash%3DZoPREB%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FZoPREB&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.ZoPREB.small.971d507f-6f84-4744-823c-7dec91f9051f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f99dcfdf3cf122ee23a744ab3bf80b8/href">https://medium.com/media/3f99dcfdf3cf122ee23a744ab3bf80b8/href</a></iframe>

Using these allows you to easily match elements that are read-only, or writable (editable). Matched elements don’t have to be form input fields either as demonstrated in the example Pen.

Finally, :placeholder-shown will match elements that haven’t been interacted with yet and still display the default placeholder text. This particular selector should be used with caution as it’s not widely supported yet.

### Structural selectors

Structural selectors are very powerful and match elements based on their position in the DOM. They give you the flexibility to match elements purely with CSS that would otherwise require JavaScript to do the same thing.

This type of selector is different from the ones show so far as some of them allow you to pass in a parameter to modify how the selector works.

For example :nth-child() receives a value that will match a specific child element relative to its parent container.

So, if we had a list of items the following selector would match the third item:

```
ul:nth-child(3)
```

However, the parameter doesn’t have to be simple number it can be a simple expression instead that makes the pseudo class even more powerful.

Valid expressions are:

- ul:nth-child(2)— Matches the second child element
- ul:nth-child(4n) — Matches every 2nd child element (4, 8, 12, …)
- ul:nth-child(2n + 1) — Matches every 2nd child element offset by one (1, 3, 5, …)
- ul:nth-child(3n — 1) — Matches every 2nd child element offset by negative one (2, 5, 8, …)
- ul:nth-child(odd) — Matches odd numbered elements (1, 3, 5, …)
- ul:nth-child(even) — Matches even numbered elements (2, 4, 6, …)

The expression variable n always starts a zero so to work out exactly what elements will match start with n as zero, then n as 1, and so on to compile a list of elements.

You can use simple expressions with the following structural selectors:

- :nth-child()
- :nth-last-child()
- :nth-of-type()
- :nth-last-of-type()
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FMGxzEq%3Fheight%3D600%26slug-hash%3DMGxzEq%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FMGxzEq&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.MGxzEq.small.8927fb95-ea4b-4e77-b9d7-408084894b21.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9d014ec25ceacf054ad1ca18e293d90/href">https://medium.com/media/d9d014ec25ceacf054ad1ca18e293d90/href</a></iframe>

:nth-last-child() and :nth-last-of-type() are very similar to :nth-child() and :nth-of-type() except that they match from the last element rather than the first.

You can get quite creative with selectors by playing around with various combinations. For example, the previous Pen example contained the selector:

```
ul:last-of-type li:nth-last-of-type(2)::after {
  content: “ (2nd from end)”;
  /\* Other styles… \*/
}
```

This matches the pseudo-element that comes after the second from last list item inside the second unordered list. If you’re ever struggling to decode a complicated selector then it’s best to read it from right to left so it can be deconstructed logically.

The next set of selectors are specialized structural selectors as they match specific child elements only. You can’t pass expressions to them to modify their behavior.

- :first-child
- :last-child
- :only-child
- :first-of-type
- :last-of-type
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FJvzwJE%3Fheight%3D600%26slug-hash%3DJvzwJE%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FJvzwJE&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.JvzwJE.small.cf57f2d9-dc9b-438b-a62f-0d53f724c2f3.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/8571c883a687450e9b65f5898bb19549/href">https://medium.com/media/8571c883a687450e9b65f5898bb19549/href</a></iframe>

At first glance, there’s a lot going on here and you need to be careful when using these type of selectors as you might get results you weren’t expecting.

For example you might be wondering why the **And so on…** text is blue inside the <section> tag. Actually all the section content is blue as it’s the last child of the main div container. Other section elements have their own colors overridden via other selectors leaving the single paragraph colored blue.

### Content selectors

These belong to a specialized set of selectors for matching content. The ones available for us to use right away are:

- ::first-line
- ::first-letter
- ::selection
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FxjBBqN%3Fheight%3D600%26slug-hash%3DxjBBqN%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FxjBBqN&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.xjBBqN.small.8b2ecbce-c2dc-4cb5-862b-7df561796991.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/52acedd023b7199768dbe3c77d44556b/href">https://medium.com/media/52acedd023b7199768dbe3c77d44556b/href</a></iframe>

::first-line and ::first-letter only work if applied to block level elements. Also be careful to use ::first-letter only on specific elements otherwise every single paragraph would have a drop cap which is probably not what you want!

There some exciting content selectors in the works which aren’t available right now, but when they are supported they’ll open up all sorts of possibilities.

Here’s a list of content selectors to watch out for:

- ::inactive-selection — Selected content inside an inactive window
- ::spelling-error — Check spelling and grammar for editable elements
- ::grammar-error — Matches grammatical errors
- ::marker — Matches list item markers
- ::placeholder — Matches placeholder text of form elements

### Miscellaneous selectors

We’ve just got time to mention a couple of other selectors that didn’t fit into any of the previous categories. Don’t worry we’re almost done! Unfortunately, most of these are experimental so you’ll have to wait a while to use them in production.

The :target selector targets an element with an id that matches part of the current URL. So if we had an element with an id of part1 and the URL:

```
[https://mysite.com#part1](https://mysite.com#part1)
```

We could match that element with:

```
:target { border: 1px red solid; }
```

If you have a large selector then :matches() can help simplify it. For example if you had the following selector:

```
nav p.content,
header p.content,
main p.content,
sidebar p.content,
footer p.content {
  margin: 15px;
  padding: 10px;
}
```

Then can be simplified with :matches() and is equivalent to:

```
:matches(nav, header, main, sidebar, footer) p:content {
margin: 15px;
padding: 10px;
}
```

Nice! This will help to make style sheets much more readable.

Next we have :any-link which is a convenience selector and does the same as :link and :visited combined.

So these two selectors would effectively be the same:

```
:any-link {
  color: red;
}
:link, :visited {
  color: red;
}
```

And that brings us to the last selectors we’ll be looking at in this article:

- :dir()
- :lang()

Both these relate to the language of your site.

:dir() takes in a parameter ltr or rtl depending on the direction of text you want to match and is only currently supported Firefox currently.

So :dir(rtl) would match all elements with content with RTL direction.

Every element in a HTML document can set it’s own individual language by using the lang attribute.

```
<div lang=”en”>The language of this element is set to English.</div>
<div lang=”el”>Η γλώσσα αυτού του στοιχείου έχει οριστεί στα ελληνικά.</div>
<div lang=”is”>Tungumál þessa þáttar er sett á íslensku.</div>
```

The same basic text is entered into three <div> tags but with the specific country added to the end of the content. Also, the country codes used in the lang attribute represent the corresponding country.

- en — English language
- el — Greek language
- is — Icelandic language

The <div> elements can be matched using the :lang() selector:

```
:lang(en) { colore: red; }
:lang(el) { colore: green; }
:lang(is) { colore: blue; }
```

Here’s a Pen to demonstrate:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fembed%2Fpreview%2FxjBoNG%3Fheight%3D600%26slug-hash%3DxjBoNG%26default-tabs%3Dhtml%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdgwyer%2Fpen%2FxjBoNG&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F210894.xjBoNG.small.7506b84a-8906-4456-9cd8-08a381bf702a.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/370d54c1a8c0aa521aa159dbf136c375/href">https://medium.com/media/370d54c1a8c0aa521aa159dbf136c375/href</a></iframe>

And the good news is that the :lang() selector is already supported by all major browsers.

### Resources

If you get stuck trying to figure out a selector, or need to dig deeper into the CSS specifications here’s some useful resources that you might want to check out:

- [MDN Web Docs](https://developer.mozilla.org/en-US/)
- [CSS Specifications](https://www.w3.org/Style/CSS/specs.en.html)
- [Can I Use](https://caniuse.com/)

### And finally…

I hope you’ve found this article useful. I certainly had a lot of fun brushing up on my CSS selector skills and playing around with the various possibilities.

There’s a lot of cool things you can do now with pure CSS that just wasn’t possible a few years ago.

This is exciting for designers who can do some pretty advanced styling and animation in pure CSS, and all without a single line of JavaScript in sight.

Happy styling!

### Plug: [LogRocket](http://logrocket.com), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](http://logrocket.com)

[LogRocket](https://logrocket.com) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

Try it for free.

* * *