Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

用 HTML5 + CSS3 打造精緻登入頁:不靠一張圖片

很多專案都缺一個簡潔又體面的登入頁。某天上班比較空閒,翻到一篇「使用 HTML5 與 CSS3 製作登入頁的完整步驟」,就動手做了一版,這裡把程式碼整理出來。因為個人認為 IE6 遲早要被淘汰,所以範例完全沒有理會它在 IE6 下的表現。

整個頁面的視覺效果—— layered 陰影、漸層背景、圓角輸入框、橙色漸層按鈕——全部由 CSS 完成,沒有用到任何一張圖。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="description" content="運用 CSS3 與 CSS 濾鏡做的精美登入界面,未使用任何圖片">
<title>登入</title>
<link rel="stylesheet" href="style/common/common.css">
<style type="text/css">
body { margin:0; padding:0; background-color:#E4E8EC; }

.wrap { margin:150px auto; width:380px; overflow:hidden; }

.loginForm {
    /* 多層陰影堆疊出「紙張」質感 */
    box-shadow: 0 0 2px rgba(0,0,0,0.2),
                0 1px 1px rgba(0,0,0,0.2),
                0 3px 0 #fff,
                0 4px 0 rgba(0,0,0,0.2),
                0 6px 0 #fff,
                0 7px 0 rgba(0,0,0,0.2);
    position:absolute;
    z-index:0;
    background-color:#FFF;
    border-radius:4px;
    height:250px;
    width:380px;
    background: -webkit-gradient(linear, left top, left 24, from(#EEE), color-stop(4%, #FFF), to(#EEE));
    background: -moz-linear-gradient(top, #EEE, #FFF 1px, #EEE 24px);
    background: -o-linear-gradient(top, #EEEEEE, #FFFFFF 1px, #EEEEEE 24px);
}

/* 虛線內框,營造雙層紙感 */
.loginForm:before {
    content:'';
    position:absolute;
    z-index:-1;
    border:1px dashed #CCC;
    top:5px; bottom:5px; left:5px; right:5px;
    box-shadow: 0 0 0 1px #FFF;
}

.loginForm h1 {
    text-shadow: 0 1px 0 rgba(255,255,255,.7), 0px 2px 0 rgba(0,0,0,.5);
    text-transform:uppercase;
    text-align:center;
    color:#666;
    line-height:3em;
    margin:16px 0 20px 0;
    letter-spacing:4px;
    font:normal 26px/1 "Microsoft YaHei", sans-serif;
}

fieldset { border:none; padding:10px 10px 0; }

fieldset input[type=text]     { background:url(style/default/images/user.png) 4px 5px no-repeat; }
fieldset input[type=password] { background:url(style/default/images/password.png) 4px 5px no-repeat; }

fieldset input[type=text], fieldset input[type=password] {
    width:300px;
    line-height:2em;
    font-size:12px;
    height:24px;
    border:none;
    padding:3px 4px 3px 2.2em;
}

.inputWrap {
    background: -webkit-gradient(linear, left top, left 24, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF));
    background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 24px);
    background: -o-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 24px);
    border-radius:3px;
    border:1px solid #CCC;
    margin:10px 10px 0;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#EEEEEE', endColorstr='#FFFFFF');
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#EEEEEE', endColorstr='#FFFFFF')";
}

fieldset input[type=submit] {
    text-align:center;
    padding:2px 20px;
    line-height:2em;
    border:1px solid #FF1500;
    border-radius:3px;
    background: -webkit-gradient(linear, left top, left 24, from(#FF6900), color-stop(0%, #FF9800), to(#FF6900));
    background: -moz-linear-gradient(top, #FF6900, #FF9800 0, #FF6900 24px);
    background: -o-linear-gradient(top, #FF6900, #FF9800 0, #FF6900 24px);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#FF9800', endColorstr='#FF6900');
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#FF9800', endColorstr='#FF6900')";
    height:30px;
    cursor:pointer;
    letter-spacing:4px;
    margin-left:10px;
    color:#FFF;
    font-weight:bold;
}
fieldset input[type=submit]:hover {
    background: -webkit-gradient(linear, left top, left 24, from(#FF9800), color-stop(0%, #FF6900), to(#FF9800));
    background: -moz-linear-gradient(top, #FF9800, #FF6900 0, #FF9800 24px);
}
fieldset a { color:blue; font-size:12px; margin:6px 0 0 10px; text-decoration:none; }
fieldset a:hover { text-decoration:underline; }
fieldset span { font-size:12px; }
</style>
<!-- 讓舊版 IE 認得 HTML5 元素 -->
<!--[if IE]>
<script type="text/javascript">
document.createElement("section");
document.createElement("header");
document.createElement("footer");
</script>
<![endif]-->
</head>

<body>
<div class="wrap">
  <form action="#" method="post">
    <section class="loginForm">
      <header><h1>登入</h1></header>
      <div class="loginForm_content">
        <fieldset>
          <div class="inputWrap">
            <input type="text" name="userName" placeholder="郵箱/會員帳號/手機號" autofocus required>
          </div>
          <div class="inputWrap">
            <input type="password" name="password" placeholder="請輸入密碼" required>
          </div>
        </fieldset>
        <fieldset>
          <input type="checkbox" checked="checked">
          <span>下次自動登入</span>
        </fieldset>
        <fieldset>
          <input type="submit" value="登入">
          <a href="#">忘記密碼?</a> <a href="#">註冊</a>
        </fieldset>
      </div>
    </section>
  </form>
</div>
</body>
</html>

值得學的幾個技巧

用 box-shadow 疊出層次。把六段陰影串在一起,白、灰交替往下偏移,卡片下方就出現了「一疊紙」的錯覺,完全不需要切圖。

用 :before 偽元素畫內框。content:'' 加上絕對定位與 z-index:-1,就能在表單後面墊一個虛線框,視覺層次更細緻。

placeholder 取代 label。HTML5 的 placeholder 直接把提示文字放進輸入框,搭配 autofocus 與 required,連驗證都交給瀏覽器處理,省下不少 JS。

漸層要寫多套。範例中同時保留了 -webkit-、-moz-、-o- 與 IE 的 filter 寫法,這是那個年代為了跨瀏覽器一致所付出的代價;今天若重做,標準的 linear-gradient 一行就能取代全部。

當然,有附圖會更直觀,但這個頁面的重點正是:所有質感都能用程式碼寫出來。

Copyright © Linux教程網 All Rights Reserved