初入前端,虽然这方面技术还很菜,但是也已经自学了一段时间,以下做两个小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的三种基本布局方式:普通流,浮动,定位 这里面都已用到。
学无止境,这里有用的不当的或者是有更好的技术点欢迎大家指教。