简体   繁体   English

透明背景CSS

[英]Transparent background CSS

I have a navigation bar with a semi-transparent background but the navigation links are also semi-transparent. 我有一个带有半透明背景的导航栏,但导航链接也是半透明的。 How can I make it so my links do not become transparent? 我怎样才能使我的链接不透明? I have attached a copy of my code below (also available on JSFiddle . 我已经在下面附上了我的代码的副本(也可以在JSFiddle找到)

<style type="text/css">
body{
margin:0;
padding:0;
}

a{
font-family:sans-serif;
font-size:15px;
}

#nav{
height:30px;
background-color:#dddddd;
filter:alpha(opacity=60);
opacity:0.6;
}

#right{
position:absolute;
top:0;
right:0;
}

.gbt{
display:inline-block;
line-height:26px;
}

.gbtc{
margin:0;
padding:0;
}

.gbts{
padding:6px;
}
</style>

<div id="nav">
<ol class=gbtc>
<li class=gbt><a href=""><span class=gbts>Link</span></a></li>
<li class=gbt><a href=""><span class=gbts>Link</span></a></li>
<li class=gbt><a href=""><span class=gbts>Link</span></a></li>
<li class=gbt><a href=""><span class=gbts>Link</span></a></li>
<li class=gbt><a href=""><span class=gbts>Link</span></a></li>
</ol>
</div>

Any ideas are much appreciated and I hope you can understand what I'm trying to describe. 任何想法都非常感谢,希望您能理解我要描述的内容。 The code I have provided is easier to see with a background image. 我提供的代码在背景图片中更易于查看。

Thanks in advance, Callum 在此先感谢Callum

To set the opacity of the background only, you can set an rgba value for the background colour. 要仅设置背景的不透明度,可以为背景颜色设置rgba值。 This will not affect any child elements. 这不会影响任何子元素。

#nav {
  background:rgba(221, 221, 221, 0.6);
}

IE does not support rgba however. IE不支持rgba For this, you need to use a proprietary filter: 为此,您需要使用专有的过滤器:

#nav {
  background:rgba(221, 221, 221, 0.6);
  filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99dddddd,endColorstr=#  99dddddd);
}

I would do it like this: http://jsfiddle.net/5p3vN/ 我会这样: http : //jsfiddle.net/5p3vN/

html: 的HTML:

<div id="bg"></div>
<div id="nav">
    <!-- the list -->
</div>

css: CSS:

#bg{
    height:30px;
    width: 100%;
    background-color:#dddddd;
    filter:alpha(opacity=60);
    opacity:0.6;
}

#nav{
    position: absolute;
    top:0;
}

The opacity in CSS is inherited to children. CSS中的不透明度继承给孩子。 Full Stop. 句号。 To overcome this create a the transparent background to your navigation seperately and absolutely position your li items over it with a higher z-index. 为了克服这个问题,请分别为导航创建一个透明的背景,并以较高的z索引绝对将li项目定位在其上。 There are other workarounds but they all require some form of js, but to do it with pure css this is what you want. 还有其他解决方法,但是它们都需要某种形式的js,但是要使用纯CSS做到这一点,这就是您想要的。

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

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