css - 什么时候应该在其他类选择器定义中定义 CSS 类选择器?

标签 css

编辑 1 根据建议,我尝试了这个:

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

/* nested for adding classes to change 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;
    }
}

在元素面板中我看到: enter image description here

但是在样式面板中我有: enter image description here

因此,虽然我正在应用附加类,但高度没有设置,附加类也没有显示在样式中。


原始问题 我正在像这样在其他类中定义一些类:

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

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

然后当需要改变我正在做的高度时:

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

但在控制台上,该元素在样式面板中没有 .lower-full 样式, 即使在元素面板中显示 class="{other classes} lower-full"

我做错了什么?

最佳答案

您不能仅使用原始 CSS 在另一个选择器中定义 CSS 选择器,您需要像 SASS 这样的预处理器。

使用 CSS,您可以指定元素何时只有 .lower-container 或两者都有的属性。这将等同于您尝试在原始代码中完成的工作。

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

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

如果您已经在使用 SCSS 文件,那么您使用的语法不正确

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

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

根据最新编辑进行编辑:代码应如下所示:

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

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

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

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

关于css - 什么时候应该在其他类选择器定义中定义 CSS 类选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69331035/

相关文章:

css - 如何组合 2 个 SASS 函数,因为它们只是使用不同的计算做几乎相同的事情?

html - 并排放置缩略图

css - 在旋转的 div 中旋转背景图像?

android - 在webview中访问Android的系统字体

CSS:如何显示隐藏类

css - 如何在不指定其位置两次的情况下围绕其中心旋转符号?

javascript - 为什么我使用 css 时我的页面没有加载?

html - 我需要将 <div> 置于 <td> 的中心

javascript - 如何切换列表中的元素单击?

html - CSS - 在输入检查时更改父 div 背景颜色