css小技巧搜集

1.移動端字體大小適配

利用媒體查詢器設(shè)置根元素的字體大小,并在開發(fā)時使用rem作為單位

@media screen and (min-width: 320px) {
    html {font-size: 14px;}
}
@media screen and (min-width: 360px) {
    html {font-size: 16px;}
}
@media screen and (min-width: 400px) {
    html {font-size: 18px;}
}
@media screen and (min-width: 440px) {
    html {font-size: 20px;}
}
@media screen and (min-width: 480px) {
    html {font-size: 22px;}
}
@media screen and (min-width: 640px) {
    html {font-size: 28px;}
}

2.sticky footer

footer始終在頁面的最下方:內(nèi)容不足時,footer在頁面的底部,內(nèi)容超過一頁時,footer在頁面所有內(nèi)容的下方。
主要實現(xiàn):三個容器:

  1. main-wrap:設(shè)置高度min-height:100%
  2. content:設(shè)置高度height:100% padding-bootom:100px,padding值等于footer高度,相當(dāng)于給footer 騰出 一個位置
  3. footer:設(shè)置高度height:100px; margin-top:100px; footer的實際位置是在main-wrap下面。當(dāng)內(nèi)容不足一頁時,它在頁面下方是看不見的,但是使用一個負(fù)邊距使其進入可視區(qū)域。這個負(fù)邊距正好等于content的padding-bottom,所以不會擋住content中的內(nèi)容。內(nèi)容滿了一頁時,自動content撐大main-wrap,將footer向下頂。
/*css*/
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}
.main-wrap {
    min-height: 100%;
    width: 100%;
    height: auto!important;
}
.content {
    width: 100%;
    padding-bottom: 100px;/*等于footer高度*/
}
.footer {
    height: 100px;
    margin-top: -100px;/*等于高度*/
    background-color: yellow;
    clear: both;
}
<!--結(jié)構(gòu)-->
<div class="main-wrap clearfix">
    <div class="content">
        <!--內(nèi)容-->
    </div>
</div>
<div class="footer">
    <!--頁尾始終在頁面最下方-->
</div>

3.fixed header &footer 固定頂部&底部

在最外層容器中,使用三個部分,header、content、footer,由于都是body的直接子元素,可以使用絕對定位,然后設(shè)置content的top bottom值,使其等于header footer的高度,并同時設(shè)置overflow:scroll,這樣頁面就可以正常的滾動。此辦法可以解決Safari中喚起鍵盤時,fixed定位的footer錯位的問題。

/*css*/
* {
    margin: 0;
    padding: 0;
}
.main-wrap {
    position: absolute;
    width: 100%;
    top: 50px;/*等于header高度*/
    bottom: 100px;/*等于footer高度*/
    overflow: scroll;
    background-color: #00bbff;
}
.header,.footer{
    position: absolute;
    left: 0;
    right: 0;
    background-color: yellow;
    text-align: center;
    font-size: 3em;
}
.header {
    height: 50px;/*等于main-wrap的top值*/
    top: 0;
}
.footer {
    height: 100px;/*等于main-wrap的bottom值*/
    bottom: 0;
}
<!--結(jié)構(gòu)-->
<body>
    <div class="header">
        <p>header</p>
    </div>
    <div class="main-wrap clearfix">
        <div class="content">
            <!--內(nèi)容-->
        </div>
    </div>
    <div class="footer">
        <p>footer</p>
    </div>
</body>

4.clearfix 偽元素清除浮動

.clearfix:after { 
    content:"\200B"; /*`\200B`是一個零寬度空格*/
    display:block; 
    height:0; 
    clear:both; 
} 
.clearfix {*zoom:1;}/*IE/7/6*/

5.ios下頁面滑動卡頓

當(dāng)ios上滾動某個元素發(fā)生卡頓時,為需要滾動的容器添加如下代碼:

.container {
    -webkit-overflow-scrolling: touch;
}

6.1px 邊框?qū)崿F(xiàn)

由于各廠商的屏幕物理像素與邏輯像素的不同,1px的邊框在某些屏幕上顯得很粗。使用媒體查詢器,條件為當(dāng)前屏幕的像素密度min-device-pixel-ratio,將其縮小。同時邊框使用偽元素實現(xiàn)。

.border-1px{
    position: relative
}
.border-1px:after{
    content: '';
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    border-top: 1px solid black;
}
@media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5){
    .border-1px:after{
        -webkit-transform :scaleY(0.7);
        transform :scaleY(0.7);
    }
}
@media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2){
    .border-1px:after{
        -webkit-transform :scaleY(0.5);
        transform :scaleY(0.5);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補...
    _Yfling閱讀 14,154評論 1 92
  • 選擇qi:是表達式 標(biāo)簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    love2013閱讀 2,438評論 0 11
  • 選擇qi:是表達式 標(biāo)簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    wzhiq896閱讀 2,119評論 0 2
  • 一、HTML5 1.1 認(rèn)識HTML5 HTML5并不僅僅只是作為HTML標(biāo)記語言的一個最新版本,更重要的是它制定...
    福爾摩雞閱讀 16,547評論 14 51
  • 田野里氤氳著熱氣,炙陽安穩(wěn)地俯瞰大地。 果實在滋滋地響,向日葵慵懶地倚在風(fēng)里,梵高在田野里來回走著,他時不時看看前...
    梵花云上閱讀 352評論 0 0

友情鏈接更多精彩內(nèi)容