[英]How to conditionally apply css to differently nested content
I'm using sass and the structure of the page is not easily changed. 我正在使用sass,页面的结构不容易更改。
I have the following structure because my header is two different sizes depending on the page of the site users are visiting: 我具有以下结构,因为根据用户访问的网站页面的不同,标题是两种不同的大小:
<div class="container">
<div class="header">
<div id="large-header"></div> (or <div id="small-header"></div>)
</div>
<div class="full-screen-menu"></div>
</div>
I want full screen menu to either be 50px or 100px depending on if .header > #small-header
or .header > #large-header
so that the header is still visible with the full screen menu. 我希望全屏菜单为50px或100px,具体取决于.header > #small-header
或.header > #large-header
以便该标题在全屏菜单中仍然可见。 Ideally I'd be able to use a conditional like 理想情况下,我可以使用条件式
.container {
@if .header > #large-header {
.full-screen-menu {
top: 100px;
}
} @else {
.full-screen-menu {
top: 50px;
}
}
}
The problem is that I can't use if statements and I also don't know a way to check a conditional traversing up the DOM of one div
and have it apply to a div
further down the page that share a common parent. 问题是我无法使用if语句,而且我也不知道一种方法来检查条件遍历一个div
的DOM,并将其应用于共享公共父级的页面下的div
。
Do you have any ideas for how I could accomplish this? 您对我如何实现此目标有任何想法吗?
SCSS: SCSS:
.container {
.header {
height:100px;
}
.full-screen-menu {
height:100px;
}
&--small-header {
.header {
height:50px;
}
.full-screen-menu {
height:50px;
}
}
}
.container .header { height: 100px; background:rgba(255,0,0,0.2); } .container .full-screen-menu { top: 100px; background:rgba(0,0,255,0.2); } .container--small-header .header { height: 50px; } .container--small-header .full-screen-menu { top: 50px; }
<div class="container"> <div class="header"> HEADER </div> <div class="full-screen-menu">MENU</div> </div> <hr/> <div class="container container--small-header"> <div class="header"> HEADER </div> <div class="full-screen-menu">MENU</div> </div>
you can actually use something like this: 您实际上可以使用如下所示的内容:
.full-screen-menu {
top: 50px;
.header > #large-header > & {
top: 100px;
}
}
just like putting an '&' before a nested selector appends it to the back of the parent selector, it works in reverse if you put it after a nested selector. 就像在嵌套选择器之前将'&'附加到父选择器的后面一样,如果将其放在嵌套选择器之后,它会反向工作。 So when thats compiled to css the selector is .header > #large-header > .full-screen-menu
因此,当多数民众赞成在编译到CSS时,选择器是.header > #large-header > .full-screen-menu
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.