超酷HTML 5和CSS3实现登录及注册功能表单

开发 前端
我们将会谈到登录和注册功能使用纯CSS3和HTML 5来实现。这里我们使用CSS3的伪类:target。我们使用CSS3和图标字体。

超酷HTML5和CSS3实现的登录及其注册功能表单

在线演示 本地下载

过去我们开发登录和注册功能大都使用javascript来实现,今天我们介绍的这个登录及其注册表单不走常人路,使用纯CSS3和HTML5来实现同样的功能。

这里我们使用CSS3的伪类:target。我们使用CSS3和图标字体。主要的想法是展示登录表单并且提供一个链接可以转向注册表单。

请大家注意这里只是一个简单的演示,不是所有的浏览器都支持:target,如果你需要在产品环境中使用,你需要使用对应的代码来处理对于老浏览器支持的fallback。

HTML

在html代码中,我们构建俩个表单并且把第二个表单隐藏。如下:

  1. <div id="container_demo" > 
  2.     <!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4  --> 
  3.     <a class="hiddenanchor" id="toregister"></a> 
  4.     <a class="hiddenanchor" id="tologin"></a> 
  5.     <div id="wrapper"> 
  6.         <div id="login" class="animate form"> 
  7.             <form  action="mysuperscript.php" autocomplete="on"> 
  8.                 <h1>Log in</h1> 
  9.                 <p> 
  10.                     <label for="username" class="uname" data-icon="u" > Your email or username </label> 
  11.                     <input id="username" name="username" required="required" type="text" placeholder="myusername or mymail@mail.com"/> 
  12.                 </p> 
  13.                 <p> 
  14.                     <label for="password" class="youpasswd" data-icon="p"> Your password </label> 
  15.                     <input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" /> 
  16.                 </p> 
  17.                 <p class="keeplogin"> 
  18.                     <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
  19.                     <label for="loginkeeping">Keep me logged in</label> 
  20.                 </p> 
  21.                 <p class="login button"> 
  22.                     <input type="submit" value="Login" /> 
  23.                 </p> 
  24.                 <p class="change_link"> 
  25.                     Not a member yet ?  
  26.                     <a href="#toregister" class="to_register">Join us</a> 
  27.                 </p> 
  28.             </form> 
  29.         </div> 
  30.    
  31.         <div id="register" class="animate form"> 
  32.             <form  action="mysuperscript.php" autocomplete="on"> 
  33.                 <h1> Sign up </h1> 
  34.                 <p> 
  35.                     <label for="usernamesignup" class="uname" data-icon="u">Your username</label> 
  36.                     <input id="usernamesignup" name="usernamesignup" required="required" type="text" placeholder="mysuperusername690" /> 
  37.                 </p> 
  38.                 <p> 
  39.                     <label for="emailsignup" class="youmail" data-icon="e" > Your email</label> 
  40.                     <input id="emailsignup" name="emailsignup" required="required" type="email" placeholder="mysupermail@mail.com"/> 
  41.                 </p> 
  42.                 <p> 
  43.                     <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label> 
  44.                     <input id="passwordsignup" name="passwordsignup" required="required" type="password" placeholder="eg. X8df!90EO"/> 
  45.                 </p> 
  46.                 <p> 
  47.                     <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label> 
  48.   <input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password" placeholder="eg. X8df!90EO"/> 
  49.                 </p> 
  50.                 <p class="signin button"> 
  51.                     <input type="submit" value="Sign up"/> 
  52.                 </p> 
  53.                 <p class="change_link"> 
  54.                     Already a member ?  
  55.                     <a href="#tologin" class="to_register"> Go and log in </a> 
  56.                 </p> 
  57.             </form> 
  58.         </div> 
  59.     </div> 
  60. </div>  

我 们在这里添加了HTML5相关元素,并且使用了一些新的输入控件。input=password自动隐藏用户输入。input=email使得浏览器检查 是否用户输入是正确的email。同时我们添加了require=required属性。支持这个属性的浏览器将不允许用户递交表单除非所有的输入区域都 是正确,大家可能注意到这里不需要使用javascript。autocomplete=on属性将会基于用户输入预先的填入内容。我们同时也可以使用一 些不错的placeholder来提示用户应该输入的内容。

接下来可能是俩个比较有趣的部分。你或许注意到了顶端的俩个<a href>链接。这是一个能够帮助我们的表单更加方便的使用anchor处理的小技巧,这样当我们点击切换链接并且触发:target的时候将不会跨越很长的页面。

第二个小技巧是使用icon字体。我们将会使用一个data-attribute来展示图标。通过使用对应的字符来设定data-icon="icon_charactoer",我们只需要一个CSS属性选择器来样式化所有的图标。如果你对这个有兴趣,可以阅读:24 Ways: Displaying Icons with Fonts and Data- Attributes

CSS

