Box Shadow CSS教程–如何向任何HTML元素添加投影

We can add a drop shadow to any HTML element using the CSS property box-shadow. Here's how.

我们可以使用CSS属性box-shadow将阴影添加到任何HTML元素。 这是如何做。

添加基本​​投影 (Adding a Basic Drop Shadow)

Let's first set up some basic HTML elements to add our drop shadows to:

让我们首先设置一些基本HTML元素,以将阴影添加到:

<div id="box1" class="box">Box1</div>
<div id="box2" class="box">Box2</div>
<div id="box3" class="box">Box3</div>

Then add some basic CSS:

然后添加一些基本CSS:

p {padding: 10px;
}
.box {padding: 20px;width: 50%;margin: 30px auto;background: #000;color: #fff;
}

The result is just three black boxes that will be easy for us to add drop shadows to by calling their unique id's:

结果只有三个黑匣子,我们可以通过调用它们的唯一ID轻松添加阴影:

To add a basic drop shadow, let's use the box-shadow property on the Box 1:

要添加基本的阴影,请使用Box 1上的box-shadow属性:

/* offset-x | offset-y | color */
#box1 {box-shadow: 6px 12px yellow;
}

We have 3 parameters here. The first 2 are, respectively, the x-offset and y-offset. They set the location of the drop shadow.

这里有3个参数。 前两个分别是x偏移和y偏移。 他们设置阴影的位置。

The offset is relative to the origin, which in HTML is always the top left corner of an element. A positive x-offset will move the shadow to the right, and a positive y-offset will move the shadow downwards.

偏移量是相对于原点的,原点在HTML中始终是元素的左上角。 正x偏移将阴影向右移动,正y偏移将阴影向下移动。

The third parameter is the color of your drop shadow.

第三个参数是投影的颜色。

Keep in mind that although we used <div> elements here, the box-shadow property can be applied to any other HTML element as well.

请记住,尽管我们在这里使用了<div>元素,但是box-shadow属性也可以应用于任何其他HTML元素。

添加模糊半径 (Adding a Blur Radius)

If we want the shadow to look a little more realistic, we will want to experiment with the blur-radius parameter.

如果我们希望阴影看起来更真实一些,我们将尝试使用blur-radius参数。

This parameter controls how much to blur the shadow such that it becomes bigger and lighter. Let's apply it to Box 2:

此参数控制模糊程度以使其变大和变亮的程度。 让我们将其应用于方框2:

/* offset-x | offset-y | blur-radius | color */
#box2 {box-shadow: 6px 12px 4px red;
}

The value of 4px sets the radius of the blur to apply to our drop shadow.

4px的值设置模糊的半径以应用于我们的阴影。

添加传播半径 (Adding a Spread Radius)

If we want to control the size of the shadow, we can use the spread-radius parameter which controls how much a shadow grows or shrinks.

如果要控制阴影的大小,则可以使用spread-radius参数来控制阴影的增长或收缩程度。

Let's add a spread radius of 8px to Box 2:

让我们在框2中添加一个8px的展开半径:

/* offset-x | offset-y | blur-radius | spread-radius | color */
#box2 {box-shadow: 6px 12px 4px 8px red;
}

Remember the order of these parameters!

记住这些参数的顺序!

在单个属性中组合多个阴影 (Combining Multiple Shadows in a Single Property)

If we want to get fancy, we can add multiple drop shadows to an element using a single box-shadow property.

如果想花哨的话,可以使用单个box-shadow属性为元素添加多个阴影。

Let's do that with Box 3 by simultaneously adding a blue and green drop shadow:

让我们在方框3中同时添加一个蓝色和绿色的阴影来做到这一点:

/* Any number of shadows, separated by commas */
#box3 {box-shadow: 6px 12px 2px 2px blue, -6px -12px 2px 2px green;
}

奖励:创建插入阴影 (Bonus: Create an Inset Shadow)

While it will not create a drop shadow, the inset parameter can also be used with the box-shadow property.

尽管不会创建阴影,但inset参数也可以与box-shadow属性一起使用。

As the name suggests, this parameter creates an inset shadow (i.e. shadow inside a box).

顾名思义,此参数将创建插入阴影(即,框内的阴影)。

The inset parameter can be placed either at the beginning or the end of the box-shadow property. Here we demonstrate its use with a blockquote element.

