CSS清除默认样式 H5端所有项目通用

将手机端设计稿的单位换算成rem,需要在文件中加入如下一段脚本:

<script type="text/javascript">
    /*  rem   尺寸换算  1rem=100px    */
    (function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if(clientWidth>=640){
                    docEl.style.fontSize = '100px';
                }else{
                    docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
                }
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
</script>

下面的代码在清除默认样式的同时,也解决了H5端的一些适配和样式兼容性问题,要将上面的JS代码与下面对应的CSS适配代码配合来使用(有些class类名是平时自己开发时的一些习惯):

/*---------全局重置开始----------*/
html {
    /* 让非ie浏览器默认也显示垂直滚动条,防止因滚动条引起的闪烁 */
    overflow-y: scroll;
    /*设置字体*/
    font-family: "PingFang", "Source Han Sans CN", "Microsoft YaHei", "Tahoma", "Helvetica", "Arial", "\5b8b\4f53", "sans-serif";
    /*webkit在渲染页面时,会自动调整字体大小,比如横竖屏切换时*/
    -webkit-text-size-adjust: 100%; 

}
html * {
    outline: 0;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    /*iOS会在元素周围显示橙色的外框,以表明该元素被tap了。如果你想自己实现tap时的响应效果,可以用以下方法“去除”这个高亮效果*/
}
body, ul, ol, li, dl, dt, dd, p, pre, h1, h2, h3, h4, h5, h6, hr, blockquote, fieldset, lengend,
button,
textarea,
th,
td {
    margin: 0;
    padding: 0;
}
article, aside, details, figcaption, figure, footer, header, hgroup,nav, section{ display:block;}
input, select, textarea {
    -webkit-appearance:none; /*去掉webkit默认的表单样式*/
}
/* 表单控件没有亮线 */
button, input, optgroup, select, textarea {
    margin: 0;
    outline: none;
    font: inherit;
    border: none;
}
/* 表格边框和元素间距清空 */
table {
    border-collapse: collapse;
    border-spacing: 0;
}
li {
    list-style: none;
}
a {
    text-decoration: none;
}
em {
    font-style: normal;
}
img {
    border: none;
    margin: 0;
    padding: 0;
    vertical-aline:middle;
}
/*-----全局重置结束-------*/
/*--------盒模型兼容开始-------*/
.flex {
    display: box;
    /*Android 4.4*/
    display: -webkit-box;
    /*iOS 6-, Safari 3.1-6*/
    display: -moz-box;
    /*Firefox 19*/
    display: -ms-flexbox;
    /*IE 10*/
    display: -webkit-flex;
    /*Chrome*/
    display: flex;
    /*Opera 12.1, Firefox 20+*/
}
/*主轴竖直*/
.flex_column {
    -webkit-box-orient: vertical;
    -webkit-flex-direction: column;
    -moz-flex-direction: column;
    -ms-flex-direction: column;
    -o-flex-direction: column;
    flex-direction: column;
}
.flex_wrap {
    -webkit-box-lines: multiple;
    -webkit-flex-direction: wrap;
    -moz-flex-direction: wrap;
    -ms-flex-direction: wrap;
    -o-flex-direction: wrap;
    flex-direction: wrap;
}
/*元素两端排列*/
.flex_jus_between {
    -webkit-box-pack: space-between;
    -webkit-justify-content: space-between;
    -moz-justify-content: space-between;
    -ms-justify-content: space-between;
    -o-justify-content: space-between;
    justify-content: space-between;
}
/*元素两端间距排列*/
.flex_jus_around {
    -webkit-box-pack: space-around;
    -webkit-justify-content: space-around;
    -moz-justify-content: space-around;
    -ms-justify-content: space-around;
    -o-justify-content: space-around;
    justify-content: space-around;
}
/*元素两端间距排列*/
.flex_jus_center {
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -moz-justify-content: center;
    -ms-justify-content: center;
    -o-justify-content: center;
    justify-content: center;
}
/*元素竖直方向居中*/
.flex-ali_i_center {
    -webkit-box-align: center;
    -webkit-align-items: center;
    -moz-align-items: center;
    -ms-align-items: center;
    -o-align-items: center;
    align-items: center;
}
/*多行元素竖直方向居中*/
.flex-ali_c_center {
    -webkit-box-align: center;
    -webkit-align-content: center;
    -moz-align-content: center;
    -ms-align-content: center;
    -o-align-content: center;
    align-content: center;
}
/*--------盒模型兼容结束-------*/
/*----------适配开始--------*/
@media screen and (max-width: 320px) {
    html {
        font-size: 90px;
    }
}
@media screen and (min-width: 320px) {
    html {
        font-size: 90px;
    }
}
@media screen and (min-width: 370px) {
    html {
        font-size: 100px;
    }
}
@media screen and (min-width: 410px) {
    html {
        font-size: 110px;
    }
}
@media screen and (min-width: 480px) {
    html {
        font-size: 130px;
    }
}
@media screen and (min-width: 560px) {
    html {
        font-size: 150px;
    }
}
@media screen and (min-width: 640px) {
    html {
        font-size: 170px;
    }
}
@media screen and (min-width: 760px) {
    html {
        font-size: 200px;
    }
}
/*----------适配结束----------*/
/*----------常用类名----------*/
.clearfix{*zoom:1;}
.clearfix:after{ content:""; display:block; clear:both;}
.pull-right {
  float: right;
}
.pull-left {
  float: left;
}
.hide {
  display: none;
}
.show {
  display: block;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值