CSS 基础(018_Opacity / Transparency)

原始网址:http://www.w3schools.com/css/css_image_transparency.asp

翻译:

CSS Opacity / Transparency


opacity 属性用以定义元素的 opacity/transparency


Transparent Image

opacity 属性取值范围是 0.0 - 1.0 。值越低,越透明:

<!DOCTYPE html>
<html>
<head>
<style>
.w3-center {
    text-align: center !important;
}

.w3-third {
    width: 30%;
    float: left;
    margin: 20px;
}

.w3-row-padding {
    width: 100%;
}
</style>
</head>
<body>
    <div class="w3-row-padding" style="margin: 0 -16px">
        <div class="w3-third w3-center">
            <img src="http://www.w3schools.com/css/img_forest.jpg" style="width: 100%; opacity: 0.2;">
            <p>opacity 0.2</p>
        </div>

        <div class="w3-third w3-center">
            <img src="http://www.w3schools.com/css/img_forest.jpg" style="width: 100%; opacity: 0.5">
            <p>opacity 0.5</p>
        </div>

        <div class="w3-third w3-center">
            <img src="http://www.w3schools.com/css/img_forest.jpg" style="width: 100%;">
            <p>opacity 1<br>(default)</p>
        </div>
    </div>
</body>
</html>

注意:**IE8 以及早期浏览器使用 filter:alpha(opacity=x) 。**x 取值范围是 0 - 100 。 值越低,越透明。

img {
    opacity: 0.5;
    filter: alpha(opacity=50); /* For IE8 and earlier */
}

Transparent Hover Effect

opacity 属性经常和 :hover 选择器一起使用以变更鼠标悬停(mouse-over)时候的透明度:

<!DOCTYPE html>
<html>
<head>
<style>
img {
    opacity: 0.5;
    filter: alpha(opacity=50); /* For IE8 and earlier */
}

img:hover {
    opacity: 1.0;
    filter: alpha(opacity=100); /* For IE8 and earlier */
}
</style>
</head>
<body>
    <h1>Image Transparency</h1>
    <p>The opacity property is often used together with the :hover selector to change the opacity on mouse-over:</p>
    <img src="http://www.w3schools.com/css/img_lights.jpg" alt="Forest" width="170" height="100">
    <img src="http://www.w3schools.com/css/img_mountains.jpg" alt="Mountains" width="170" height="100">
    <img src="http://www.w3schools.com/css/img_fjords.jpg" alt="Fjords" width="170" height="100">
    <p><b>Note:</b> In IE, a !DOCTYPE must be added for the :hover selector to work on other elements than the a element.</p>
</body>
</html>

示例解释

第一段 CSS 代码块和示例 1 中的代码是相似的。另外,当用户鼠标悬停于图片之上的时候,我们还增加了该如何变更式样的控制。在这个示例中,当用户鼠标悬停于图片之上的时候,我们想让它们不透明。相应的 CSS 代码为 opacity:1;
当鼠标指针从图片上离开的时候,图片将再次透明。

以下为悬停效果的反例

<!DOCTYPE html>
<html>
<head>
<style>
img:hover {
    opacity: 0.5;
    filter: alpha(opacity=50); /* For IE8 and earlier */
}
</style>
</head>
<body>
    <h1>Image Transparency</h1>
    <p>The opacity property is often used together with the :hover selector to change the opacity on mouse-over:</p>
    <img src="http://www.w3schools.com/css/img_lights.jpg" alt="Forest" width="170" height="100">
    <img src="http://www.w3schools.com/css/img_mountains.jpg" alt="Mountains" width="170" height="100">
    <img src="http://www.w3schools.com/css/img_fjords.jpg" alt="Fjords" width="170" height="100">
    <p><b>Note:</b> In IE, a !DOCTYPE must be added for the :hover selector to work on other elements than the a element.</p>
</body>
</html>

Transparent Box

