1. 程式人生 > >使用HTML 5和CSS3製作登入頁面完整步驟

使用HTML 5和CSS3製作登入頁面完整步驟

本文詳細介紹使用HTML 5 和CSS3 製作一個登入頁面的完整過程。

login.html

  1. <formid="login">
  2. <h1>Log In</h1>
  3. <fieldsetid="inputs">
  4. <inputid="username"type="text"placeholder="Username" autofocus required>
  5. <inputid="password"type="password"placeholder="Password" required>
  6. </fieldset>
  7. <fieldset
    id="actions">
  8. <inputtype="submit"id="submit"value="Log in">
  9. <ahref="">Forgot your password?</a><ahref="">Register</a>
  10. </fieldset>
  11. </form>

所用到的HTML 5的特性:

◆ placeholder – 輸入框的簡短提示,當該輸入框獲得輸入焦點時,該提示資訊自動消失

◆ required – 指定該輸入元素是否必須提供

◆ autofocus – 指定輸入框是否在頁面載入完畢自動獲取輸入焦點

◆ type=”password” – 指定密碼輸入(非HTML5專有)

CSS

在這裡我們用到了 CSS3 的一些專有屬性,包括:

Box-shadow 可以幫我們製作效果很好的邊框陰影

  1. #login  
  2. {  
  3.     box-shadow:  
  4.           0 0 2px rgba(0, 0, 0, 0.2),  
  5.           0 1px 1px rgba(0, 0, 0, .2),  
  6.           0 3px 0 #fff,  
  7.           0 4px 0 rgba(0, 0, 0, .2),  
  8.           0 6px 0 #fff,  
  9.           0 7px 0 rgba(0, 0, 0, .2);  

Stitch effect (縫效果)

  1. #login  
  2. {  
  3.     position: absolute;  
  4.     z-index: 0;  
  5. }  
  6. #login:before  
  7. {  
  8.     content: '';  
  9.     position: absolute;  
  10.     z-index: -1;  
  11.     border: 1px dashed #ccc;  
  12.     top: 5px;  
  13.     bottom: 5px;  
  14.     left: 5px;  
  15.     right: 5px;  
  16.     -moz-box-shadow: 0 0 0 1px #fff;  
  17.     -webkit-box-shadow: 0 0 0 1px #fff;  
  18.     box-shadow: 0 0 0 1px #fff;  

Subtle gradient lines (微妙的漸變線)

  1. h1  
  2. {  
  3.     text-shadow: 0 1px 0 rgba(255, 255, 255, .7), 0px 2px 0 rgba(0, 0, 0, .5);  
  4.     text-transform: uppercase;  
  5.     text-align: center;  
  6.     color: #666;  
  7.     margin: 0 0 30px 0;  
  8.     letter-spacing: 4px;  
  9.     font: normal 26px/1 Verdana, Helvetica;  
  10.     position: relative;  
  11. }  
  12. h1:after, h1:before  
  13. {  
  14.     background-color: #777;  
  15.     content: "";  
  16.     height: 1px;  
  17.     position: absolute;  
  18.     top: 15px;  
  19.     width: 120px;  
  20. }  
  21. h1:after  
  22. {  
  23.     background-image: -webkit-gradient(linear, left top, right top, from(#777), to(#fff));  
  24.     background-image: -webkit-linear-gradient(left, #777, #fff);  
  25.     background-image: -moz-linear-gradient(left, #777, #fff);  
  26.     background-image: -ms-linear-gradient(left, #777, #fff);  
  27.     background-image: -o-linear-gradient(left, #777, #fff);  
  28.     background-image: linear-gradient(left, #777, #fff);  
  29.     right: 0;  
  30. }  
  31. h1:before  
  32. {  
  33.     background-image: -webkit-gradient(linear, right top, left top, from(#777), to(#fff));  
  34.     background-image: -webkit-linear-gradient(right, #777, #fff);  
  35.     background-image: -moz-linear-gradient(right, #777, #fff);  
  36.     background-image: -ms-linear-gradient(right, #777, #fff);  
  37.     background-image: -o-linear-gradient(right, #777, #fff);  
  38.     background-image: linear-gradient(right, #777, #fff);  
  39.     left: 0;  

最終結果

結論

在一些老的瀏覽器上也表現不錯,下圖是在IE8下的效果:

<!DOCTYPE html>

<html>
<head>
<title>Create a nice login form using CSS3 and HTML5</title>
<style>

html, body
{
    height: 100%;
}

body
{
    font: 12px 'Lucida Sans Unicode', 'Trebuchet MS', Arial, Helvetica;    
    margin: 0;
    background-color: #d9dee2;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebeef2), to(#d9dee2));
    background-image: -webkit-linear-gradient(top, #ebeef2, #d9dee2);
    background-image: -moz-linear-gradient(top, #ebeef2, #d9dee2);
    background-image: -ms-linear-gradient(top, #ebeef2, #d9dee2);
    background-image: -o-linear-gradient(top, #ebeef2, #d9dee2);
    background-image: linear-gradient(top, #ebeef2, #d9dee2);    
}

/*--------------------*/

#login
{
    background-color: #fff;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee));
    background-image: -webkit-linear-gradient(top, #fff, #eee);
    background-image: -moz-linear-gradient(top, #fff, #eee);
    background-image: -ms-linear-gradient(top, #fff, #eee);
    background-image: -o-linear-gradient(top, #fff, #eee);
    background-image: linear-gradient(top, #fff, #eee);  
    height: 240px;
    width: 400px;
    margin: -150px 0 0 -230px;
    padding: 30px;
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 0;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;  
    -webkit-box-shadow:
          0 0 2px rgba(0, 0, 0, 0.2),
          0 1px 1px rgba(0, 0, 0, .2),
          0 3px 0 #fff,
          0 4px 0 rgba(0, 0, 0, .2),
          0 6px 0 #fff,  
          0 7px 0 rgba(0, 0, 0, .2);
    -moz-box-shadow:
          0 0 2px rgba(0, 0, 0, 0.2),  
          1px 1px   0 rgba(0,   0,   0,   .1),
          3px 3px   0 rgba(255, 255, 255, 1),
          4px 4px   0 rgba(0,   0,   0,   .1),
          6px 6px   0 rgba(255, 255, 255, 1),  
          7px 7px   0 rgba(0,   0,   0,   .1);
    box-shadow:
          0 0 2px rgba(0, 0, 0, 0.2),  
          0 1px 1px rgba(0, 0, 0, .2),
          0 3px 0 #fff,
          0 4px 0 rgba(0, 0, 0, .2),
          0 6px 0 #fff,  
          0 7px 0 rgba(0, 0, 0, .2);
}

#login:before
{
    content: '';
    position: absolute;
    z-index: -1;
    border: 1px dashed #ccc;
    top: 5px;
    bottom: 5px;
    left: 5px;
    right: 5px;
    -moz-box-shadow: 0 0 0 1px #fff;
    -webkit-box-shadow: 0 0 0 1px #fff;
    box-shadow: 0 0 0 1px #fff;
}

/*--------------------*/

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;
    margin: 0 0 30px 0;
    letter-spacing: 4px;
    font: normal 26px/1 Verdana, Helvetica;
    position: relative;
}

h1:after, h1:before
{
    background-color: #777;
    content: "";
    height: 1px;
    position: absolute;
    top: 15px;
    width: 120px;   
}

h1:after
{ 
    background-image: -webkit-gradient(linear, left top, right top, from(#777), to(#fff));
    background-image: -webkit-linear-gradient(left, #777, #fff);
    background-image: -moz-linear-gradient(left, #777, #fff);
    background-image: -ms-linear-gradient(left, #777, #fff);
    background-image: -o-linear-gradient(left, #777, #fff);
    background-image: linear-gradient(left, #777, #fff);      
    right: 0;
}

h1:before
{
    background-image: -webkit-gradient(linear, right top, left top, from(#777), to(#fff));
    background-image: -webkit-linear-gradient(right, #777, #fff);
    background-image: -moz-linear-gradient(right, #777, #fff);
    background-image: -ms-linear-gradient(right, #777, #fff);
    background-image: -o-linear-gradient(right, #777, #fff);
    background-image: linear-gradient(right, #777, #fff);
    left: 0;
}

/*--------------------*/

fieldset
{
    border: 0;
    padding: 0;
    margin: 0;
}

/*--------------------*/

#inputs input
{
    background: #f1f1f1 url(http://www.red-team-design.com/wp-content/uploads/2011/09/login-sprite.png) no-repeat;
    padding: 15px 15px 15px 30px;
    margin: 0 0 10px 0;
    width: 353px; /* 353 + 2 + 45 = 400 */
    border: 1px solid #ccc;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-shadow: 0 1px 1px #ccc inset, 0 1px 0 #fff;
    -webkit-box-shadow: 0 1px 1px #ccc inset, 0 1px 0 #fff;
    box-shadow: 0 1px 1px #ccc inset, 0 1px 0 #fff;
}

#username
{
    background-position: 5px -2px !important;
}

#password
{
    background-position: 5px -52px !important;
}

#inputs input:focus
{
    background-color: #fff;
    border-color: #e8c291;
    outline: none;
    -moz-box-shadow: 0 0 0 1px #e8c291 inset;
    -webkit-box-shadow: 0 0 0 1px #e8c291 inset;
    box-shadow: 0 0 0 1px #e8c291 inset;
}

/*--------------------*/
#actions
{
    margin: 25px 0 0 0;
}

#submit
{		
    background-color: #ffb94b;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#fddb6f), to(#ffb94b));
    background-image: -webkit-linear-gradient(top, #fddb6f, #ffb94b);
    background-image: -moz-linear-gradient(top, #fddb6f, #ffb94b);
    background-image: -ms-linear-gradient(top, #fddb6f, #ffb94b);
    background-image: -o-linear-gradient(top, #fddb6f, #ffb94b);
    background-image: linear-gradient(top, #fddb6f, #ffb94b);
    
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    
    text-shadow: 0 1px 0 rgba(255,255,255,0.5);
    
     -moz-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
     -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
     box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;    
    
    border-width: 1px;
    border-style: solid;
    border-color: #d69e31 #e3a037 #d5982d #e3a037;

    float: left;
    height: 35px;
    padding: 0;
    width: 120px;
    cursor: pointer;
    font: bold 15px Arial, Helvetica;
    color: #8f5a0a;
}

#submit:hover,#submit:focus
{		
    background-color: #fddb6f;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ffb94b), to(#fddb6f));
    background-image: -webkit-linear-gradient(top, #ffb94b, #fddb6f);
    background-image: -moz-linear-gradient(top, #ffb94b, #fddb6f);
    background-image: -ms-linear-gradient(top, #ffb94b, #fddb6f);
    background-image: -o-linear-gradient(top, #ffb94b, #fddb6f);
    background-image: linear-gradient(top, #ffb94b, #fddb6f);
}	

#submit:active
{		
    outline: none;
   
     -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;
     -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;
     box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;		
}

#submit::-moz-focus-inner
{
  border: none;
}

#actions a
{
    color: #3151A2;    
    float: right;
    line-height: 35px;
    margin-left: 10px;
}

/*--------------------*/

#back
{
    display: block;
    text-align: center;
    position: relative;
    top: 60px;
    color: #999;
}


</style>
</head>

<body>

<form id="login">
    <h1>Log In</h1>
    <fieldset id="inputs">
        <input id="username" type="text" placeholder="Username" autofocus required>   
        <input id="password" type="password" placeholder="Password" required>
    </fieldset>
    <fieldset id="actions">
        <input type="submit" id="submit" value="Log in">
        <a href="">Forgot your password?</a><a href="">Register</a>
    </fieldset>
    <a href="http://www.red-team-design.com/slick-login-form-with-html5-css3" id="back">Back to article...</a>
</form>

<!-- BSA AdPacks code -->
<script src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script>
(function(){
  var bsa = document.createElement('script');
     bsa.async = true;
     bsa.src = 'http://www.red-team-design.com/js/adpacks-demo.js';
  (document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(bsa);
})();
</script>

</body>
</html>