可以将inset参数放在box-shadow属性的开头或结尾。 在这里,我们通过blockquote元素演示其用法。

HTML:

HTML:

<blockquote><q>The key to success is to start before you're ready.</q><p>&mdash; Marie Forleo</p>
</blockquote>

CSS:

CSS:

blockquote {width: 50%;margin: 50px auto;padding: 20px;font-size: 24px;box-shadow: inset 10px 5px black;
}

Of course you can add some blur and spread to enhance the shadow, or even multiple shadows:

当然,您可以添加一些模糊和散布以增强阴影,甚至多个阴影:

box-shadow: inset 10px 5px 25px 5px black, 5px 5px 12px 2px black;

With the box-shadow property, we can easily make elements on a web page stand out to create a nice 3D lighting effect.

使用box-shadow属性,我们可以轻松地使网页上的元素突出以创建漂亮的3D照明效果。

If you want to do some experimenting yourself, here's a code pen I created with the examples used in this tutorial.

如果您想做一些实验,请使用我在本教程中使用的示例创建的一支代码笔 。

Play around and see what you can come up with!

玩转,看看你能想到什么!

是否想查看更多Web开发技巧和知识? (Want to See More Web Development Tips and Knowledge?)

  • Subscribe to my weekly newsletter

    订阅我的每周新闻

  • Visit my blog at  1000 Mile World

    访问我在1000 Mile World上的博客

  • Follow me on Twitter

    在Twitter上关注我

翻译自: https://www.freecodecamp.org/news/css-tutorial-drop-shadow/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390111.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据结构学习笔记(一)——《大话数据结构》

第一章 数据结构绪论 基本概念和术语 数据 描述客观事物的符号&#xff0c;计算机中可以操作的对象&#xff0c;能被计算机识别并输入给计算机处理的符号的集合。包括整型、实型等数值类型和字符、声音、图像、视频等非数值类型。 数据元素 组成数据的、有一定意义的基本单位&a…

6. Z 字形变换

6. Z 字形变换 将一个给定字符串 s 根据给定的行数 numRows &#xff0c;以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 “PAYPALISHIRING” 行数为 3 时&#xff0c;排列如下&#xff1a; P A H N A P L S I I G Y I R之后&#xff0c;你的输出需要从…

java的垃圾回收机制包括:主流回收算法和收集器(jvm的一个主要优化方向)

2019独角兽企业重金招聘Python工程师标准>>> java的垃圾回收机制是java语言的一大特色&#xff0c;解放了开发人员对内存的复杂控制&#xff0c;但如果你想要一个高级java开发人员&#xff0c;还是需要知道其机制&#xff0c;所谓不仅要会用还要知道其原理这样才能用…

北京dns服务器ip地址_什么是DNS? 域名系统,DNS服务器和IP地址概念介绍

北京dns服务器ip地址介绍 (Introduction) By the end of this article, you should have a better understanding of:在本文末尾&#xff0c;您应该对以下内容有更好的了解&#xff1a; What DNS is and what it does 什么是DNS及其作用 What DNS servers do DNS服务器做什么 …

767. 重构字符串

767. 重构字符串 给定一个字符串S&#xff0c;检查是否能重新排布其中的字母&#xff0c;使得两相邻的字符不同。 若可行&#xff0c;输出任意可行的结果。若不可行&#xff0c;返回空字符串。 示例 1: 输入: S “aab” 输出: “aba” 示例 2: 输入: S “aaab” 输出: “…

长生生物狂犬病疫苗造假

这两天暴发的长生生物狂犬病疫苗造假案真是很厉害&#xff0c;世人都说投资不过山海关还真有一定道理。 市场上长生生物的狂犬病疫苗约占1/4左右&#xff0c;是一个非常大的用量。 你别说&#xff0c;疫苗真的是非常适合造假&#xff1a; 1. 狂犬病有一定潜伏期&#xff0c;几天…

小程序 杂记

调试打印测试的方法&#xff1a; 方法1、显示提示框 &#xff08;微信自带的API&#xff09; wx.showToast({title: 成功,icon: success,duration: 2000 }) 方法2、js的console.log()方法 //test.js Page({onLoad: function(option){console.log(option.query)} }) wx.showToa…

使用fetch封装ajax_如何使用Fetch在JavaScript中进行AJAX调用

