search
HomeWeb Front-endCSS TutorialHow to add shadow effect to images with css? Two ways to add shadows to pictures (code examples)

css can achieve many picture effects, and picture shadow effect is one of them. So how to add shadow effect to pictures in css? This article will introduce to you how to use css to add shadows to images (with code), and let you know the two methods of adding shadows to images with css. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First let’s take a look at how to add shadow effects to images using cssThe first method: setting the box-shadow attribute, let’s take a look at the implementation of the box-shadow attribute through a simple code example method.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>css 图片阴影--box-shadow属性</title>
		<style>
			.demo{
				width: 400px;
				height: 300px;
				margin: 50px auto;
			}
			.demo img{
				 box-shadow: 10px 10px 10px rgba(0,0,0,.5);
				 /*考虑浏览器兼容性*/
				 -moz-box-shadow: 10px 10px 10px rgba(0,0,0,.5);
                 -webkit-box-shadow: 10px 10px 10px rgba(0,0,0,.5);
			}
		</style>
	</head>
	<body>
		<div class="demo">
			<img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn//upload/image/168/638/459/1539834402154297.jpg?x-oss-process=image/resize,p_40"  class="lazy"   / alt="How to add shadow effect to images with css? Two ways to add shadows to pictures (code examples)" >
		</div>
	</body>
</html>

Rendering:

How to add shadow effect to images with css? Two ways to add shadows to pictures (code examples)

#The box-shadow attribute is not very simple to set the image shadow. Let’s take a look at how to set the box-shadow attribute. of:

box-shadow: h-shadow v-shadow blur spread color inset;

h-shadow: Required value that defines the position of the horizontal shadow. Negative values ​​are allowed.

v-shadow: A required value that defines the position of the vertical shadow. Negative values ​​are allowed.

blur: Optional setting value, defining the blur distance.

spread: Optional setting value that defines the size of the shadow.

color: Optional set value that defines the color of the shadow. If no value is set, the color value is based on browser display. It is recommended to set it.

inset: The value of the optional setting. After setting, the outer shadow (outset) can be changed to the inner shadow.

Next let’s take a look at how to add shadow effects to images using cssThe second method: use the filter attribute of css3 -----filter attribute to set the shadow of the image.

You can set filter:drop-shadow(); to add a shadow to the image. Let’s see how to implement it through a simple code example.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>css 图片阴影--box-shadow属性</title>
		<style>
			.demo{
				width: 400px;
				height: 300px;
				margin: 50px auto;
			}
			.demo img{
				-webkit-filter: drop-shadow(10px 10px 10px rgba(0,0,0,.5)); /*考虑浏览器兼容性:兼容 Chrome, Safari, Opera */
                filter: drop-shadow(10px 10px 10px rgba(0,0,0,.5));
			}
		</style>
	</head>
	<body>
		<div class="demo">
			<img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn//upload/image/763/722/108/1539841316558232.jpg?x-oss-process=image/resize,p_40"  class="lazy"   / alt="How to add shadow effect to images with css? Two ways to add shadows to pictures (code examples)" >
		</div>
	</body>
</html>

Rendering:

How to add shadow effect to images with css? Two ways to add shadows to pictures (code examples)

Description:

filter:drop-shadow(h-shadow v-shadow blur spread color);

h-shadow: Set the horizontal offset of the shadow; negative values ​​are allowed , negative values ​​will cause the shadow to appear on the left side of the element.

v-shadow: Set the vertical offset of the shadow; negative values ​​are allowed, and negative values ​​will cause the shadow to appear above the element.

blur: Set the blur of the shadow. The larger the value, the blurrier the shadow will be. The shadow will become larger and lighter; negative values ​​are not allowed. If not set, the default is 0 (the boundary of the shadow is very small). sharp).

spread: Set the size of the shadow. Positive values ​​will cause the shadow to expand and become larger, while negative values ​​will cause the shadow to shrink. If not set, the default is 0 (the shadow will be the same size as the element).
Note: Spread is not supported in Webkit and some other browsers, and it will not render if added.

color: Set the shadow color; if not set, the color value is based on the browser, it is recommended to set the color.

Summary: The above is the complete content of the two methods of adding shadows to images with CSS. I hope it will be helpful to everyone's learning. For more related tutorials, please visit CSS3 Video Tutorial, Html5 Video Tutorial, bootstrap Video Tutorial!

The above is the detailed content of How to add shadow effect to images with css? Two ways to add shadows to pictures (code examples). 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
Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software