When is the appropriate time to nest CSS class selectors within other class selector definitions?

REVISED 1 After receiving feedback, I made the following changes:

.lower-container {
    justify-content: center;
    position: absolute;
    width: 92vw;
//    height: 46vh; <--- no set height

/* nested classes for adjusting position and height of lower-container */
    .lower-container.lower-full {
      height: 92vh;
      top: 0;
    }

    .lower-container.lower-mid {
      height: 46vh;
      top: 46vh;
    }

    .lower-container.lower-closed {
      height: 1vh;
      top: 91vh;
    }
}

When inspecting in the Elements Panel: https://i.stack.imgur.com/e2u8N.png

However, in the Styles panel, there is a discrepancy: https://i.stack.imgur.com/U22sQ.png

Despite applying the additional class, the height is not being adjusted and the new class isn't visible in the styles.


Initial Inquiry I have defined some nested classes like this:

.lower-container {
    height: 46vh;
    display: flex;
    justify-content: center;
    ...

    .lower-full {
        height: 92vh;
    }
}

Then, when trying to change the height, I am executing:

    lowerContainerElement.classList.add('lower-full')

However, upon checking the console, the element does not reflect the .lower-full style in the Styles panel, even though the Elements panel displays class="{other classes} lower-full"

What could be the issue here?

Answer №1

It's not possible to define a CSS selector within another one using vanilla CSS; SASS would be required for that. With CSS, you can set properties based on whether an element has only the .lower-container class or both classes. This achieves a similar effect to what you were attempting in your original code.

.lower-container {
   height: 46vh;
   display: flex;
   justify-content: center;
   ...
}

.lower-container.lower-full {
    height: 92vh;
}

If you're already working with SCSS files, make sure you're using the correct syntax:

.lower-container {
    height: 46vh;
    display: flex;
    justify-content: center;
    ...

    &.lower-full {
        height: 92vh;
   }
}

Update based on the latest edits: The code should be structured as follows:

.lower-container {
    justify-content: center;
    position: absolute;
    width: 92vw;
//    height: 46vh; <--- no height defined

/* Add nested classes to modify position and height of lower-container */
    &.lower-full {
      height: 92vh;
      top: 0;
    }

    &.lower-mid {
      height: 46vh;
      top: 46vh;
    }

    &.lower-closed {
      height: 1vh;
      top: 91vh;
    }
}

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

What is the best way to widen each tab so they fill up the entire width of the content block?

As a newcomer to both programming and this website, I've been exploring various codes for creating vertical and horizontal tabs. I have a specific query about this particular example found at https://jsfiddle.net/eu81273/812ehkyf/: My goal is to adju ...

Ensure that the heights of columns in UL CSS are aligned regardless of the number of lines they contain

In my current project, I am using CSS columns to establish a two-column layout for an unordered list. Here is the code snippet: HTML <div class="meta-data"> <ul> <li><i class="fa fa-chevron-right fa-xs"></i><str ...

The inclusion of the <div> tag disrupts the functionality of the Material UI Grid

While attempting to enclose my element within a <div> that is nested inside the <Grid>, I realized that this was causing the <Grid> layout to break! Surprisingly, when I tried the same approach with Bootstrap grid system, this issue did n ...

Is it possible to utilize viewport height as a trigger for classes in Gatsby?

Unique Case Working on a Gatsby site with Tailwind CSS has brought to light an interesting challenge regarding different types of content. While the blog pages fill the entire viewport and offer scrolling options for overflowing content, other pages with ...

Is there a way to show an element on a website only when the height of the HTML content exceeds the height of the viewport?

My webpage serves as a dynamic to-do list, allowing users to add an endless number of tasks of all types. As the list grows in size, the page height increases and a scrollbar appears. Positioned at the bottom right corner of the page is a fixed button that ...

SCSS files scattered throughout Solution Explorer create disorder in Visual Studio

