gpt4 book ai didi

javascript - 路径中每个节点的CSS选择器

转载 作者:行者123 更新时间:2023-11-30 09:38:23 25 4
gpt4 key购买 nike

我试图让一个 div 来填充剩余屏幕空间的高度,并在这里找到了很好的答案:Make a div fill the height of the remaining screen space (选择的答案)。

我的问题 https://stackoverflow.com/users/1490904/pebbl的是,他所呈现的内容的 flex 容器是 height:100% 并且是 html 和 body 元素的直接子元素,height:100%。在我的应用程序中,主体和 flex 容器之间有一堆中间元素,如果我希望 flex 容器填充所有可用的垂直空间,这些元素中的每一个也必须具有 height:100%

如果我有像这样的 css 规则,它会很好用

 * {
height: 100%;
}

但这很疯狂,对吧?我不知道将我的 DOM 中的每个元素都设置为 100% 是否有问题,但如果不需要,我宁愿不这样做。

我试过了(认为这会选择 body 和我的 flex-box 之间的每个元素):

body * .flex-box {
height: 100%;
}

没有运气。

我的 html(经过 React 处理后)看起来有点像:

<html>
<body>
<div>
<div class="another-div">
<div class="another-div navbar-and-whatnot">
<div class="yet-another-bunch-of-divs">
<div class="flex-box">
<div class="flex-remaining-height">
<svg class="this-should-be-big" />
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

我的 scss 文件的相关部分(包括沿路径设置高度的非工作尝试)是:

html, body {
height: 100%;
}
body * .flex-box { // this part doesn't work. the rest comes from https://stackoverflow.com/a/24979148/1368860
height: 100%;
}
.flex-box {
display: flex;
flex-flow: column;
height: 100%;
//.flex-row { border: 1px dotted grey; }
.flex-content-height {
flex: 0 1 auto;
/* The above is shorthand for:
flex-grow: 0,
flex-shrink: 1,
flex-basis: auto
*/
}
.flex-remaining-height {
flex: 1 1 auto;
}
.flex-fixed-height-40 {
flex: 0 1 40px;
}
}

我想现在我可以弄清楚 body 和我的 flex-box 之间路径中的每个元素并为它们设置高度,但这非常脆弱。我需要不断地修复它。

顺便说一句,我正在使用 react-bootstrap,所以如果有更简单的解决方案,我会采用。

而且,对于进一步的上下文,我想这样做的原因是我正在使用一个库来创建一个与其周围的 div 一样大的 SVG 元素,我希望 SVG 尽​​可能大(并适合屏幕)。

附言我也加载了 jquery。我想这将是最简单的修复方法。不过,如果有一个 css 解决方案就好了。

最佳答案

CSS 中无法选择这种“路径”。 使用 * 选择所有内容是危险的,而且对性能也不利。 我建议您改为自己定义路径。最简单的方法可能是定义一个类,比如 .full-height 并将该类赋予路径中的所有元素。这样您只需将类添加到您添加到此路径的任何新元素,CSS 可以保持不变。

.full-height {
height: 100%
}

关于javascript - 路径中每个节点的CSS选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42367607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com