{"id":5123,"date":"2025-02-07T09:50:06","date_gmt":"2025-02-07T14:50:06","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=5123"},"modified":"2025-02-07T09:50:07","modified_gmt":"2025-02-07T14:50:07","slug":"three-approaches-to-the-ampersand-selector-in-css","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/three-approaches-to-the-ampersand-selector-in-css\/","title":{"rendered":"Three Approaches to the &#8220;&amp;&#8221; (ampersand) Selector in CSS"},"content":{"rendered":"\n<p>In CSS nesting, the&nbsp;<code>&amp;<\/code>&nbsp;(ampersand symbol) selector adds style rules based on the relation between nested selectors. For example, a pseudo-class (<code>:hover<\/code>) nested inside a type selector (<code>div<\/code>) becomes a compound selector (<code>div:hover<\/code>) when the nested pseudo-class is prefixed with&nbsp;<code>&amp;<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">div<\/span> {\n  &amp;:hover {\n    <span class=\"hljs-attribute\">background<\/span>: green;\n  }\n}\n\n<span class=\"hljs-comment\">\/*\nThe above code is equivalent to:\ndiv:hover {\n  background: green;\n}\n*\/<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A use-case for the&nbsp;<code>&amp;<\/code>&nbsp;selector is to combine it with the&nbsp;<code>:has()<\/code>&nbsp;pseudo-class to select and style elements based on the child elements they contain. In the following example, a&nbsp;<code>label<\/code>&nbsp;is styled for when the checkbox within it is checked.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span>&gt;<\/span>\n  Allow access to all files\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">label<\/span> {\n  <span class=\"hljs-comment\">\/* etc. *\/<\/span>\n  &amp;:has(:checked) {\n    <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid lime;\n  }\n}\n\n<span class=\"hljs-comment\">\/*\n  The above code is equivalent to:\n\n  label:has(:checked) {\n    border: 1px solid lime;\n  }\n*\/<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_ogvVEGJ\" src=\"\/\/codepen.io\/anon\/embed\/ogvVEGJ?height=450&amp;theme-id=47434&amp;slug-hash=ogvVEGJ&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed ogvVEGJ\" title=\"CodePen Embed ogvVEGJ\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>In a way, the&nbsp;<code>&amp;<\/code>&nbsp;symbol is a placeholder for the outer level selector in the nesting hierarchy.&nbsp;<strong>This enables flexible combinations of selectors customized to suit our code\u2019s modular organization preferences<\/strong>. In this article, we\u2019ll see three kinds of modular possibilities with the&nbsp;<code>&amp;<\/code>&nbsp;selector in native CSS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"linked-class-names\">1) Linked Class Names<\/h2>\n\n\n\n<p>Starting with the easiest approach, the&nbsp;<code>&amp;<\/code>&nbsp;selector can be used to combine class names. Elements often have multiple class names to group and style them together. Sometimes, grouping is within a module, and other times style rules can intersect between modules due to shared class names.<\/p>\n\n\n\n<p><strong>By using the<\/strong>&nbsp;<code>&amp;<\/code>&nbsp;<strong>selector to concatenate class names, style rules for elements within a module can be arranged together based on their shared class names.<\/strong><\/p>\n\n\n\n<p>In the example below, the three card modules, grouped under the class name&nbsp;<code>cards<\/code>, share most style rules, such as dimensions. However, they have different background images to reflect their content, defined separately within the&nbsp;<code>.cards<\/code>&nbsp;ruleset by concatenating the&nbsp;<code>&amp;<\/code>&nbsp;selector with the exclusive class names of each card.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"cards trek\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Trekking<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"cards wildlife\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Wildlife spotting<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"cards stargaze\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Stargazing camp<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.cards<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: center\/cover <span class=\"hljs-built_in\">var<\/span>(--bg);\n\n  &amp;.trek {\n    <span class=\"hljs-attribute\">--bg<\/span>: <span class=\"hljs-built_in\">url<\/span>(<span class=\"hljs-string\">\"trek.jpg\"<\/span>);\n  }\n  &amp;<span class=\"hljs-selector-class\">.wildlife<\/span> {\n    <span class=\"hljs-attribute\">--bg<\/span>: <span class=\"hljs-built_in\">url<\/span>(<span class=\"hljs-string\">\"wildlife.jpg\"<\/span>);\n  }\n  &amp;<span class=\"hljs-selector-class\">.stargaze<\/span> {\n    <span class=\"hljs-attribute\">--bg<\/span>: <span class=\"hljs-built_in\">url<\/span>(<span class=\"hljs-string\">\"stargaze.jpg\"<\/span>);\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_pvzQKyL\" src=\"\/\/codepen.io\/anon\/embed\/pvzQKyL?height=450&amp;theme-id=47434&amp;slug-hash=pvzQKyL&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed pvzQKyL\" title=\"CodePen Embed pvzQKyL\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Class names can also be connected using the attribute selector:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"element-one\"<\/span>&gt;<\/span>text one<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"element-two\"<\/span>&gt;<\/span>text two<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;class|=<span class=\"hljs-string\">\"element\"<\/span>]<\/span> {\n  &amp;&#91;class$=\"one\"] { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;<span class=\"hljs-selector-attr\">&#91;class$=<span class=\"hljs-string\">\"two\"<\/span>]<\/span> { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Another example is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"element_one\"<\/span>&gt;<\/span>text one<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"element_two\"<\/span>&gt;<\/span>text two<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;class^=<span class=\"hljs-string\">\"element\"<\/span>]<\/span> {\n  &amp;&#91;class$=\"one\"] { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;<span class=\"hljs-selector-attr\">&#91;class$=<span class=\"hljs-string\">\"two\"<\/span>]<\/span> { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"parent-and-previous-element-selectors\">2) Parent and Previous Element Selectors<\/h2>\n\n\n\n<p>The conventional method of organizing nested style rulesets involves including the rulesets of child elements within the ruleset of their parent elements.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"error\"<\/span>&gt;<\/span>\n  Server down. Try again after thirty minutes. \n  If you still can't access the site, \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"\/errorReport\"<\/span>&gt;<\/span>submit us a report<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>. \n  We'll resolve the issue within 24hrs.\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.error<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: red;\n  a {\n    <span class=\"hljs-attribute\">color<\/span>: navy;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, the opposite is also possible due to the&nbsp;<code>&amp;<\/code>&nbsp;selector: nesting a parent element\u2019s style rules within its child element\u2019s ruleset. This can be beneficial when it\u2019s easier to&nbsp;<strong>organize an element\u2019s style rules by its purpose rather than its position in a layout<\/strong>.<\/p>\n\n\n\n<p>For instance, styling can be static or dynamic. Dynamic styling happens when user interactions, or scripts, trigger the application of a selector\u2019s ruleset to an element on a page. In such cases, it\u2019s convenient to categorize rulesets into dynamic and static.<\/p>\n\n\n\n<p>In the following example, all rules related to the appearance of the agreement modules upon page load, such as layout, dimensions, and colors, are included in the&nbsp;<code>.agreements<\/code>&nbsp;ruleset. However, the style rules that modify the appearance of the agreement modules when their checkboxes are checked, i.e&nbsp;<strong>when user interaction occurs<\/strong>, are placed within the nesting selector&nbsp;<code>.isAgreed:checked<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">article<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"agreements terms\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"isAgreed\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">!<\/span> <span class=\"hljs-attr\">--<\/span> <span class=\"hljs-attr\">...<\/span> <span class=\"hljs-attr\">--<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">article<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">article<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"agreements privacy\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"isAgreed\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">!<\/span> <span class=\"hljs-attr\">--<\/span> <span class=\"hljs-attr\">...<\/span> <span class=\"hljs-attr\">--<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">article<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">article<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"agreements diagnostics\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"isAgreed\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!-- ... --&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">article<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.agreements<\/span> {\n  <span class=\"hljs-comment\">\/* etc. *\/<\/span>\n  &amp;.terms {  <span class=\"hljs-attribute\">--color<\/span>: <span class=\"hljs-built_in\">rgb<\/span>(<span class=\"hljs-number\">45<\/span> <span class=\"hljs-number\">94<\/span> <span class=\"hljs-number\">227<\/span>);  }\n  &amp;<span class=\"hljs-selector-class\">.privacy<\/span> { <span class=\"hljs-attribute\">--color<\/span>: <span class=\"hljs-built_in\">rgb<\/span>(<span class=\"hljs-number\">231<\/span> <span class=\"hljs-number\">51<\/span> <span class=\"hljs-number\">35<\/span>); }\n  &amp;<span class=\"hljs-selector-class\">.diagnostics<\/span> { <span class=\"hljs-attribute\">--color<\/span>: <span class=\"hljs-built_in\">rgb<\/span>(<span class=\"hljs-number\">59<\/span> <span class=\"hljs-number\">135<\/span> <span class=\"hljs-number\">71<\/span>); }\n  <span class=\"hljs-comment\">\/* etc. *\/<\/span>\n}\n\n<span class=\"hljs-selector-class\">.isAgreed<\/span><span class=\"hljs-selector-pseudo\">:checked<\/span> { \n  <span class=\"hljs-comment\">\/* checkbox's background color change *\/<\/span>\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-built_in\">var<\/span>(--color);\n  <span class=\"hljs-comment\">\/* checkbox's border color change *\/<\/span>\n  <span class=\"hljs-attribute\">border-color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--color);\n  <span class=\"hljs-comment\">\/* checkbox shows a checkmark *\/<\/span>\n  &amp;::before { <span class=\"hljs-attribute\">content<\/span>: <span class=\"hljs-string\">'\\2713'<\/span>; <span class=\"hljs-comment\">\/* checkmark (\u2713) *\/<\/span> }\n  \n  <span class=\"hljs-comment\">\/* Agreement section's (checkbox's parent) border color change *\/<\/span>\n  <span class=\"hljs-selector-class\">.agreements<\/span><span class=\"hljs-selector-pseudo\">:has(<\/span>&amp;) { \n    <span class=\"hljs-comment\">\/* same as .agreements:has(.isAgreed:checked) *\/<\/span>\n    <span class=\"hljs-attribute\">border-color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--color); \n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_xbKQzON\" src=\"\/\/codepen.io\/anon\/embed\/xbKQzON?height=450&amp;theme-id=47434&amp;slug-hash=xbKQzON&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed xbKQzON\" title=\"CodePen Embed xbKQzON\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>In the above demo, a parent element selection is shown as example, but the same logic applies for previous element selections as well.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>some text<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span>\/&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-comment\">\/* When the checkbox is checked *\/<\/span>\n<span class=\"hljs-selector-pseudo\">:checked<\/span> {\n  <span class=\"hljs-comment\">\/* \n    rules to style the checkbox\n  *\/<\/span>\n\n  <span class=\"hljs-comment\">\/* style for &lt;p&gt; when the checkbox is checked *\/<\/span>\n  <span class=\"hljs-attribute\">p<\/span>:<span class=\"hljs-built_in\">has<\/span>(+&amp;) { \n    <span class=\"hljs-comment\">\/* same as p:has(+:checked) *\/<\/span>\n    color: blue;    \n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"recurring-selectors\">3) Recurring Selectors<\/h2>\n\n\n\n<p>With IDs, class names, semantic markup, and so forth, we rarely need to repeat selectors within compound selectors to achieve specificity. However,&nbsp;<strong>repeating selectors are still useful, particularly when the same type of elements are styled similarly but with slight adjustments based on their positions in the layout and among themselves<\/strong>.<\/p>\n\n\n\n<p>A good example of this is how paragraphs are typically spaced in an article. There\u2019s the spacing between each paragraph, and the spacing between the paragraphs and another kind of element, such as an image, that\u2019s inserted between them.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">article<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">figure<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">figure<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">blockquote<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">blockquote<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">figure<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">figure<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span>&gt;<\/span><span class=\"hljs-comment\">&lt;!--...--&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">article<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">article<\/span> {\n  <span class=\"hljs-comment\">\/* etc. *\/<\/span>\n  p {\n    <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\n    \n    <span class=\"hljs-comment\">\/* &lt;p&gt; that's after\/below an element that's not &lt;p&gt; *\/<\/span>\n    *:not(&amp;) + &amp; { \n      <span class=\"hljs-attribute\">margin-top<\/span>: <span class=\"hljs-number\">30px<\/span>; \n    }\n    \n    <span class=\"hljs-comment\">\/* &lt;p&gt; that's before\/above an element that's not &lt;p&gt; *\/<\/span>\n    &amp;<span class=\"hljs-selector-pseudo\">:not(<\/span><span class=\"hljs-selector-pseudo\">:has(+<\/span>&amp;)) { \n      <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">30px<\/span>; \n    } \n    \n    <span class=\"hljs-comment\">\/* &lt;p&gt; that's after\/below another &lt;p&gt; *\/<\/span>\n    &amp; + &amp; {\n      <span class=\"hljs-attribute\">margin-top<\/span>: <span class=\"hljs-number\">12px<\/span>; \n    }\n  }\n  <span class=\"hljs-comment\">\/* etc. *\/<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_xbKQzEN\" src=\"\/\/codepen.io\/anon\/embed\/xbKQzEN?height=750&amp;theme-id=47434&amp;slug-hash=xbKQzEN&amp;default-tab=result\" height=\"750\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed xbKQzEN\" title=\"CodePen Embed xbKQzEN\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>In the above example, the gaps between paragraphs are small compared to the larger gaps between a paragraph and a non-paragraph element.<\/p>\n\n\n\n<p>The selectors can be explained like:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>*:not(p) + p<\/code>&nbsp;\u2014 The paragraph below a non-paragraph element has a larger top margin<\/li>\n\n\n\n<li><code>p:not(:has(+p))<\/code>&nbsp;\u2014 The paragraph above a non-paragraph element has a larger bottom margin<\/li>\n\n\n\n<li><code>p + p<\/code>&nbsp;\u2014 The paragraph below another paragraph has a smaller top margin<\/li>\n<\/ol>\n\n\n\n<p>Besides its flexibility, another compelling reason to use the&nbsp;<code>&amp;<\/code>&nbsp;selector in organizing our code is that it lacks its own specificity. This means we can&nbsp;<strong>rely on the specificity of the usual selectors and the nesting hierarchy<\/strong>&nbsp;to apply the rules as intended.<\/p>\n\n\n\n<p class=\"learn-more\">If you&#8217;re looking to level up your skills in CSS nesting, check out Kevin Powell&#8217;s new course <a href=\"https:\/\/frontendmasters.com\/courses\/pro-css\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">Build a Website from Scratch<\/a> and Jen Kramer&#8217;s <a href=\"https:\/\/frontendmasters.com\/courses\/css-layouts\/?utm_source=boost&amp;utm_medium=blog&amp;utm_campaign=boost\">Practical CSS Layouts<\/a> both of which cover CSS nesting and lots more!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-vanilla-css-and--in-frameworks\">Using <code>&amp;<\/code>\u00a0in Vanilla CSS vs.\u00a0Using <code>&amp;<\/code>\u00a0in Frameworks<\/h2>\n\n\n\n<p>The&nbsp;<code>&amp;<\/code>&nbsp;is vanilla CSS always represents the outer level&nbsp;<em>selector<\/em>, which might not be the case for the&nbsp;<code>&amp;<\/code>&nbsp;used in CSS frameworks, such as Sass. The&nbsp;<code>&amp;<\/code>&nbsp;in frameworks could mean the outer level&nbsp;<em>string<\/em>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"parent--one\"<\/span>&gt;<\/span>text one<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"parent--two\"<\/span>&gt;<\/span>text two<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In Sass (SCSS), the style could be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SCSS\" data-shcb-language-slug=\"scss\"><span><code class=\"hljs language-scss\"><span class=\"hljs-selector-class\">.parent<\/span> {\n  &amp;--one { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;--two { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SCSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">scss<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That will not work in vanilla CSS, but it still can be done! A similar ruleset would be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;class|=<span class=\"hljs-string\">\"parent\"<\/span>]<\/span> {\n  &amp;&#91;class$=\"--one\"] { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;<span class=\"hljs-selector-attr\">&#91;class$=<span class=\"hljs-string\">\"--two\"<\/span>]<\/span> { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that, in the SCSS code, there is no real\u00a0<code>.parent<\/code>\u00a0selector, as in no element on page matches it, whereas in CSS,\u00a0<code>[class|=\"parent\"]<\/code>\u00a0<em>will<\/em> match an element. If we add a style rule to the outer level ruleset in both of those code snippets, the SCSS will fail to find an element to apply the style rule, whereas the CSS will apply the style to both the elements that has the class name starting with\u00a0<code>parent<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"SCSS\" data-shcb-language-slug=\"scss\"><span><code class=\"hljs language-scss\"><span class=\"hljs-selector-class\">.parent<\/span> {\n  <span class=\"hljs-attribute\">font-weight<\/span>: bold; <span class=\"hljs-comment\">\/* this won\u2019t work, as it doesn't match anything *\/<\/span>\n  &amp;--one { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;--two { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SCSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">scss<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;class|=<span class=\"hljs-string\">\"parent\"<\/span>]<\/span> {\n  <span class=\"hljs-attribute\">font-weight<\/span>: bold; <span class=\"hljs-comment\">\/* works *\/<\/span>\n  &amp;&#91;class$=\"one\"] { <span class=\"hljs-attribute\">color<\/span>: green; }\n  &amp;<span class=\"hljs-selector-attr\">&#91;class$=<span class=\"hljs-string\">\"two\"<\/span>]<\/span> { <span class=\"hljs-attribute\">color<\/span>: blue; }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Hence, the downside of an&nbsp;<code>&amp;<\/code>&nbsp;that represents a selector-syntax string rather than a viable selector is that it might mislead us into thinking elements matching the perceived selector exist when they don\u2019t, something that we don\u2019t have to worry with the native&nbsp;<code>&amp;<\/code>&nbsp;selector.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>When using a nested style rule, one must be able to refer to the elements matched by the parent rule; that is, after all, the entire point of nesting. To accomplish that, this specification defines a new selector, the nesting selector, written as &amp; (U+0026 AMPERSAND).<\/p>\n\n\n\n<p>When used in the selector of a nested style rule, the nesting selector r<strong>epresents the elements matched by the parent rule<\/strong>. When used in any other context, it represents the same elements as :scope in that context (unless otherwise defined).<\/p>\n\n\n\n<p>\u2014&nbsp;<a href=\"https:\/\/www.w3.org\/TR\/css-nesting-1\/#nest-selector\">CSS Nesting Module 1<\/a><a href=\"https:\/\/www.w3.org\/TR\/css-nesting-1\/#nest-selector\"><strong>,<\/strong><\/a>&nbsp;<a href=\"https:\/\/www.w3.org\/TR\/css-nesting-1\/#nest-selector\">W3C<\/a><\/p>\n<\/blockquote>\n\n\n\n<p>On the other hand, we can combine strings more freely to produce the selectors we want using the&nbsp;<code>&amp;<\/code>&nbsp;in frameworks. Which is useful when class names are extensively relied on for modularity.<\/p>\n\n\n\n<p>Either way, grouping style rulesets is crucial for enhancing code readability, maintainability, and to provide a desired order of use among conflicting style rules. The&nbsp;<code>&amp;<\/code>&nbsp;selector in native CSS can help with that, as well as in&nbsp;<strong>specifying selection criteria that might otherwise be challenging to define<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"further-reading\">Further Reading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_nesting\">CSS Nesting<\/a>, MDN<\/li>\n\n\n\n<li><a href=\"https:\/\/www.w3.org\/TR\/selectors-4\/#overview\">List of Selectors Level 4<\/a>, W3C<\/li>\n\n\n\n<li><a href=\"https:\/\/css-tricks.com\/specifics-on-css-specificity\/\">Specifics on CSS Specificity<\/a>, CSS-Tricks<\/li>\n\n\n\n<li><a href=\"https:\/\/webkit.org\/blog\/13096\/css-has-pseudo-class\/#the-basics-of-how-to-use-has-as-a-parent-selector\">Using :has() as a CSS Parent Selector and much more<\/a><\/li>\n\n\n\n<li>WebKit Blog&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Attribute_selectors#syntax\">Attribute selectors<\/a>, MDN<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The &#038; is a powerful addition to CSS, allowing us to craft selectors without repetition and helping organization and understanding.<\/p>\n","protected":false},"author":20,"featured_media":5137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[7,294,117],"class_list":["post-5123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-nesting","tag-scss"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/02\/pexels-photo-6250312.jpeg?fit=1733%2C1300&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=5123"}],"version-history":[{"count":13,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5123\/revisions"}],"predecessor-version":[{"id":5148,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5123\/revisions\/5148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/5137"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=5123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=5123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=5123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}