sass error triggered by the application of :nth-child/:nth-of-type selectors

I am currently implementing SASS in my project and I am facing difficulties with compiling the following code:

.section {
    &:first-of-type {
        background: url("../img/misc/.jpg") center center no-repeat;
        background-size: cover;
        &--text {
            &--title {
                /*THIS WON'T COMPILE*/
            }
            &--subtitle {
                /*THIS WON'T COMPILE*/
            }
            &--subscribe {
                &--input {
                    /*THIS WON'T COMPILE*/
                }
                &--button {
                    /*THIS WON'T COMPILE*/
                }
            }
        }
    }
    &:nth-of-type(2) {
        &--text {
            &--title {
                /*THIS WON'T COMPILE*/
            }
            &--paragraph {
                /*THIS WON'T COMPILE*/
            }
       }
   }
   }

I am encountering the following error: Invalid parent selector for "&--text": "Home stylesheet .section:nth-of-type(2)"

Below is the HTML markup:

<section class="section"> <!-- Cover section -->
            <div class="section--text">
                <h1 class="section--text--title">
                    Main title
                </h1>
                <p class="section--text--subtitle">
                    Some text
                </p>
                <div class="section--text--subscribe">
                    <input class="section--text--subscribe--input" type="email" placeholder="Your email">
                    <button class="section--text--subscribe--button">button</button>
                </div>
            </div>
        </section>
        <section class="section"> <!-- Problem section -->
            <img src="img/ipad.jpg" alt="App screenshot">
            <div class="section--text">
                <h2 class="section--text--title">
                     Title
                </h2>
                <p class="section--text--paragraph">
                    Some text
                </p>
            </div>
         </section>

Is it not possible to apply CSS rules to children of pseudo-selectors using nesting? What could be the missing piece here?

Thank you in advance!

Answer №1

It seems there may be a misunderstanding regarding the usage of '&' to nest selectors. In your code, the first nested selector would compile to something like .section:first-of-type --text. To utilize child classes, you must include the full class name with the '.' selector. You can achieve this by:

.section {
    &:first-of-type {
        background: url("../img/misc/.jpg") center center no-repeat;
        background-size: cover;
        &.section--text {
            &.section--text--title {
                /*THIS WON'T COMPILE*/
            }
            &.section--text--subtitle {
                /*THIS WON'T COMPILE*/
            }
            &.section--text--subscribe {
                &.section--text--subscribe--input {
                    /*THIS WON'T COMPILE*/
                }
                &.section--text--subscribe--button {
                    /*THIS WON'T COMPILE*/
                }
            }
        }
    }
    &:nth-of-type(2) {
        &.section--text {
            &.section--text--title {
                /*THIS WON'T COMPILE*/
            }
            &.section--text--paragraph {
                /*THIS WON'T COMPILE*/
            }
       }
   }

}

The use of '&' functions similarly to a space in CSS. For more information, visit:

http://sass-lang.com/documentation/file.SASS_REFERENCE.html#parent-selector

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The behavior of React Native margin is applied internally within the object, rather than externally

Visit the link and then open the file login_screen/components/Form.js. Within this file, you will notice that the textInput is styled as follows: textInput: { flex:1, height: 50, marginBottom: 20 } If you observe closely, you'll see ...

CSS: What causes the gap below my border to appear?

I've noticed an issue in the HTML code snippet below: there seems to be some extra spacing under the border of the 'bucket' div. Despite my attempts, I haven't been able to figure out why this is happening. Can someone provide some ins ...

Is my JQuery width() function giving incorrect value results?

Currently, I am facing a situation similar to the following: We have two popup windows that are quite alike. These popups are generated by a factory in JavaScript, allowing them to share common events and functions. One such function is our custom resize ...

Adjust the size of the div container while ensuring that all elements are aligned horizontally

