I recently received an email from a GenerateBlocks user who was confused about the compound selector toggle in the GenerateBlocks system. If you’re not used to writing CSS, this toggle might seem a bit mysterious. So, I thought I’d create a quick guide to explain when and why you might want to use this toggle—and when you don’t.
What Is a Compound Selector?
In GenerateBlocks, the compound selector toggle is a helper tool that adds an ampersand (&
) to your selector. The ampersand acts as a placeholder for the class name you’re working with, allowing you to create more specific selectors without manually typing the class name.
When the toggle is off, the selector you create will include a space, which targets descendant elements. When the toggle is on, the ampersand replaces the space, allowing you to target pseudo-classes (like :hover
) or create more complex selectors.
Setting Up the Example
To demonstrate how compound selectors work, I’ve set up a simple page with three identical sections:
- Each section contains a container with an inner container that constrains the content width.
- Inside each inner container is a paragraph block.
We’ll use this setup to explore how compound selectors work and when to use them.
Using a Basic Class
Let’s start by adding a class to the first inner container.
- Add a Class:
- Select the inner container and give it a class name (e.g.,
kvd-wrapper
). - Start with a blank style.
- Select the inner container and give it a class name (e.g.,
- Apply a Background Color:
- Go to the Background settings and set the background color to blue.
- Reuse the Class:
- Add the same
kvd-wrapper
class to the third section. - Both sections now share the same background color.
- Add the same
If you update the background color in the kvd-wrapper
class (e.g., change it to yellow), the change will apply to all sections using that class.
Descendant Selectors
Now let’s target the paragraphs inside the kvd-wrapper
class.
- Create a Descendant Selector:
- Select the
kvd-wrapper
class and click More > New. - Turn off the compound selector toggle.
- Type
p
(for paragraph) and hit Create.
- Select the
- Resulting Selector:
- The selector will look like this:
.kvd-wrapper p
. - This is a descendant selector, meaning it targets all paragraphs inside the
kvd-wrapper
class.
- The selector will look like this:
- Apply a Style:
- Change the text color to light gray.
- Only the paragraphs inside the
kvd-wrapper
sections will update, while the middle section remains unchanged.
If you add the kvd-wrapper
class to the middle section, its paragraph will also inherit the light gray text color.
Using Compound Selectors
Now let’s use the compound selector toggle to create a hover effect.
- Create a Hover Selector:
- Select the
kvd-wrapper
class and click More > New. - Turn on the compound selector toggle.
- Type
:hover
(no space) and hit Create.
- Select the
- Resulting Selector:
- The selector will look like this:
.kvd-wrapper:hover
. - This targets the
kvd-wrapper
container when it’s hovered.
- The selector will look like this:
- Apply a Style:
- Change the background color to blue.
- Now, when you hover over the
kvd-wrapper
sections, the background color changes to blue.
Combining Compound and Descendant Selectors
Let’s take it a step further by combining a hover effect with a descendant selector.
- Create a Combined Selector:
- Select the
kvd-wrapper
class and click More > New. - Turn on the compound selector toggle and type
:hover
. - Add a space and type
p
(for paragraph).
- Select the
- Resulting Selector:
- The selector will look like this:
.kvd-wrapper:hover p
. - This targets paragraphs inside the
kvd-wrapper
container when the container is hovered.
- The selector will look like this:
- Apply a Style:
- Change the text color to yellow.
- Now, when you hover over the
kvd-wrapper
sections, the background changes to blue, and the text changes to yellow.
Key Differences Between Compound and Descendant Selectors
- Compound Selector (Toggle On):
- Adds an ampersand (
&
) to the selector. - Targets pseudo-classes (e.g.,
:hover
) or creates more specific selectors without spaces.
- Adds an ampersand (
- Descendant Selector (Toggle Off):
- Includes a space between the class name and the target element.
- Targets child elements inside the container.
Practical Use Cases
When to Use Compound Selectors
- Pseudo-Classes:
- Use compound selectors for hover effects, focus states, or other pseudo-classes (e.g.,
.kvd-wrapper:hover
).
- Use compound selectors for hover effects, focus states, or other pseudo-classes (e.g.,
- Sibling or Direct Child Selectors:
- Use compound selectors for more specific targeting (e.g.,
.kvd-wrapper > p
).
- Use compound selectors for more specific targeting (e.g.,
When to Use Descendant Selectors
- Child Elements:
- Use descendant selectors to target elements inside a container (e.g.,
.kvd-wrapper p
).
- Use descendant selectors to target elements inside a container (e.g.,
Understanding the Toggle
The simplest way to think about the toggle is:
- Toggle Off: Adds a space between the class name and the target element.
- Toggle On: Adds an ampersand (
&
) and removes the space, allowing for pseudo-classes or more specific selectors.
Learning More
To fully understand when to use compound selectors versus descendant selectors, it’s helpful to learn more about CSS selectors. I’ll include links to resources that explain these concepts in greater detail.
Wrapping Up
The compound selector toggle in GenerateBlocks is a powerful tool, but it requires a basic understanding of CSS to use effectively. Once you grasp the difference between compound and descendant selectors, you’ll be able to create more advanced styles and interactions in your designs.
This tutorial is just scratching the surface of what’s possible with GenerateBlocks’ selector system. I’ll be covering more advanced use cases in upcoming posts, so stay tuned!