当使用 opacity 属性对元素的背景添加透明的时候,所有的子元素也会跟着透明。这会使置于完全透明的元素内的文本难以阅读:
Transparent Box

<!DOCTYPE html>
<html>
<head>
<style>
div {
    background-color: #4CAF50;
    padding: 10px;
}

div.first {
    opacity: 0.1;
    filter: alpha(opacity = 10); /* For IE8 and earlier */
}

div.second {
    opacity: 0.3;
    filter: alpha(opacity = 30); /* For IE8 and earlier */
}

div.third {
    opacity: 0.6;
    filter: alpha(opacity = 60); /* For IE8 and earlier */
}
</style>
</head>
<body>
    <h1>Transparent Box</h1>
    <p>
        When using the opacity property to add transparency to the
        background of an element, all of its child elements become transparent
        as well. This can make the text inside a fully transparent element
        hard to read:
    </p>
    <div class="first"><p>opacity 0.1</p></div>
    <div class="second"><p>opacity 0.3</p></div>
    <div class="third"><p>opacity 0.6</p></div>
    <div><p>opacity 1 (default)</p></div>
</body>
</html>

Transparency using RGBA

如果我们不想对子元素应用 opacity,如上例所示,那么,我们还可以使用 RGBA 颜色值。以下示例只对背景色设置了透明度,对文本没有设置:
Transparency using RGBA

<!DOCTYPE html>
<html>
<head>
<style>
.w3-row-padding {
    padding: 0 8px;
}

.w3-center {
    text-align: center !important;
}

.w3-col.m3 {
    float: left;
    width: 20%;
    padding: 0 8px;
}
</style>
</head>
<body>
    <div class="w3-row-padding" style="margin:0 -16px">
        <div class="w3-col m3 w3-center">
            <div style="background:rgb(76, 175, 80);padding:50px;color:black"><p>100% opacity</p></div>
        </div>

        <div class="w3-col m3 w3-center">
            <div style="background:rgba(76, 175, 80,0.6);padding:50px;color:black"><p>60% opacity</p></div>
        </div>

        <div class="w3-col m3 w3-center">
            <div style="background:rgba(76, 175, 80,0.3);padding:50px;color:black"><p>30% opacity</p></div>
        </div>

        <div class="w3-col m3 w3-center">
            <div style="background:rgba(76, 175, 80,0.1);padding:50px;color:black"><p>10% opacity</p></div>
        </div>
    </div>
</body>
</html>

你可以从 CSS Colors Chapter 章节学习以使用 RGB 作为颜色值。另外,CSS3 是以 RGBA 来介绍 RGB 颜色值的,A 即 an alpha channel 是用以定义颜色的透明度。
我们使用 rgba(red, green, blue, alpha) 来指定 RGBA 颜色值。alpha 参数值是介于 0.0 和 1.0 之间的数。
提示:你将在 CSS3 Colors Chapter 章节学习到更多有关 RGBA 颜色的内容。

<!DOCTYPE html>
<html>
<head>
<style>
div {
    background: rgb(76, 175, 80);
    padding: 10px;
}

div.first {
    background: rgba(76, 175, 80, 0.1);
}

div.second {
    background: rgba(76, 175, 80, 0.3);
}

div.third {
    background: rgba(76, 175, 80, 0.6);
}
</style>
</head>
<body>

    <h1>Transparent Box</h1>
    <p>With opacity:</p>
    <div style="opacity:0.1;"><p>10% opacity</p></div>
    <div style="opacity:0.3;"><p>30% opacity</p></div>
    <div style="opacity:0.6;"><p>60% opacity</p></div>
    <div><p>opacity 1</p></div>

    <p>With RGBA color values:</p>
    <div class="first"><p>10% opacity</p></div>
    <div class="second"><p>30% opacity</p></div>
    <div class="third"><p>60% opacity</p></div>
    <div><p>default</p></div>

    <p>Notice how the text gets transparent as well as the background color when using the opacity property.</p>