使用fetch封装ajaxI will be sharing bite sized learnings about JavaScript regularly in this series. Well cover JS fundamentals, browsers, DOM, system design, domain architecture and frameworks. 在本系列中&#xff0c;我将定期分享有关JavaScript的小知识。 我们…

RxJS笔记

RxJS 《深入浅出RxJS》读书笔记遗留问题 Observable的HOT与COLD对应的实际场景&#xff0c;以及在编码中的体现chapter1 html部分 测试你对时间的感觉按住我一秒钟然后松手你的时间&#xff1a;毫秒jquery实现 var time new Date().getTime(); $("#hold-me").moused…

滚动一定的高度底色递增

$(window).scroll(function() {var swipeHeight 200;//完全变色高度var scrollTop $(document).scrollTop();//页面滚动高度var x scrollTop/swipeHeight;$(".head-bg").css({"opacity":x}); }) 转载于:https://www.cnblogs.com/lhj-blog/p/8521525.htm…

@hot热加载修饰器导致static静态属性丢失(已解决)

react开发的时候&#xff0c;引入热加载&#xff0c;用了修饰器的引入方式&#xff0c;发现了一个很有意思的问题&#xff0c;网上并没有相关文章&#xff0c;所以抛出来探讨下。 一段很简单的测试代码。但是经过babel编码后&#xff0c;变得很有意思。假设编码成es2016&#x…

49. 字母异位词分组

49. 字母异位词分组 给你一个字符串数组&#xff0c;请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词&#xff0c;所有源单词中的字母都恰好只用一次。 示例 1: 输入: strs [“eat”, “tea”, “tan”…

python 入门程序_非Python程序员的Python速成课程-如何快速入门

python 入门程序This article is for people who already have experience in programming and want to learn Python quickly.本文适用于已经有编程经验并希望快速学习Python的人们。 I created this resource out of frustration when I couldnt find an online course or a…

cmd命令操作Oracle数据库

//注意cmd命令执行的密码字符不能过于复杂 不能带有特殊符号 以免执行不通过 譬如有&#xff01;#&#xffe5;%……&*之类的 所以在Oracle数据库设置密码是不要太复杂 /String Database "ORCL"; 不指向地址程序只能安装在数据库服务器上才能执行到命令String …

OpenCV学习(7.16)

写了个实现摄像头上画线并输出角度的东西……虽然很简单&#xff0c;但脑残的我还是debug了很长时间。 1 // 圆和直线.cpp : 定义控制台应用程序的入口点。2 //3 4 #include "stdafx.h"5 6 using namespace std;7 using namespace cv;8 9 void onMouse(int event, in…

学习vue.js的自我梳理笔记

基本语法格式&#xff1a; <script> new Vue({ el: #app, data: { url: http://www.runoob.com } }) </script> 指令 【指令是带有 v- 前缀的特殊属性。】 判断 <p v-if"seen">现在你看到我了</p> 参数 <a v-bind:href"url"&…

722. 删除注释

722. 删除注释 给一个 C 程序&#xff0c;删除程序中的注释。这个程序source是一个数组&#xff0c;其中source[i]表示第i行源码。 这表示每行源码由\n分隔。 在 C 中有两种注释风格&#xff0c;行内注释和块注释。 字符串// 表示行注释&#xff0c;表示//和其右侧的其余字符…

如何创建一个自记录的Makefile

My new favorite way to completely underuse a Makefile? Creating personalized, per-project repository workflow command aliases that you can check in.我最喜欢的完全没用Makefile的方法&#xff1f; 创建个性化的按项目存储库工作流命令别名&#xff0c;您可以检入。…

【BZOJ3262】陌上花开

CDQ分治模板 注意三元组完全相等的情况 1 #include<bits/stdc.h>2 using namespace std;3 const int N100010,K200010;4 int n,k,cnt[N],ans[N];5 struct Node{6 int a,b,c,id;7 bool operator<(const Node& k)const{8 if(bk.b&&ck.c) re…

Spring+jpaNo transactional EntityManager available

2019独角兽企业重金招聘Python工程师标准>>> TransactionRequiredException: No transactional EntityManager availableEntityManager执行以下方法(refresh, persist, flush, joinTransaction, remove, merge) 都需要需要事务if (transactionRequiringMethods.cont…