每個專案開頭幾乎都會寫一次的重置樣式與工具類別,整理成一份可直接貼用的基礎樣式:
body, div, ul, li, h1, h2, h3, h4, h5, h6, p, dl, dt, dd,
ol, form, input, textarea, th, td, select {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
table {
border-collapse: collapse;
}
body {
font-family: "Microsoft YaHei";
}
ul, li {
list-style: none;
}
a {
text-decoration: none;
color: #232323;
}
input, textarea {
outline: none;
box-shadow: none;
}
textarea {
resize: none;
overflow: auto;
}
/* 清除浮動 */
.clearfix {
zoom: 1;
}
.clearfix:after {
content: ".";
width: 0;
height: 0;
visibility: hidden;
display: block;
clear: both;
}
/* 浮動 */
.fl { float: left; }
.fr { float: right; }
/* 文字對齊 */
.tl { text-align: left; }
.tc { text-align: center; }
.tr { text-align: right; }
/* 單行文字省略 */
.ellipse {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
幾個使用提醒:*{ box-sizing: border-box } 用萬用選擇器會略微影響效能,若在意可改為只套用在主要容器上;.clearfix 中的 zoom:1 是為了相容舊版 IE 觸發 hasLayout;而 input 上去掉 outline 雖然視覺乾淨,但會移除鍵盤操作的焦點提示,若在意無障礙,建議改用 :focus-visible 保留可見的焦點樣式。