简体   繁体   English

固定位置导航栏

[英]Fixed position navigation bar

I have a navigation bar that I would like to keep at a fixed position (top), on my website. 我有一个导航栏,我想在我的网站上保持固定位置(顶部)。 I would also like for the navigation bar to have some transparency. 我还希望导航栏有一些透明度。 I applied the transparency to the navigation bar, which also makes any content (child elements) inside that div also inherit the transparency. 我将透明度应用于导航栏,这也使得该div内的任何内容(子元素)也继承透明度。 So, to solve that I applied a relative position to the content inside of the navigation bar, but now the content that is supposed to appear fixed (inside the navigation bar) scrolls with the rest of the page content. 因此,为了解决这个问题,我将相对位置应用于导航栏内的内容,但现在应该看起来固定的内容(在导航栏内)与页面内容的其余部分一起滚动。 How can I fix this? 我怎样才能解决这个问题?

fiddle 小提琴

Don't use opacity in this case, use RGBa for the background of the navigation bar background: rgba(0,0,0,.5); 请不要在此情况下,使用不透明,使用RGBabackground的导航栏的background: rgba(0,0,0,.5); (and filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#0F000000, endColorstr=#0F000000); for IE 8 and older). (和filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#0F000000, endColorstr=#0F000000);对于IE 8及更早版本)。 All children inherit the opacity value and there is nothing you can do to about it (except setting the parent's opacity back to 1 again, in which case you won't have semi-transparency on the parent anymore). 所有的孩子都继承了opacity值,你无法做任何事情(除了将父母的opacity再次设置为1 ,在这种情况下你不再对父母进行半透明)。

Demo (changed back to original structure with navigation content inside navigation container and RGBa) 演示 (更改回原始结构,导航容器和RGBa中的导航内容)

Demo#2 (uses current structure and opacity , changed position for navigation content to fixed ) 演示#2 (使用当前结构和opacity ,将导航内容的位置更改为fixed

Why not add another transparency rule into your CSS for the elements inside the nav bar, to override the one which they inherit from the bar? 为什么不在CSS中为导航栏内的元素添加另一个透明度规则,以覆盖他们从栏中继承的那个? Changing the position attribute shouldn't be necessary, I don't see how this will affect transparency. 不需要更改位置属性,我不知道这将如何影响透明度。

您可以使用CSS伪选择器:children并将不透明度设置为1.这将使该元素的所有子元素都不透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM