Home  >  Article  >  Web Front-end  >  How to set the transparency of images in css

How to set the transparency of images in css

青灯夜游
青灯夜游Original
2021-04-21 15:51:3920797browse

How to set image transparency in css: 1. Use the filter attribute to add the "filter:opacity(value%)" style to the image element; the value is between 0 and 100, and "0%" is completely transparent. , "100%" means there is no change in the image. 2. Use the opacity attribute, the syntax "opacity: value".

How to set the transparency of images in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

Method 1: Use the filter attribute--filter:opacity(%)

The filter attribute defines the visibility of the element (usually) Effects (e.g. blur and saturation).

opacity(%): used to convert the transparency of the image. The value defines the scale of the conversion. A value of 0% means complete transparency, a value of 100% means no change to the image. Values ​​between 0% and 100% are linear multipliers of the effect, equivalent to multiplying the number of image samples. If the value is not set, the value defaults to 1. This function is very similar to the existing opacity attribute, except that through filter, some browsers provide hardware acceleration to improve performance.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<style>
			.img1{
				-webkit-filter: opacity(50%); /* Chrome, Safari, Opera */
				filter: opacity(50%);
				
			}
			.img2{
				-webkit-filter: opacity(20%); /* Chrome, Safari, Opera */
				filter: opacity(20%);
				
			}
		</style>
	</head>
	<body>
		<div>
			<p>原图:</p>
		  <img  src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
		<div>
			<p>透明度为50%:</p>
		  <img  class="img1" src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
		<div>
			<p>透明度为20%:</p>
		  <img  class="img2" src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
	</body>
</html>

Rendering:

How to set the transparency of images in css

##Method 2: Use the opacity attribute

The Opacity attribute sets the transparency level of an element. Range of values: from 0.0 (fully transparent) to 1.0 (fully opaque).

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<style>
			.img1{
				opacity: 0.5;
				
			}
			.img2{
				opacity: 0.2;
				
			}
		</style>
	</head>
	<body>
		<div>
			<p>原图:</p>
		  <img  src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
		<div>
			<p>透明度为0.5:</p>
		  <img  class="img1" src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
		<div>
			<p>透明度为0.2:</p>
		  <img  class="img2" src="img/1.jpg"    style="max-width:90%"/ alt="How to set the transparency of images in css" >
		</div>
	</body>
</html>

Rendering:


How to set the transparency of images in css

(Learning video sharing:

css video tutorial )

The above is the detailed content of How to set the transparency of images in css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn