在編寫不少時候頁面的時候咱們都須要佈局居中對齊的,這裏引用一個小案例講解一下水平+垂直居中對齊
html代碼:javascript
<body> <div class="main"> <div class="input-username div-input"> <div class="icon icon-uname"></div> <input type="text" name="username" placeholder="USERNAME"> </div> <div class="input-passowd div-input"> <div class="icon icon-pwd"></div> <input type="password" name="password" placeholder="PASSWORD"> </div> <div class="input-verfiy-code div-input"> <div class="icon icon-vcode"></div> <input type="text" name="verify" placeholder="VERIFYCODE"> <img src="index.php?&a=verifyCode" onclick="this.src='index.php?&a=verifyCode&rd='+Math.random();" class="verify-code"> </div> <div class="input-submit">提交登陸</div> </div> <script> window.onload = function(){ // do some thing..... }; </script> </body>
css樣式php
<style> body{ background:#2d3238; margin:0; padding:0; } .main{ position:absolute; left: 50%; top:50%; height:285px; width:283px; /*偏移距離爲容器的一半*/ margin-top:-142px; margin-left:-140px; } .div-input{ height:42px; margin:10px 0; background:#3c4147; } .input-submit{ height: 45px; line-height: 45px; margin-top: 18px; background: #ea4c89; text-align: center; font-weight: 700; color: #fff; font-size: 16px; cursor:pointer; } .icon{ width:45px; display: inline-block; height: 100%; float:left; } .icon-uname{ background: url(images/uname.svg) no-repeat center; } .icon-pwd{ background: url(images/pwd.svg) no-repeat center; } .icon-vcode{ background: url(images/vcode.svg) no-repeat center; } /*清理input瀏覽器默認樣式*/ input[type=text]{ appearance: none; -moz-appearance: none; -webkit-appearance: none; border-radius: 0; -moz-border-radius: 0; -webkit-border-radius: 0; } /*從新設置元素樣式*/ input{ outline: none; outline-color: transparent; outline-style: none; background: none; border: none; height: 100%; margin: 0px; padding: 0; display: inline-block; color: #fff; font-size: 14px; } </style>
在前臺佈局和使用javascript的時候咱們要學會本身動手一步步的來,有不懂的地方第一時間是參考官方資料和手冊(W3C,MDN),這樣才能學到東西,也是最高效的學習方法,基礎知識很重要,即便新技術再怎麼變,只要理解了底層的原理就不用太擔憂,有時間多看別人的代碼,學習和借鑑,把書本和別人的知識轉變爲本身的纔是關鍵css