为了使得代码更加清晰,在教程里我们忽略了浏览器提供商指定的前缀, 当然你可以在对应代码中找到相关的设定。当然我们使用了很多不错的CSS3技巧可能不在所有的浏览器中生效。

俩个表单的样式

首先我们配置容器的样式:

  1. #subscribe,  
  2. #login{  
  3.     positionabsolute;  
  4.     top: 0px;  
  5.     width88%;  
  6.     padding18px 6% 60px 6%;  
  7.     margin0 0 35px 0;  
  8.     backgroundrgb(247247247);  
  9.     border1px solid rgba(147184189,0.8);  
  10.     box-shadow:  
  11.         0pt 2px 5px rgba(105108109,  0.7),  
  12.         0px 0px 8px 5px rgba(2082232260.4inset;  
  13.     border-radius: 5px;  
  14. }  
  15. #login{  
  16.     z-index22;  

这里我们添加了一个漂亮的盒式阴影(box shadow)来创建2个阴影:一个inset用来创建内部蓝色,还有一个外部阴影。这里我们稍微解释一下z-index。

在下面代码中,我们定义了标题的样式,使用了background-clip。

  1. /**** general text styling ****/ 
  2. #wrapper h1{  
  3.     font-size48px;  
  4.     colorrgb(6106117);  
  5.     padding2px 0 10px 0;  
  6.     font-family'FranchiseRegular','Arial Narrow',Arial,sans-serif;  
  7.     font-weightbold;  
  8.     text-aligncenter;  
  9.     padding-bottom30px;  
  10. }  
  11.    
  12. /** For the moment only webkit supports the background-clip:text; */ 
  13. #wrapper h1{  
  14.     background:  
  15.     -webkit-repeating-linear-gradient(-45deg,  
  16.         rgb(188393) ,  
  17.         rgb(18839320px,  
  18.         rgb(6411111820px,  
  19.         rgb(6411111840px,  
  20.         rgb(18839340px);  
  21.     -webkit-text-fill-colortransparent;  
  22.     -webkit-background-clip: text;  
  23. }  
  24.    
  25. #wrapper h1:after{  
  26.     content:' ';  
  27.     display:block;  
  28.     width:100%;  
  29.     height:2px;  
  30.     margin-top:10px;  
  31.     background:  
  32.         linear-gradient(left,  
  33.             rgba(147,184,189,00%,  
  34.             rgba(147,184,189,0.820%,  
  35.             rgba(147,184,189,153%,  
  36.             rgba(147,184,189,0.879%,  
  37.             rgba(147,184,189,0100%);  

大家注意目前只有webkit的浏览器支持background-clip,所以我们将只为webkit创建条纹式的背景,我们将这个特效添加到H1标题。 因为目前只能在webkit浏览器上生效,我们将使用webkit前缀。只使用-webkit-prefix是一个糟糕的习惯,这里只是为了演示,你不应 该在一个正式的网站中使用!-webkit-text-fill-color:trasparent帮助你生成一个透明的背景。当然其它浏览器都会忽略。

我们使用:after伪类来在标题下创建了一个淡出的直线效果。我们对直线两端使用2px的高度渐变并且淡出背景到0。

接下来我们使的输入更好漂亮。

  1. /**** advanced input styling ****/ 
  2. /* placeholder */ 
  3. ::-webkit-input-placeholder  {  
  4.     colorrgb(190188188);  
  5.     font-styleitalic;  
  6. }  
  7. input:-moz-placeholder,  
  8. textarea:-moz-placeholder{  
  9.     colorrgb(190188188);  
  10.     font-styleitalic;  
  11. }  
  12. input {  
  13.   outlinenone;  

首先我们定义了input样式并且删除了outline。注意outline帮助用户知道目前聚焦到那个输入项,如果你删除你应该提供:active和:focus。

  1. /* all the input except submit and checkbox */ 
  2. #wrapper input:not([type="checkbox"]){  
  3.     width92%;  
  4.     margin-top4px;  
  5.     padding10px 5px 10px 32px;  
  6.     border1px solid rgb(178178178);  
  7.     box-sizing : content-box;  
  8.     border-radius: 3px;  
  9.     box-shadow: 0px 1px 4px 0px rgba(1681681680.6inset;  
  10.     transition: all 0.2s linear;  
  11. }  
  12. #wrapper input:not([type="checkbox"]):active,  
  13. #wrapper input:not([type="checkbox"]):focus{  
  14.     border1px solid rgba(9190900.7);  
  15.     background: rgba(2382362400.2);  
  16.     box-shadow: 0px 1px 4px 0px rgba(1681681680.9inset;  

这里我们使用:not pseudo class,来定义所有input样式,除了checkbox。我提供了一个:focus和:active状态,因为我们要删除outline。

接 下来我们介绍icon字体部分。因为对于input来说我们没有办法添加:before和:class伪类,所以我们需要作弊一下:我们添加icon到 label,然后添加到input中。这里我们使用fontomas library的图标。 记得data-icon属性吧?在这里我们使用data-icon='u'代表用户,'e'代表电子邮件,'p'代表密码。一旦选择这些字母,我们下载字 体,并且使用fontsquirrel font generator来将他们生成@font-face兼容格式。

  1. @font-face {  
  2.     font-family'FontomasCustomRegular';  
  3.     srcurl('fonts/fontomas-webfont.eot');  
  4.     srcurl('fonts/fontomas-webfont.eot?#iefix'format('embedded-opentype'),  
  5.          url('fonts/fontomas-webfont.woff'format('woff'),  
  6.          url('fonts/fontomas-webfont.ttf'format('truetype'),  
  7.          url('fonts/fontomas-webfont.svg#FontomasCustomRegular'format('svg');  
  8.     font-weightnormal;  
  9.     font-stylenormal;  
  10. }  
  11.    
  12. /** the magic icon trick ! **/ 
  13. [data-icon]:after {  
  14.     contentattr(data-icon);  
  15.     font-family'FontomasCustomRegular';  
  16.     colorrgb(106159171);  
  17.     positionabsolute;  
  18.     left: 10px;  
  19.     top: 35px;  
  20.     width30px;  

没错,你不需要为每一个图标设置class。我们使用content:attr(data-icon)来取得字母,因此我们只需要申明字体,选择颜色并且定义位置。

接下来我们定义递交按钮:

  1. /*styling both submit buttons */ 
  2. #wrapper p.button input{  
  3.     width30%;  
  4.     cursorpointer;  
  5.     backgroundrgb(61157179);  
  6.     padding8px 5px;  
  7.     font-family'BebasNeueRegular','Arial Narrow',Arial,sans-serif;  
  8.     color#fff;  
  9.     font-size24px;  
  10.     border1px solid rgb(28108122);  
  11.     margin-bottom10px;  
  12.     text-shadow0 1px 1px rgba(0000.5);  
  13.     border-radius: 3px;  
  14.     box-shadow:  
  15.         0px 1px 6px 4px rgba(0000.07inset,  
  16.         0px 0px 0px 3px rgb(254254254),  
  17.         0px 5px 3px 3px rgb(210210210);  
  18.     transition: all 0.2s linear;  
  19. }  
  20. #wrapper p.button input:hover{  
  21.     backgroundrgb(74179198);  
  22. }  
  23. #wrapper p.button input:active,  
  24. #wrapper p.button input:focus{  
  25.     backgroundrgb(40137154);  
  26.     positionrelative;  
  27.     top: 1px;  
  28.     border1px solid rgb(127687);  
  29.     box-shadow: 0px 1px 6px 4px rgba(0000.2inset;  
  30. }  
  31. p.login.button,  
  32. p.signin.button{  
  33.     text-alignright;  
  34.     margin5px 0;  

这里我们主要使用box-shadow来创建多余的border。你可以使用一个边框,但是也可以创建更多。我们使用length数值来创建一个假的白色边框,3px宽,没有模糊。

开发完毕,希望大家喜欢这个教程,如果你想看到所有效果,使用Chrome来运行在线演示吧,谢谢大家支持!

原文链接:http://www.cnblogs.com/gbin1/archive/2012/04/09/2439806.html

【编辑推荐】

  1. 8个非常有用的HTML 5工具你值得拥有
  2. 6个优秀的HTML 5/CSS3演示PPT框架推荐
  3. 解决跨平台问题的终极密钥就是HTML 5?
  4. HTML 5开发:地理位置定位指南
  5. 一句代码实现HTML 5淘宝语音搜索
责任编辑:彭凡 来源: 博客园
相关推荐

2011-07-15 09:10:44

HTML 5CSS3

2012-02-29 09:27:36

ibmdw

2013-08-21 13:19:33

HTML5CSS3表单设计

2011-03-22 08:54:02

HTML 5CSS3JavaScript

2012-05-11 09:37:34

HTML5

2010-03-22 08:56:12

2012-05-24 11:03:55

HTML5

2014-07-14 12:37:36

jQueryCSS3

2011-11-25 13:18:40

HTML 5

2012-05-30 09:22:56

Hybrid App助HTML5JavaScript

2011-08-30 16:39:34

HTML 5

2011-11-17 09:24:27

HTML 5

2013-07-09 09:24:29

响应式HTML5CSS3

2011-11-18 13:25:48

HTML 5

2012-05-25 10:31:44

HTML5

2011-06-17 08:54:38

HTML 5CSS3

2011-09-21 11:02:17

HTML 5

2011-01-25 09:16:33

HTML 5CSS3Web

2011-05-11 16:13:43

CSS3

2012-06-07 09:23:45

ibmdw
点赞
收藏

51CTO技术栈公众号