:placeholder-shown另外,劃重點,這個僞類是仍處於實驗室的方案。也就是未歸入標準,固然咱們的目的是探尋有意思的 CSS 。當 input 類型標籤使用了 placeholder 屬性有了默認佔位的文字,會觸發此僞類樣式。配合 :not()僞類,能夠再改變當默認文字消失後的樣式,再配合本文的主角,咱們能夠實現表單的一系列效果。
.g-container { width: 500px; height: 60px; input { height: 100%; width: 100%; &:not(:placeholder-shown) { ... } &:placeholder-shown { ... } } &:focus-within { ... } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .g-container { position: relative; margin: 100px auto; display: flex; flex-wrap: wrap; width: 500px; height: 60px; overflow: hidden; transition: 0.3s; } .g-container > * { border: none; outline: none; } .g-container .g_input_search { padding: 0 15px; height: 100%; width: 100%; border: 1px solid #ddd; font-size: 18px; box-sizing: border-box; } /*.g-container .g_input_search:not(:placeholder-shown) {*/ /*border: 1px solid #03a9f4;*/ /*}*/ .g-container .g_input_search:not(:placeholder-shown) + .g_button_search { opacity: 1; } .g-container .g_input_search:placeholder-shown + .g_button_search { opacity: 0; } .g-container .g_input_search:not(:placeholder-shown){ border: 10px solid #03a9f4; font-size: 38px; color: #03a9f4; } .g-container .g_button_search { background: #03a9f4; color: #feffd4; font-size: 15px; cursor: pointer; width: 100px; height: calc(100% - 20px); transition: 0.3s; position: absolute; right: 10px; top: 10px; } .g-container:focus-within { -webkit-transform: translateY(-4px); transform: translateY(-4px); border-color: #bbb; box-shadow: 4px 4px 10px 2px #ddd; } </style> </head> <body> <div class="g-container"> <input type="text" placeholder="輸入你想查詢的內容" class="g_input_search" > <button type="button" class="g_button_search">GO</button> </div> </body> </html>