My goal is to design a page where all elements remain perfectly aligned, even when the user resizes the window. However, the code I have written does not achieve this. <div style="display:flex; justify-content: left; padding: 3px;"> <div style="m ...

Tips for directing specific mouse interactions to the underlying elements within an SVG

I'm working with a scatterplot that includes a brush layer for zooming in on the data and voronois for mouse hover snapping. However, I've encountered an issue where the click event doesn't fall through to the brush layer when the voronoi pa ...

Text alignment validation in jQuery

utilizing the validation capabilities of the jQuery validation plugin check out: http://jsfiddle.net/Kn3v5/205/ <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script> <div> <form id ...

What happens when absolute positioning looks for "relative" inside nested divs?

I'm struggling to grasp the concept of nested divs and how they work together. I understand that when you have a "position absolute" element inside a "position relative" element, the former looks for the latter as its reference point. But what happens ...

How can I fix the issue of CSS styles not being applied to a button and remove the blue highlight when clicked

Recently, I encountered a problem while developing a web application using HTML, CSS, and JS in PyCharm. The issue is with the upload button that should trigger the uploading of source code. Despite being new to this, I have been unable to find a solution. ...

Generate a pair of rows using input-prepend

I am attempting to place two buttons on either side of the search field, so I implemented the following code: <div id="fixtures-filter"> <div class="input-group-btn"> <div class="input-group-prepend"><button type="button" class= ...

The background extends beyond the confines of its container

I'm facing some challenges trying to create this specific layout, particularly with setting a background that should cover 50% of the screen size. My initial thought was to use an image as the background, but I need different colors for each page. ht ...

By simply clicking the link, the content comes to life with animation

Is there a way to replicate the scrolling effect seen on this website ? The content shifts to the left and reveals new site content. Please excuse my English. ...

Can a javascript code for "Infinite Scroll" be created to manage the back button?

Head over to this website: Experiment with the infinite scroll feature. You may notice that when you click a link and then press "back", there are some glitches. Therefore, I am considering developing my own version of an Infinite Scroll functionality. ...

Is there a way to apply CSS to a PHP file within a WordPress website?

Similar Question: How To Add CSS and jQuery to a WordPress Plugin? I am trying to insert a button into a Wordpress plugin. I would like to customize the style of the button by linking a CSS file. I understand how to link a CSS file in an HTML docum ...

``How can I apply styling to a child component in React within a regular HTML element?

I'm currently learning React and faced a unique situation while working on projects. Here's the scenario - I have two React child components nested under a standard HTML element. If I need to target and style each individual React child element ...

The header and navigation bar are both set to stay in place, but unfortunately my div element is not displaying beneath

After setting both the Nav and Header to fixed, everything seems to start out well. I adjust the Nav margin-top so that it appears below the header, which works fine. However, things take a wrong turn when I try adjusting the div. The div ends up behind th ...

Add a partially see-through overlay to the background image

I'm designing a webpage that requires a background image with a semi-transparent black overlay on top (rgba 0 0 0 0.5). Originally, I tried this approach: <body id="body-pd" style="background: url('./img/Home e Roleta.png' ...

Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side. My initial attempt involved using flex layout, but I have ...

In Firefox, selecting the <option disabled> feature causes the corresponding option item to vanish

<select class="form-control" id="service_select"> <option disabled="disabled"></option> </select> It works fine in Chrome, but is there an alternative method to disable it without hiding it? Thank you. ...

Tips for expanding the width of an image when employing position:relative

Below is the code snippet: <table border="1" cellpadding="2" cellspacing="2" height="250"> <tbody> <tr> <td> <div style="background: url('path to image'); width: 300px; height: 250px; position: ...

One common issue that arises is the failure of a Javascript function to execute when trying to display a div that is initially hidden using the CSS

I have a div on my website that is initially hidden using this css rule: .menu-popup{ display:none; border:1px solid #FFF; padding:8px; width:100%; height:100vh; position:fixed; top:60px; overflow:hidden; } When I click a ...