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.pswp.cn/news/390111.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390111.shtml
英文地址,請注明出處:http://en.pswp.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…