我需要使一个白色背景透明度为50%,而不影响其他任何内容。如何实现?
我需要使一个白色背景透明度为50%,而不影响其他任何内容。如何实现?
使用 rgba()
:
.transparent {
background-color: rgba(255,255,255,0.5);
}
使用以下代码可以使盒子的透明度为50%,同时盒子内容保持100%不透明。
background-color: rgba(0, 0, 0, 0.5);
如果您使用opacity:0.5
,则盒子内容和背景都会变得透明。因此,请避免使用它。
background:
替换为 background-color:
。 - Gabrielizalobackground-color
并不比background
更正确。 - Sean这个方法可以使用,但是具有该类的所有子元素也会变得透明,没有任何方法防止这种情况发生。
.css-class-name {
opacity:0.8;
}
如果您想将背景透明化为灰色,请尝试:
.transparent{
background:rgba(1,1,1,0.5);
}
需要知道的事情
一些网络浏览器在透明背景上渲染带有阴影的文本时会出现困难。这时,您可以使用一个半透明的1x1 PNG图像作为背景。
注意
请记住,IE6不支持PNG文件。
div.main{
width:100%;
height:550px;
background: url('https://images.unsplash.com/photo-1503135935062-
b7d1f5a0690f?ixlib=rb-enter code here0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=cf4d0c234ecaecd14f51a2343cc89b6c&dpr=1&auto=format&fit=crop&w=376&h=564&q=60&cs=tinysrgb') no-repeat;
background-position:center;
background-size:cover
}
div.main>div{
width:100px;
height:320px;
background:transparent;
background-attachment:fixed;
border-top:25px solid orange;
border-left:120px solid orange;
border-bottom:25px solid orange;
border-right:10px solid orange;
margin-left:150px
}
这很简单而且简洁。像下面这样使用hsla css函数
.transparent {
background-color: hsla(0,0%,4%,.4);
}
尽管有些过时,但是这个帖子上的任何一个答案都不能普遍适用。使用rgba创建透明色掩码 - 这并不完全解释如何在背景图像中实现。
我的解决方案适用于背景图像或彩色背景。
#parent {
font-family: 'Open Sans Condensed', sans-serif;
font-size: 19px;
text-transform: uppercase;
border-radius: 50%;
margin: 20px auto;
width: 125px;
height: 125px;
background-color: #476172;
background-image: url('https://unsplash.it/200/300/?random');
line-height: 29px;
text-align:center;
}
#content {
color: white;
height: 125px !important;
width: 125px !important;
display: table-cell;
border-radius: 50%;
vertical-align: middle;
background: rgba(0,0,0, .3);
}
<h1 id="parent"><a href="" id="content" title="content" rel="home">Example</a></h1>
试试这个:
.transparent
{
opacity:.50;
-moz-opacity:.50;
filter:alpha(opacity=50);
}
.transparent * { opacity:1; }
,除非例如带有transparent
类的元素具有内部html。 - Tim Cooke