I currently work with Visual Studio 2013. Typically, in the solution explorer, scss files are displayed with their .css, .css.map, and .min.css files neatly grouped under the main .scss file. However, I'm experiencing a lack of organization as all th ...

What is the best way to display the user login in the center of a page with all necessary controls

Challenge How can the user login be displayed in the center of the page? Additional Information The current layout displays user login data on the left-hand side. I am looking to have the user login data centralized on the page, which includes labels f ...

Concealing a child component when hovering over its parent element with styled-components

I have a react component structured like this - const MyComponent = () => ( <ContainerSection> <DeleteButtonContainer> <Button theme="plain" autoWidth onClick={() = ...

Issue with jQuery's .show method not functioning properly following the .hide method

Is there a way to make it so that only the "pastInvestments" are displayed when the "pastButton" is clicked, and only the "currentInvestments" are displayed when the "currentButton" is clicked? In the HTML code below, there are 2 buttons and 2 divs: < ...

Utilizing the '::marker' pseudo-element for generating Markdown-inspired headers

This code snippet is functioning correctly, however, I have a suggestion to make it more appropriate. Instead of using '::before', why not try using '::marker'? I attempted this adjustment but encountered an issue. Can anyone explain wh ...

Dealing with challenges in integrating ngx-masonry with Angular 14

I am currently working with Angular 14 framework and the ngx-masonry library (https://www.npmjs.com/package/ngx-masonry/v/14.0.1). However, I am facing some issues where it is not functioning correctly. I would appreciate any assistance or guidance on how ...

The webpage fails to return to its original position after the script has been executed

My website has a sticky div that stays at the top when scrolling down, but does not return to its original position when scrolling back up. Check out this example function fixDiv() { var $div = $("#navwrap"); if ($(window).scrollTop() > $div.data("top ...

Repositioning a variable number of divs as the cursor hovers over the target area

Can anyone show me how to generate div elements in a loop using jQuery and change their position with the "mouseover" function? I've tried some code, but the positioning isn't changing correctly. Any suggestions on what needs fixing? var r, g, ...

I'm wondering why my positive numbers aren't displayed in green and negative numbers in red

I am currently working on a commodities quotes widget. I have already set up the 'Current' and '24-hour' divs, but I'm facing an issue where positive values are not displaying in green and negatives in red as intended. I have check ...

The width of DIVs in Mac Safari exceeds that of other browsers

Encountering an issue that is specific to MAC Safari. Here's the code snippet: <div class="wrapper" style="max-width:1220px"> <div style="width:50%;display:inline-block">First</div> <div style="width:25%;display:inline-block"&g ...

Internet Explorer 9 users experience a sudden blank page display

Are there any effective methods for diagnosing why a page suddenly becomes blank in Internet Explorer? I am aware that switching to Compatibility Mode 7 is an option, however dealing with the quirks of IE7 can be even more challenging. ...

I currently have two responsive menus and I'm trying to figure out how to modify the javascript so that when one menu is opened, the other

I am facing an issue with my responsive menus on a webpage, similar to the example provided in the jsfiddle link below. Currently, when one menu is open and I click on another, both remain open. How can I modify the JavaScript code so that when one menu op ...

Press the "Reveal More" button to expand the content to its full size

I am currently working on coding a "read more" section using a button. I have looked at some existing solutions, but none of them are to my liking. My plan is to create a div that includes all the relevant content. My goal is to have a div at the bottom w ...

Dynamic banner image

I currently have a jumbotron image in the background, but it isn't as responsive and I'm struggling to get the whole image to display properly. Here is what it looks like currently: https://i.sstatic.net/IpuDi.png Here is the full image for ref ...

Safari not centering element with justify-self in CSS grid

My challenge involves adjusting the alignment of a div (mobile menu) that is currently centered using css grid and the justify-self property in chrome. However, when viewed in Safari, it appears on the left side of the screen behind the Instagram icon: ht ...