初入前端,雖然這方面技術還很菜,但是也已經自學了一段時間,以下做兩個小demo來檢驗自己的學習成效。
(以HTML和CSS爲主)
win10計算器
(由於我技術水平有限,這裏暫時還不會行列布局隨着長度的改變而改變)
首先看看樣本效果(電腦自帶的計算器效果):
瞭解目標頁面後,開始進行頁面佈局。
首先,整個頁面可以看成一個div,然後裏面又分成三個大板塊:顯示屏,小按鍵以及鍵盤。
下面其是HTML佈局的代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>calculator</title>
<link rel="stylesheet" href="calculator.css">
</head>
<body>
<div class="calculator">
<div class="screen"><span>0</span></div>
<div class="M">
<ul>
<li class="m">MC</li>
<li class="m">MR</li>
<li class="c1 m1">M+</li>
<li class="c1 m1">M-</li>
<li class="c1 m1">MS</li>
</ul>
</div>
<div class="row u1">
<ul>
<li class="c1">%</li>
<li class="c1">x<sup>1/2</sup></li>
<li class="c1">x<sup>2</sup></li>
<li class="c1">1/x</li>
</ul>
</div>
<div class="row u1">
<ul>
<li class="c1">CE</li>
<li class="c1">C</li>
<li class="c1">DEL</li>
<li class="c1">/</li>
</ul>
</div>
<div class="row">
<ul>
<li class="c1 u2">7</li>
<li class="c1 u2">8</li>
<li class="c1 u2">9</li>
<li class="c1 u1">*</li>
</ul>
</div>
<div class="row">
<ul>
<li class="c1 u2">4</li>
<li class="c1 u2">5</li>
<li class="c1 u2">6</li>
<li class="c1 u1">-</li>
</ul>
</div>
<div class="row">
<ul>
<li class="c1 u2">1</li>
<li class="c1 u2">2</li>
<li class="c1 u2">3</li>
<li class="c1 u1">+</li>
</ul>
</div>
<div class="row">
<ul>
<li class="c1 u1">+/_</li>
<li class="c1 u2">0</li>
<li class="c1 u1">.</li>
<li class="c1 u1">=</li>
</ul>
</div>
</div>
</body>
</html>
注:在鍵盤佈局時這裏使用的是ul+li,使用多個div也是ok的。
下面是其CSS代碼:
* {
margin: 0;
padding: 0;
}
.calculator {
width: 400px;
height: 100%;
background-color: rgb(241, 241, 241);
overflow: hidden;
margin: 0 auto;
border-top-left-radius: 15%;
border-top-right-radius: 15%;
}
.screen {
height: 100px;
font-size: 50px;
line-height: 100px;
color: #000;
margin-top: 50px;
font-weight: 600;
}
.screen span {
float: right;
}
.M {
margin-top: 20px;
height: 30px;
}
.M li {
width: 65px;
line-height: 30px;
list-style: none;
float: left;
text-align: center;
font-size: 10px;
}
.m {
color: rgb(192, 192, 192);
}
.m1 {
font-weight: 600;
}
.c1:hover {
background-color: rgb(214, 212, 212);
}
.row {
height: 80px;
}
.row li {
width: 100px;
height: 80px;
line-height: 70px;
list-style: none;
float: left;
text-align: center;
border: 2px solid rgb(241, 241, 241);
box-sizing: border-box;
}
.u1 {
font-size: 20px;
background-color: rgb(245, 245, 245);
}
.u2 {
font-size: 25px;
font-weight: 700;
background-color: rgb(255, 255, 255);
}
這裏也有4個需要注意的地方:
- 使用右浮動使得0顯示在顯示屏右側
- 每一行的行內佈局都是採用左浮動
- 鍵盤間的邊框顯示以及鍵盤採用CSS3的盒子模型
- 添加上鼠標的hover效果
代碼的顯示效果如下:(手段增加的圓邊效果)
Google頁面
首先看看樣本效果:
佈局分析:兩部分,上半logo和下半輸入框。
考慮到位置問題,這裏這兩個部分我都採用絕對定位進行佈局。
logo部分可以嘗試直接用span進行拼接,但是由於這個字體以及邊距太。。。所以我這裏和原著一樣也直接圖片代替。
輸入框部分整個可以看成一個大的div,然後裏面又三個小部分:兩邊和中間的input。
下面其是HTML佈局的代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Google</title>
<link rel="stylesheet" href="Google.css">
</head>
<body>
<div class="logo">
</div>
<div class="box">
<div class="box-icon"></div>
<input type="text" class="box-text" value="在 Google 上搜索,或者輸入一個網址"
onfocus="if(value=='在 Google 上搜索,或者輸入一個網址') {value=''}"
onblur="if (value=='') {value='在 Google 上搜索,或者輸入一個網址'}">
<button class="box-microphone" title="語音搜索"></button>
</div>
</body>
</html>
注意兩個地方:
- 兩邊的兩個圖案我是用字體圖標來實現的
- input裏面的js事件是爲了實現鼠標點擊輸入框能夠瞬間清空輸入框的效果
下面是其CSS代碼:
@font-face {
font-family: "icomoon";
src: url('fonts/icomoon.eot');
src: url('fonts/icomoon.woff') format('woff'),
url('fonts/icomoon.ttf') format('truetype'),
url('fonts/icomoon.svg') format('svg');
}
* {
box-sizing: border-box;
}
.logo {
height: 92px;
width: 272px;
background-image: url("google.png");
background-repeat: no-repeat;
background-size: 100%;
position: absolute;
left: 50%;
top: 20%;
margin-left: -136px;
margin-top: 15px;
}
.box {
height: 44px;
width: 584px;
background: white;
box-shadow: 0 1px 6px 0 rgba(32, 33, 36, .28);
border-radius: 22px;
font-size: 16px;
line-height: 21px;
position: absolute;
left: 50%;
top: 40%;
margin-left: -292px;
margin-top: -10px;
}
.box-icon {
font-family: "icomoon";
color: rgb(117, 117, 117);
bottom: 0;
height: 24px;
margin: auto 0;
margin-left: 16px;
position: absolute;
top: 0;
width: 24px;
}
.box-text {
width: 500px;
height: 44px;
position: absolute;
left: 40px;
line-height: 44px;
color: rgb(117, 117, 117);
font-size: 16px;
border: none;
outline: none;
overflow: hidden;
}
.box-microphone {
font-family: "icomoon";
font-size: 18px;
color: yellowgreen;
margin-right: 16px;
background-color: white;
border: none;
cursor: pointer;
bottom: 0;
position: absolute;
right: 0;
top: 0;
}
注意幾個地方:
- 想要使用字體圖標除了這裏CSS裏面引用的部分外還需要在icon裏面下載一些東西
- 這裏採用的也是CSS3的盒子模型
代碼的顯示效果如下:
以上就是兩個小demo的全部內容,CSS的三種基本佈局方式:普通流,浮動,定位 這裏面都已用到。
學無止境,這裏有用的不當的或者是有更好的技術點歡迎大家指教。