如何仅将css应用于顶级元素

Seb*_*n K 3 css css-selectors

在我的HTML中,我在单独的部分中有一些页脚元素,然后是页面的一个全局页脚元素.我想只使全局页脚显示在视口的底部.

如果我这样做,它将影响所有的页脚:

footer {
    position: fixed;
    bottom: 0px;    
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做,它按预期工作,但body语法看起来很难:

body > footer {
    position: fixed;
    bottom: 0px;    
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来指定顶级页脚?

rei*_*der 7

将一个类添加到您的页脚并将您的CSS应用于该特定类.正如其他人指出的那样,没有任何问题body > footer.