</body>
</html>

Text in Transparent Box

<!DOCTYPE html>
<html>
<head>
<style>
div.background {
  background: url(http://www.w3schools.com/css/klematis.jpg) repeat;
  border: 2px solid black;
}

div.transbox {
  margin: 30px;
  background-color: #ffffff;
  border: 1px solid black;
  opacity: 0.6;
  filter: alpha(opacity=60); /* For IE8 and earlier */
}

div.transbox p {
  margin: 5%;
  font-weight: bold;
  color: #000000;
}
</style>
</head>
<body>
    <div class="background">
        <div class="transbox">
            <p>This is some text that is placed in the transparent box.</p>
        </div>
    </div>
</body>
</html>

首先,我们创建了一个有背景图片和边框的 <div> 元素(class="background"),然后,我们在其内部创建了另一个 <div> 元素(class="transbox")。<div class="transbox"> 有背景色、边框 - div 是透明的。在透明 div 的内部,我们在 <p> 元素内添加了相应的文本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您想了解CSS3蒲公英飘动效果和CSS3云朵飘动效果动画的具体实现方法吗?我可以为您提供一些简单的代码示例和实现步骤。 首先,让我们来看一下CSS3蒲公英飘动效果的实现方法。下面是一个简单的实例代码: ```html <!DOCTYPE html> <html> <head> <title>CSS3蒲公英飘动效果</title> <style> /* 设置body的背景颜色和字体颜色 */ body { background-color: #34AADC; color: #fff; } /* 设置蒲公英的样式 */ .dandelion { position: absolute; width: 30px; height: 30px; background-image: url("https://cdn.pixabay.com/photo/2014/04/03/10/30/dandelion-310807_960_720.png"); background-size: 100% 100%; animation: dandelion 5s linear infinite; } /* 设置蒲公英飘动的动画 */ @keyframes dandelion { 0% { transform: translate(0, 0); opacity: 1; } 50% { transform: translate(500px, -500px); opacity: 0.5; } 100% { transform: translate(1000px, -1000px); opacity: 0; } } </style> </head> <body> <div class="dandelion"></div> </body> </html> ``` 在这个示例中,我们使用了CSS3的动画属性`animation`和关键帧`@keyframes`来实现蒲公英的飘动效果。我们首先定义了一个类名为`dandelion`的元素,并设置了它的样式,包括背景图片、大小和动画,然后在关键帧中定义了蒲公英的运动轨迹和透明度变化。最后,我们在页面中插入了一个`div`元素,并将它的类名设置为`dandelion`,这样就可以实现蒲公英的飘动效果了。 接下来,我们来看一下CSS3云朵飘动效果的实现方法。下面是一个简单的实例代码: ```html <!DOCTYPE html> <html> <head> <title>CSS3云朵飘动效果</title> <style> /* 设置body的背景颜色和字体颜色 */ body { background-color: #34AADC; color: #fff; } /* 设置云朵的样式 */ .cloud { position: absolute; width: 100px; height: 50px; background-image: url("https://cdn.pixabay.com/photo/2016/09/17/16/26/cloud-1674193_960_720.png"); background-size: 100% 100%; animation: cloud 10s linear infinite; } /* 设置云朵飘动的动画 */ @keyframes cloud { 0% { transform: translate(-100px, 0); } 100% { transform: translate(1000px, 0); } } </style> </head> <body> <div class="cloud"></div> </body> </html> ``` 在这个示例中,我们同样使用了CSS3的动画属性`animation`和关键帧`@keyframes`来实现云朵的飘动效果。我们首先定义了一个类名为`cloud`的元素,并设置了它的样式,包括背景图片、大小和动画,然后在关键帧中定义了云朵的运动轨迹。最后,我们在页面中插入了一个`div`元素,并将它的类名设置为`cloud`,这样就可以实现云朵的飘动效果了。 希望这些代码示例和实现步骤对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值