1.平时写代码遵守的编码规范
- tab 用两个空格表示
- css的 :后加个空格, {前加个空格
- 每条声明后都加上分号
- 换行,而不是放到一行
- 颜色用小写,用缩写, #fff
- 小数不用写前缀, 0.5s -> .5s;0不用加单位
- 尽量缩写, margin: 5px 10px 5px 10px -> margin: 5px 10px
参考代码规范连接:http://codeguide.bootcss.com/
https://google.github.io/styleguide/htmlcssguide.xml
2.垂直居中有几种实现方式,给出代码范例
- 绝对定位实现居中
<head>
<title></title>
<style type="text/css">
.ct {
width:400px;
height: 300px;
position: relative;
margin: 0 auto;
background-color: #eee;
}
.box {
width: 100px;
height: 100px;
position: absolute;
background-color: red;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="ct">
<div class="box"></div>
</div>
</body>
- vertical-align实现居中
<head>
<title></title>
<style type="text/css">
.ct {
width:400px;
height: 300px;
margin: 0 auto;
background-color: #eee;
text-align: center;
}
.ct::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="ct">
<div class="box"></div>
</div>
</body>
- table-cell实现居中
<head>
<title></title>
<style type="text/css">
.ct {
width:400px;
height: 300px;
background-color: #eee;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class="ct">
<div class="box"></div>
</div>
</body>