CSS零碎知識整理

布局

1. 三列布局,左右側(cè)欄固定300px,三列高度統(tǒng)一給定,中間自適應(yīng)

此簡單問題,答上三種及格,答上五種優(yōu)秀,分別為:

  1. float
  2. position: absolute
  3. tablecell
  4. flexbox
  5. grid

重點:

答案:

<!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>水平布局</title>
    <style media="screen">
        html * {
            padding: 0;
            margin: 0;
        }
        .layout {
            margin-top: 10px;
        }
        .layout article div {
            min-height: 100px;
        }
    </style>
</head>
<body>
    <section class="layout float">
        <style>
            .layout.float .left{
                float: left;
                width: 300px;
                background: lightcoral;
            }
            .layout.float .right{
                float: right;
                width: 300px;
                background: aquamarine;
            }
            .layout.float .center{
                background: antiquewhite;
                overflow: hidden;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮動解決</h1>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
                <p>1. 三欄布局中間部分</p>
            </div>
        </article>
    </section>
    <section class="layout absolute">
        <style>
            .left-right-center {
                height: 100px;
            }
            .layout.absolute .left-right-center>div {
                position: absolute;
            }
            .layout.absolute .left {
                left: 0;
                width: 300px;
                background: lightcoral;
            }
            .layout.absolute .right {
                right: 0;
                width: 300px;
                background: aquamarine;
            }
            .layout.absolute .center {
                left: 300px;
                right: 300px;
                background: yellow;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>絕對布局</h1>
                1. 三欄布局中間部分
                2. 三欄布局中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout flexbox">
        <style>
            .layout.flexbox .left-right-center {
                display: flex;
            }
            .layout.flexbox .left {
                width: 300px;
                background: lightcoral;
            }
            .layout.flexbox .right {
                width: 300px;
                background: aquamarine;
            }
            .layout.flexbox .center {
                background: navajowhite;
                flex-grow: 1;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>flexbox</h1>
                1. 三欄布局中間部分
                2. 三欄布局中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout table">
        <style>
            .layout.table .left-right-center {
                width: 100%;
                display: table;
                height: 100px;
            }
            .layout.table .left-right-center>div{
                display: table-cell;
            }
            .layout.table .left {
                width: 300px;
                background: lightcoral;
            }
            .layout.table .right {
                width: 300px;
                background: aquamarine;
            }
            .layout.table .center {
                background: lightsalmon;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>table-cell</h1>
                1. 三欄布局中間部分
                2. 三欄布局中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>
    <section class="layout grid">
        <style>
            .layout.grid .left-right-center {
                display: grid;
                width: 100%;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }
            .layout.grid .left {
                background: lightcoral;
            }
            .layout.grid .right {
                background: aquamarine;
            }
            .layout.grid .center {
                background: orange;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="center">
                <h1>grid</h1>
                1. 三欄布局中間部分
                2. 三欄布局中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>
</body>
</html>

ps: tablecell也可以實現(xiàn)帶多行文本的div垂直居中。

.parent {
     display: table;
     width: 300px;
     height: 300px;
     text-align: center;
}
.son {
     display: table-cell;
     height: 200px;
     background-color: yellow;
     vertical-align: middle;
 }
image

2. 三列布局,上下固定高度,中間自適應(yīng)(移動端常見)

目前實現(xiàn)了三種,分別為:

  1. position:absolute
  2. flexbox
  3. grid (稍微做了下變形)

答案:

<!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>垂直布局</title>
    <style>
        html * {
            padding: 0;
            margin: 0;
        }
        .layout .top-center-bottom {
            height: 100vh;
        }
        .layout {
            margin-top: 20px;
        }
        .layout article div {
            width: 100%;
        }
    </style>
</head>
<body>
    <section class="layout absolute">
        <style>
            .layout.absolute .top {
                height: 100px;
                background: lightcoral;
                position: absolute;
                top: 20px;
            }
            .layout.absolute .bottom {
                height: 150px;
                background: aquamarine;
                position: absolute;
                bottom: -20px;
            }
            .layout.absolute .center {
                height: 100%;
                background: antiquewhite;
                padding: 100px 0 150px;
                box-sizing: border-box;
            }
        </style>
        <article class="top-center-bottom">
            <div class="top"></div>
            <div class="center">
                <h1>絕對定位</h1>
                <p>1. 中間填充的文字</p>
                <p>1. 中間填充的文字</p>
            </div>
            <div class="bottom"></div>
        </article>
    </section>
    <section class="layout flex">
        <style>
            .top-center-bottom {
                display: flex;
                flex-direction: column;

            }
            .layout.flex .top {
                height: 100px;
                background: lightcoral;                    
            }
            .layout.flex .bottom {
                height: 150px;
                background: aquamarine;
            }
            .layout.flex .center {
                background: antiquewhite;
                flex-grow: 1;
            }
        </style>
        <article class="top-center-bottom">
            <div class="top"></div>
            <div class="center">
                <h1>flexbox</h1>
                <p>1. 中間填充的文字</p>
                <p>1. 中間填充的文字</p>
            </div>
            <div class="bottom"></div>
        </article>
    </section>
    <section class="layout grid">
        <style>
            .layout.grid .top-center-bottom {
                display: grid;
                grid-template-rows: 100px auto 150px;
                grid-template-columns: 1400px;
                grid-auto-columns: auto;
            }
            .layout.grid .top {
                grid-area: 1/1/2/3;
                background: lightcoral;                    
            }
            .layout.grid .bottom {
                background: aquamarine;
                grid-area: 3/1/4/3;
            }
            .layout.grid .center {
                grid-area: 2/1/3/2;
                background: antiquewhite;
                justify-self: center;
            }
        </style>
        <article class="top-center-bottom">
            <div class="top"></div>
            <div class="center">
                <h1>grid</h1>
                <p>1. 中間填充的文字</p>
                <p>1. 中間填充的文字</p>
            </div>
            <div class="bottom"></div>
        </article>
    </section>
</body>
</html>

3. 用flex和grid繪制帶有hover高亮邊框效果的九宮格

九宮格
  <style>
    .line {
      display: flex;
    }

    .gezi {
      width: 100px;
      height: 100px;
      /* position: relative; */
      border: 5px solid #ccc;
      display: inline-block;
      line-height: 100px;
      text-align: center;
      box-sizing: border-box;
    }

    .gezi+.gezi {
      /* border-left: 0px; */
      margin-left: -5px;
    }

    .line+.line .gezi {
      margin-top: -5px;
    }

    .gezi:hover {
      z-index: 1;
      border-color: crimson;
    }

  </style>
  <div class="line">
    <div class="gezi">1</div>
    <div class="gezi">2</div>
    <div class="gezi">3</div>
  </div>
  <div class="line">
    <div class="gezi">4</div>
    <div class="gezi">5</div>
    <div class="gezi">6</div>
  </div>
  <div class="line">
    <div class="gezi">7</div>
    <div class="gezi">8</div>
    <div class="gezi">9</div>
  </div>

  <style>
    .jiu {
      display:grid;
      width: 300px;
      height: 300px;
      grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
      margin-left: 0;
      margin-top: 20px;
      padding: 0;
    }
    .jiu .box {
      list-style-type:none;
      border: 5px solid #ccc;
      margin-top: -5px;
      margin-left: -5px;
      line-height: 90px;
      text-align: center;
    }
    .jiu .box:hover {
      z-index: 1;
      border-color: crimson;
    }
  </style>
  <ul class="jiu">
    <li class="box">1</li>
    <li class="box">2</li>
    <li class="box">3</li>
    <li class="box">4</li>
    <li class="box">5</li>
    <li class="box">6</li>
    <li class="box">7</li>
    <li class="box">8</li>
    <li class="box">9</li>
  </ul>

盒模型

  1. 標(biāo)準(zhǔn)模型寬高不計算padding和border;IE模型寬高計算padding和border。
    box-sizing : content-box(標(biāo)準(zhǔn)模型-默認(rèn))/border-box(IE模型)
  2. JS獲取寬高
    dom.style.width 只能取內(nèi)聯(lián)寬高.
    window.getComputedStyle(dom).width 瀏覽器渲染之后的取值,兼容性更好. IE使用dom.currentStyle.width .
    dom.getBoundingClientRect().width/height/left/top/bottom/right ,返回所在ViewPort左頂點的絕對位置,常用于計算位置.

BFC (塊級格式化上下文)

  1. BFC布局規(guī)則:(引用自 - 關(guān)于CSS-BFC深入理解 )
    1.內(nèi)部的Box會在垂直方向,一個接一個地放置。
    2.Box垂直方向的距離由margin決定。屬于同一個BFC的兩個相鄰Box的margin會發(fā)生重疊
    3.每個元素的margin box的左邊, 與包含塊border box的左邊相接觸(對于從左往右的格式化,否則相反)。即使存在浮動也是如此。
    4.BFC的區(qū)域不會與float box重疊。
    5.BFC就是頁面上的一個隔離的獨立容器,容器里面的子元素不會影響到外面的元素。反之如此。
    6.計算BFC的高度時,float元素也參與計算

  2. 創(chuàng)建BFC
    1.position 為 absolute 或 fixed (position不為static或relative)
    2.浮動元素 (float不為none)
    3.displayinline-block | table | flex | grid
    4.overflow不為visible
    5.<html>標(biāo)簽

零碎小技巧

  1. 妙用background:background: url(...) no-repeat center 0;保持圖片在父容器大小變化時,時刻保持水平居中。
  2. 清除浮動時,mdn上最新推薦的clearfix寫法:
/* new clearfix */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
    overflow: hidden;
    visibility: hidden;
}

動畫

CSS3
SVG
Convas

多行文本省略號

.element {
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;  // 截斷在第二行
    -webkit-box-orient: vertical; // 方向垂直
    // height: 50px;
}

字體

查看網(wǎng)站引用字體 開發(fā)者工具-Application-Frames-Fonts
.woff格式
字體文件
自定義字體
字體圖標(biāo)

最后編輯于
?著作權(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)容

  • 本章節(jié)是前端開發(fā)者面試問題 - CSS 部分的參考答案。 歡迎提出 PR 進(jìn)行建議和指正! CSS 選擇器的優(yōu)先級...
    程序猿天璇閱讀 1,091評論 0 14
  • 盒模型(標(biāo)準(zhǔn)盒模型,ie盒模型) 盒子由四個部分(或稱區(qū)域)組成,每個盒子有四個邊界:內(nèi)容邊界 Content e...
    冫笙閱讀 233評論 0 0
  • 作者:北風(fēng)菌 鏈接:https://juejin.im/post/6876623829574090760 來源:掘...
    碼匠編程閱讀 517評論 0 0
  • 所有題目答案整理自網(wǎng)絡(luò),如有錯誤,接受指正,拒絕批評! 關(guān)于html5 HTML5的十大新特性 語義化標(biāo)簽使得頁面...
    黃金原野閱讀 1,560評論 0 0
  • 久違的晴天,家長會。 家長大會開好到教室時,離放學(xué)已經(jīng)沒多少時間了。班主任說已經(jīng)安排了三個家長分享經(jīng)驗。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,788評論 16 22

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