. 黑白圖像
這段代碼會讓你的彩色照片顯示為黑白照片,是不是很酷?
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); }
2. 使用 :not() 在菜單上應用/取消應用邊框
先給每一個菜單項添加邊框
/* add border */ .nav li { border-right: 1px solid #666; }
然后再除去最后一個元素
// remove border / .nav li:last-child { border-right: none; }
可以直接使用 :not() 偽類來應用元素:
.nav li:not(:last-child) { border-right: 1px solid #666; }
這樣代碼就干凈,易讀,易于理解了。
當然,如果你的新元素有兄弟元素的話,也可以使用通用的兄弟選擇符(~):
.nav li:first-child ~ li { border-left: 1px solid #666; }
3. 頁面頂部陰影
下面這個簡單的 CSS3 代碼片段可以給網頁加上漂亮的頂部陰影效果:
body:before { content: ""; position: fixed; top: -10px; left: 0; width: 100%; height: 10px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8); -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8); box-shadow: 0px 0px 10px rgba(0,0,0,.8); z-index: 100; }
4. 給 body 添加行高
你不需要分別添加 line-height 到每個p,h標記等。只要添加到 body 即可:
body { line-height: 1; }
這樣文本元素就可以很容易地從 body 繼承。
5. 所有一切都垂直居中
要將所有元素垂直居中,太簡單了:
html, body { height: 100%; margin: 0; } body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex; }
看,是不是很簡單。
注意:在IE11中要小心flexbox
6. 逗號分隔的列表
讓HTML列表項看上去像一個真正的,用逗號分隔的列表:
ul > li:not(:last-child)::after { content: ","; }
對最后一個列表項使用 :not() 偽類。
7. 使用負的 nth-child 選擇項目
在CSS中使用負的 nth-child 選擇項目1到項目n。
li { display: none; } /* select items 1 through 3 and display them */ li:nth-child(-n+3) { display: block; }
8. 對圖標使用 SVG
我們沒有理由不對圖標使用SVG:
.logo { background: url("logo.svg"); }
SVG對所有的分辨率類型都具有良好的擴展性,并支持所有瀏覽器都回歸到IE9。這樣可以避開.png、.jpg或.gif文件了。
9. 優化顯示文本
有時,字體并不能在所有設備上都達到最佳的顯示,所以可以讓設備瀏覽器來幫助你:
html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
注:請負責任地使用 optimizeLegibility。此外,IE /Edge沒有 text-rendering 支持。
10. 對純 CSS 滑塊使用 max-height
使用 max-height 和溢出隱藏來實現只有CSS的滑塊:
.slider ul { max-height: 0; overlow: hidden; } .slider:hover ul { max-height: 1000px; transition: .3s ease; }
11. 繼承 box-sizing
讓 box-sizing 繼承 html:
html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; }
這樣在插件或杠桿其他行為的其他組件中就能更容易地改變 box-sizing 了。
12. 表格單元格等寬
表格工作起來很麻煩,所以務必盡量使用 table-layout: fixed 來保持單元格的等寬:
.calendar { table-layout: fixed; }
13. 用 Flexbox 擺脫外邊距的各種 hack
當需要用到列分隔符時,通過flexbox的 space-between 屬性,你就可以擺脫nth-,first-,和 last-child 的hack了:
.list { display: flex; justify-content: space-between; } .list .person { flex-basis: 23%; }
現在,列表分隔符就會在均勻間隔的位置出現。
14. 使用屬性選擇器用于空鏈接
當a元素沒有文本值,但 href 屬性有鏈接的時候顯示鏈接:
a[href^="http"]:empty::before { content: attr(href); }
相當方便。
15. 檢測鼠標雙擊
HTML:
<div class="test3"> <span><input type="text" value=" " readonly="true" /> <a >Double click me</a></span> </div>
CSS:
.test3 span { position: relative; } .test3 span a { position: relative; z-index: 2; } .test3 span a:hover, .test3 span a:active { z-index: 4; } .test3 span input { background: transparent; border: 0; cursor: pointer; position: absolute; top: -1px; left: 0; width: 101%; /* Hacky */ height: 301%; /* Hacky */ z-index: 3; } .test3 span input:focus { background: transparent; border: 0; z-index: 1; }
16. CSS 寫出三角形
/* create an arrow that points up */ div.arrow-up { width:0px; height:0px; border-left:5px solid transparent; /* left arrow slant */ border-right:5px solid transparent; /* right arrow slant */ border-bottom:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px; } /* create an arrow that points down */ div.arrow-down { width:0px; height:0px; border-left:5px solid transparent; border-right:5px solid transparent; border-top:5px solid #2f2f2f; font-size:0px; line-height:0px; } /* create an arrow that points left */ div.arrow-left { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-right:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px; } /* create an arrow that points right */ div.arrow-right { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-left:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px; }
17. CSS3 calc() 的使用
calc() 用法類似于函數,能夠給元素設置動態的值:
/* basic calc */ .simpleBlock { width: calc(100% - 100px); } /* calc in calc */ .complexBlock { width: calc(100% - 50% / 3); padding: 5px calc(3% - 2px); margin-left: calc(10% + 10px); }
18. 文本變漸
文本漸變效果很流行,使用 CSS3 能夠很簡單就實現:
h2[data-text] { position: relative; } h2[data-text]::after { content: attr(data-text); z-index: 10; color: #e3e3e3; position: absolute; top: 0; left: 0; -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));}
19. 禁用鼠標事件
CSS3 新增的 pointer-events 讓你能夠禁用元素的鼠標事件,例如,一個連接如果設置了下面的樣式就無法點擊了。
.disabled { pointer-events: none; }
20. 模糊文本
簡單但很漂亮的文本模糊效果,簡單又好看!
家好,今天我們聊一下現代應用中常見的一種交互驗證方式:滑塊驗證。滑塊驗證也被稱為拼圖驗證碼,是一種用于驗證用戶是否為人類而不是機器人的常見方法。用戶需要完成驗證后才能繼續往下操作,而機器人通常很難模擬這種人類行為。也因為這樣,滑塊驗證已經成為了網站注冊、登錄、商品防爬以及其他交互過程中的一種重要安全措施。
滑塊驗證的核心思路是要求用戶完成一個任務來驗證其非機器人的身份。大多數應用通常包括將一個滑塊拖動到正確的位置,使其與一個缺口圖像對齊。用戶需要通過鼠標或手指觸摸滑動來完成。在這個過程中,應用會檢測用戶的行為數據,并且判斷是否是機器人操作。
準備兩張圖片,一張是背景圖,一張是從背景圖上截的一塊:
下面我們實現一個簡單的滑塊驗證示例,通過簡單的HTML、CSS和JavaScript來實現一個完整的拼圖驗證碼。
創建HTML結構,包括一個背景圖和一個滑塊,以及一個包含缺口的小圖塊。代碼非常簡單:
<div class="slider-container">
<div class="slider-background">
<div class="gap-fixed"></div> <!-- 固定缺口位置 -->
<div class="slider">
<div class="gap"></div> <!-- 缺口圖放置在滑塊內部 -->
</div>
<div class="slider-trabecula"></div>
</div>
</div>
我們需要為HTML元素添加樣式,包括背景圖、滑塊、缺口等。樣式可以通過CSS來定義,以下是一些基本樣式的示例:
/* 樣式 */
.slider-container {
width: 500px; /* 背景圖寬度 */
height: 300px; /* 背景圖高度 */
margin: 20px auto;
position: relative;
}
.slider-background {
width: 100%;
background: url('https://files.mdnice.com/user/1107/739c3c4b-0082-4719-a5fd-f17506e9ea01.png'); /* 背景圖URL */
background-size: 100% 100%; /* 背景圖鋪滿 */
border-radius: 5px;
height: 300px;
position: relative;
overflow-x: hidden;
}
.slider {
width: 80px; /* 滑塊寬度 */
height: 50px;
background-color: #3498db;
border-radius: 5px;
position: absolute;
cursor: pointer;
user-select: none; /* 禁止文本選中 */
bottom: 10px; /* 滑塊位于底部 */
z-index: 2;
transition: transform 0.2s ease; /* 添加平滑動畫效果 */
}
.slider-trabecula {
bottom: 10px;
width: 100%;
height: 50px;
position: absolute;
z-index: 1;
background: rgb(204 204 204 / 50%);
}
.gap {
width: 75px; /* 缺口圖的寬度 */
height: 50px; /* 缺口圖的高度 */
background-image: url('https://files.mdnice.com/user/1107/b835a241-5a0d-40ac-9b95-23f7e283dc03.png'); /* 缺口圖的URL */
background-size: 100% 100%;
position: absolute;
top: -112px;
box-shadow: 0px 0px 5px 1px #ccc;
}
.gap-fixed {
width: 75px; /* 缺口圖的寬度 */
height: 50px; /* 缺口圖的高度 */
position: absolute;
bottom: 122px;
left: 358px;
box-shadow: 0px 0px 5px 1px #ff0000;
}
最后一步也是最重要的部分JS交互。我們需要處理滑塊的拖動、驗證邏輯以及用戶與滑塊的交互。這里展示核心部分:
// 模擬橫軸滑動位置
const mockPositionX=360;
// JavaScript
const slider=document.querySelector('.slider');
let isDragging=false;
let startPositionX=0;
slider.addEventListener('mousedown', (e)=> {
isDragging=true;
startPositionX=e.clientX;
});
document.addEventListener('mousemove', (e)=> {
if (!isDragging) return;
let offsetX=e.clientX - startPositionX;
slider.style.transform=`translateX(${offsetX}px)`;
});
document.addEventListener('mouseup', ()=> {
if (!isDragging) return;
const transformX=Math.abs(parseInt(slider.style.transform.slice(11)));
if (transformX < mockPositionX + 10 && transformX > mockPositionX - 10) {
// 驗證通過
document.querySelector('.slider-trabecula').style.background='rgb(12 160 18 / 50%)';
// 關閉滑塊
// 此處可以觸發后續操作,如登錄、列表加載等
} else {
// 驗證失敗
document.querySelector('.slider-trabecula').style.background='rgb(249 2 2 / 50%)';
setTimeout(()=> {
document.querySelector('.slider-trabecula').style.background='rgb(204 204 204 / 50%)';
slider.style.transform='translateX(0)';
}, 1500);
}
isDragging=false;
});
這樣我們就完成了一個簡單的滑塊驗證碼了,來看下效果:
當然在實際應用中,我們可能需要添加更復雜的驗證邏輯,以確保安全性。業內有一些常用的方案和最佳實踐,以下是一些業內常用的方案,我在后續的文章中詳細討論,可以關注我及時了解后續。
為了增加滑塊驗證的難度,滑塊的位置和缺口的位置應該是隨機的,不能是固定的。用戶在每次驗證時都需要滑動不同位置的滑塊,進而防止攻擊者根據固定的位置進行攻擊。
有些滑塊驗證方案會在缺口圖像上添加旋轉變換,使缺口不僅需要水平滑動,還需要旋轉以匹配背景圖。這增加了攻擊的難度,因為攻擊者需要模擬用戶旋轉滑塊。
監視用戶的行為模式,例如鼠標移動、點擊速度和拖動路徑等。通過分析這些行為可以更好地區分人類用戶和機器人。例如,機器人通常會以直線移動滑塊,而用戶的移動路徑可能會更加曲線和隨機。
這個很重要了,前端進行驗證永遠是不可靠的,需要在后端進行二次驗證。前端驗證可以提供用戶友好的反饋,但最終的驗證應該由后端來完成,防止攻擊者繞過前端驗證,采用后端驗證可以確保只有經過驗證的請求才能被處理。
允許用戶有一定的容錯空間,即使滑塊沒有完美對齊,也可以通過驗證。這可以減少誤報。容錯機制可以通過在驗證時允許一定范圍的誤差來實現,可參考示例代碼,容錯10像素 transformX < mockPositionX + 10 && transformX > mockPositionX - 10。
定期監控滑塊驗證的性能和安全性,根據具體情況進行調整。分析攻擊常用的模式并采取相應的反制措施。持續的監控可以幫助我們及時發現和應對新的威脅。
這些方案可以根據具體的需求和威脅模型進行定制。滑塊驗證作為一種反機器人的工具,也會不斷發展和演進,以適應不斷變化的威脅。希望本文提供的思路和實現步驟能夠幫助大家更好地理解和應用滑塊驗證技術,喜歡點個關注吧。
.使用CSS復位
CSS復位可以在不同的瀏覽器上保持一致的樣式風格。您可以使用CSS reset 庫Normalize等,也可以使用一個更簡化的復位方法:
*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0;}
現在元素的 margin 和padding 已為0,box-sizing可以管理您的CSS盒模型布局。
注意:如果你遵循接下來繼承 box-sizing講解的這個技巧, 你不需要在以上代碼中添加 box-sizing 屬性。
2.繼承 box-sizing
從 html 元素繼承box-sizing :
html { box-sizing: border-box;} *,*::before,*::after { box-sizing: inherit;}
如此在插件或其它組件里改變 box-sizing 變得簡單。
3.使用unset而不是重置所有屬性
重置元素的屬性時,不需要重置每個單獨的屬性:
button { background: none; border: none; color: inherit; font: inherit; outline: none; padding: 0;}
你可以用all簡寫來指定所有元素的屬性。 將該值設置為unset會將元素的屬性更改為其初始值:
button { all: unset;}
注意: 所有速記在IE11中不被支持,目前正在考慮Edge的支持。 IE11不支持unset。
4.使用 :not() 選擇器來決定表單是否顯示邊框
先為元素添加邊框
/* 添加邊框 */.nav li { border-right: 1px solid #666;}
為最后一個元素去除邊框
/* 去掉邊框 */.nav li:last-child { border-right:none;}
不過不要這么做,使用 :not() 偽類來達到同樣的效果:
.nav li:not(:last-child) { border-right: 1px solid #666;}
當然,你也可以使用 .nav li + li,但是 :not() 更加清晰,具有可讀性。
5.為 body 元素添加行高
不必為每一個 <p>,<h*>元素逐一添加 line-height,直接添加到 body 元素:
body { line-height: 1.5;}
文本元素可以很容易地繼承 body 的樣式。
6.為表單元素設置:focus
有視力的鍵盤用戶依靠焦點來確定鍵盤事件在頁面中的位置。 使表單元素的焦點脫穎而出,然后與瀏覽器的默認實現保持一致:
a:focus,button:focus,input:focus,select:focus,textarea:focus { box-shadow: none; outline: #000 dotted 2px; outline-offset: .05em;}
7.垂直居中任何元素
不!這絕不是黑魔法,真的可以垂直居中任何元素:
html,body { height: 100%; margin: 0;} body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex;}
...還有CSS Grid:
body { display: grid; height: 100vh; margin: 0; place-items: center center;}
這還不夠?垂直方向,水平方向?任何元素,任何時間,任何地點?CSS-Tricks 有篇好文講到了各種居中的技巧。
注意: IE11 對 flexbox 的支持有點 bug。
8.逗號分隔列表
使列表的每項都由逗號分隔:
ul > li:not(:last-child)::after { content: ",";}
因最后一項不加逗號,可以使用 :not() 偽類。
注意: 這一技巧對于無障礙,特別是屏幕閱讀器而言并不理想。而且復制粘貼并不會帶走CSS生成的內容,需要注意。
9.使用負的 nth-child 來選擇元素
使用負的 nth-child 可以選擇1 至 n 個元素。
li { display: none;} /* 選擇第 1 至第 3 個元素并顯示出來 */li:nth-child(-n+3) { display: block;}
或許你已經掌握了如何使用 :not()這個技巧,試下這個:
/* 選擇除前3個之外的所有項目,并顯示它們 */li:not(:nth-child(-n+3)) { display: none;}
如此簡單!
10.使用 SVG 圖標
沒有理由不使用 SVG 圖標:
.logo { background: url("logo.svg");}
SVG 在所有分辨率下都可以良好縮放,并且支持所有 IE9 以后的瀏覽器,丟掉你的 .png, .jpg, 或 .gif-jif-whatev 文件吧。
注意: 針對僅有圖標的按鈕,如果 SVG 沒有加載成功的話,以下樣式對無障礙有所幫助:
.no-svg .icon-only::after { content: attr(aria-label);}
11.使用 “形似貓頭鷹” 的選擇器
這個名字可能比較陌生,不過通用選擇器 (*) 和 相鄰兄弟選擇器 (+) 一起使用,效果非凡:
* + * { margin-top: 1.5em;}
在此示例中,文檔流中的所有的相鄰兄弟元素將都將設置 margin-top: 1.5em的樣式。
更多 “形似貓頭鷹” 的選擇器,可參考 A List Apart 上面 Heydon Pickering 的文章
12.使用 max-height 來建立純 CSS 的滑塊
max-height 與overflow hidden 一起來建立純 CSS 的滑塊:
.slider { max-height: 200px; overflow-y: hidden; width: 300px;} .slider:hover { max-height: 600px; overflow-y: scroll;}
鼠標移入滑塊元素時增大它的 max-height 值,便可以顯示溢出部分。
13.創造格子等寬的表格
table-layout: fixed 可以讓每個格子保持等寬:
.calendar { table-layout: fixed;}
無痛的 table 布局。
14.利用 Flexbox 去除多余的外邊距
與其使用 nth-, first-,和 last-child 去除列之間多余的間隙,不如使用 flexbox 的 space-between 屬性:
.list { display: flex; justify-content: space-between;} .list .person { flex-basis: 23%;}
列之間的間隙總是均勻相等。
15.利用屬性選擇器來選擇空鏈接
當 <a> 元素沒有文本內容,但有 href 屬性的時候,顯示它的 href 屬性:
a[href^="http"]:empty::before { content: attr(href);}
相當簡便。
16.給 “默認” 鏈接定義樣式
給 “默認” 鏈接定義樣式:
a[href]:not([class]) { color: #008000; text-decoration: underline;}
通過 CMS 系統插入的鏈接,通常沒有class 屬性,以上樣式可以甄別它們,而且不會影響其它樣式。
17.一致垂直節奏
通用選擇器 (*) 跟元素一起使用,可以保持一致的垂直節奏:
.intro > * { margin-bottom: 1.25rem;}
一致的垂直節奏可以提供視覺美感,增強內容的可讀性。
18.固定比例盒子
要創建具有固定比例的一個盒子,所有你需要做的就是給 div 設置一個 padding:.container { height: 0; padding-bottom: 20%; position: relative;} .container div { border: 2px dashed #ddd; height: 100%; left: 0; position: absolute; top: 0; width: 100%;}
使用20%的padding-bottom使得框等于其寬度的20%的高度。與視口寬度無關,子元素的div將保持其寬高比(100%/ 20%=5:1)。
19.為破碎圖象定義樣式
只要一點CSS就可以美化破碎的圖象:
img { display: block; font-family: sans-serif; font-weight: 300; height: auto; line-height: 2; position: relative; text-align: center; width: 100%;}
以添加偽元素的法則來顯示用戶信息和URL的引用:
img::before { content: "We're sorry, the image below is broken :("; display: block; margin-bottom: 10px;} img::after { content: "(url: " attr(src) ")"; display: block; font-size: 12px;}
了解更多關于這類樣式的技巧 Ire Aderinokun的 原帖.
20.用 rem 來調整全局大小;用 em 來調整局部大小
在根元素設置基本字體大小后 (html { font-size: 100%; }), 使用 em 設置文本元素的字體大小:
h2 { font-size: 2em;} p { font-size: 1em;}
然后設置模塊的字體大小為 rem:
article { font-size: 1.25rem;} aside .module { font-size: .9rem;}
現在,每個模塊變得獨立,更容易、靈活的樣式便于維護。
21.隱藏沒有靜音、自動播放的影片
這是一個自定義用戶樣式表的不錯的技巧。避免在加載頁面時自動播放。如果沒有靜音,則不顯示視頻:
video[autoplay]:not([muted]) { display: none;}
再次,我們利用了 :not() 的優點。
22.使用選擇器:root來控制字體彈性
在響應式布局中,字體大小應需要根據不同的視口進行調整。你可以計算字體大小根據視口高度的字體大小和寬度,這時需要用到:root:
:root { font-size: calc(1vw + 1vh + .5vmin);}
現在,您可以使用 root em
body { font: 1rem/1.6 sans-serif;}
23.為更好的移動體驗,為表單元素設置字體大小
當觸發<select>的下拉列表時,為了避免表單元素在移動瀏覽器(IOS Safari 等等)上的縮放,加上font-size:
input[type="text"],input[type="number"],select,textarea { font-size: 16px;}:dancer:
24.使用指針事件來控制鼠標事件
指針事件允許您指定鼠標如何與其觸摸的元素進行交互。 要禁用按鈕上的默認指針事件,例如:
.button-disabled { opacity: .5; pointer-events: none;}
就這么簡單。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。