天天看點

40個來自CSS世界中的實用技巧彙總

40個來自CSS世界中的實用技巧彙總
1、清除浮動

主要為子元素浮動(float)之後,父元素無法撐起高度和寬度。

<!-- html -->
<div class="clear">
    <img src="demo.gif">
</div>


<!-- css -->
<style>
    img {
        float: left;
    }
    /* 清除浮動 */
    .clear::after {
      content: "";
      display: block;
      clear: both;
    }
</style>      
2、文字少時居中,多時靠左

但是要注意,當p的内容為英文單詞組成的時候,如果單詞過長,而不是“ pppppppppppppppppppppppppppppp”這樣的一次,會被視為一個機關而造成超過div的尺寸。

如果你想要英文字元也有中文字元的效果的話,在p使用“ word-break:break-all”。

<!-- html -->
<div class="box">
    <p class="content"></p>
</div>


<!-- css -->
<style>
    .box {
        text-align: center;
    }
    .content {
        display: inline-block;
        text-align: left;
    }
</style>      
3、凹凸人

目的在于制造一個凹或凸的形狀,利用了“ 2”。

<!-- html -->
<div class='ao'></div>


<!-- CSS -->
<style>
    .ao {
        display: inline-block;
        width: 0;
    }
    .ao::before {
        content: "love 你 love";
        outline: 2px solid #000;
        color: #fff;
    }
</style>      
4、讓padding,border不影響盒模型的大小

相信這點大部分人都知道,但是有一些奇怪的行為,比如說width <content + padding會怎樣?實際上當padding + border> width時,元素的渲染大小(Chrome下)為padding + border;而padding + border <width時,允許剩餘空間配置設定給content。

<!-- html -->
<div></div>


<!-- CSS -->
<style>
    div {
        box-sizing: border-box;
    }
</style>      
身高:100%占屏效果
<!-- html -->
<div></div>


<!-- CSS方法一 -->
<style>
    html,body {
        height: 100%;
    } 
    div {
        height: 100%
    }
</style>
<!-- CSS方法二 -->
<style>
    div {
        position: absolute;
        height: 100%;
    }
</style>      
5、任意高度元素展開

缺點是,如果高度太大會造成展開過快和重複中斷,那麼這個足夠大的值應該适當。

<!-- html -->
<div></div>


<!-- CSS -->
<style>
    div {
        max-height: 0;
        overflow: hidden;
        transition: max-height .25s; 
    }
    div.active {
        max-height: 666px;  /* 需要足夠大的值 */
    }
</style>      
6、優雅的圖檔未加載或加載失敗效果

需要注意的是,圖檔顯示完成後,img會成為“替換元素”,而替換元素是無法設定僞元素的,因為内容被圖檔替換掉了;還需要注意attr裡面的變量不能加雙引号。

<!-- html -->
<div>
    <img src="demo.gif" alt="lululu">
</div>


<!-- CSS -->
<style>
    div {
        width: 100px;
        height: 100px;
        overflow: hidden;
    }
    img {
        display: inline-block;
        width: 100%;
        height: 100%;
        position: relative;
    }
    img::after {
        /* 生成 alt 資訊 */
        content: attr(alt);
        /* 尺寸和定位 */
        position: absolute; left: 0;bottom: 0;right: 0;
        /* 顔色 */
        background-color: rgba(0,0,0,.5);
        /* alt 資訊隐藏 */
        transform: translateY(100%);
        /* 過渡動畫效果 */
        transition: transform .2s;
        }
        img:hover::after {
        /* alt 資訊顯示 */
        transform: translateY(0);
    } 
</style>      
7、CSS的懸浮圖檔替換效果

需要注意的是,如果快捷儲存圖檔,儲存的是src内的圖檔,而不是替換之後的。

<!-- html -->
<img src="demo.gif">


<!-- CSS -->
<style>
    img:hover {
        content: url(amazing.gif);
    }
</style>      
8、利于seo的“替換元素”标題logo

用h1的原因主要是因為SEO,語義化的問題。

<!-- html -->
<h1>Weismann's blog</h1>


<!-- CSS -->
<style>
    h1 {
        content: url(logo.gif);
    }
</style>      
9、高相容,自動等寬,底部對齊的柱狀圖

需要注意的是,第一個i不能換行,換行後會産生後移的結果。

<!-- html -->
<div class="box"><i class="bar"></i>
    <i class="bar"></i>
    <i class="bar"></i>
    <i class="bar"></i>
</div>


<!-- CSS -->
<style>
    .box {
        width: 256px;
        height: 256px;
        text-align: justify;
    }
    .box:before { 
        content: "";
        display: inline-block;
        height: 100%;
    }
    .box:after {
        content: "";
        display: inline-block;
        width: 100%;
    }
    .bar {
        display: inline-block;
        width: 20px;
        /* height自定 */
    } 
</style>      
10、高相容性的加載效果

在IE6-IE9下是...,其他都是動态的;使用點的目的是語義化和低版本浏覽器的相容。

<!-- html -->
正在加載中<dot>...</dot>


<!-- CSS -->
    <style>
    dot {
        display: inline-block;
        height: 1em;
        line-height: 1;
        text-align: left;
        vertical-align: -.25em;
        overflow: hidden;
    }
    dot::before {
        display: block;
        content: '...\A..\A.'; 
        white-space: pre-wrap;
        animation: dot 3s infinite step-start both;
    }
    @keyframes dot {
        33% { transform: translateY(-2em); }
        66% { transform: translateY(-1em); }
    }
</style>      
11、擴大點選區域

第一種主要利用了内聯元素的填充隻會影響外觀和不影響布局的特點;第二種針對其他屬性會改變背景圖定位的一種方式。

<!-- html -->
<a href="">demo</a>


<!-- CSS1 -->
<style>
    a {
        padding: 20px 0; 
    }
</style>
<!-- CSS2 -->
<style>
    a {
        border: 11px solid transparent;
    }
</style>      
12、不使用僞元素的“三道杠”和“圓點”效果
<!-- html -->
<i class="icon"></i>
<!-- CSS三道杠 -->
<style>
    .icon {
        display: inline-block;
        width: 140px; height: 10px;
        padding: 35px 0;
        border-top: 10px solid;
        border-bottom: 10px solid;
        background-color: currentColor;
        background-clip: content-box;
    }
</style>
<!-- CSS三道杠2 -->
<style>
    .icon {
        width: 120px;
        height: 20px;
        border-top: 60px double;
        border-bottom: 20px solid;
    }
</style>
<!-- CSS圓點 -->
<style>
    .icon {
        display: inline-block;
        width: 100px; height: 100px;
        padding: 10px;
        border: 10px solid;
        border-radius: 50%;
        background-color: currentColor;
        background-clip: content-box;
    }
</style>      
13、導航欄消除右邊多餘的尺寸

利用保證金來改變尺寸,需要注意,改變尺寸的元素水準方向的尺寸不能是确定的。

<!-- html -->
<div>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>


<!-- CSS -->
<style>
    div {
        width: 380px;
    }
    ul {
        margin-right: -20px;
    }
    ul > li {
        float: left;
        width: 100px;
        margin-right: 20px;
    } 
</style>      
14、正确的滾動底部留白方式

如果使用padding留白,在Firefox和IE不會顯示。

<!-- html -->
<div class="box">
    <img src="demo.gif">
</div>


<!-- CSS -->
    <style>
    .box {
        height:200px;
        overflow:auto;
    }
    .box>img {
        margin: 50px 0;
    }
</style>      
15、高相容的多欄等高

注意container高度不能是确定值,缺點是如果在内部使用錨點定位會出現問題。

<!-- html -->
<div class="container">
    <div id="colLeft" class="column-left">
        <h4>正方觀點</h4>
        <p>觀點1</p>
        <p>觀點1</p>
    </div>
    <div id="colRight" class="column-right">
        <h4>反方觀點</h4>
        <p>觀點1</p>
    </div>
</div>


<!-- CSS -->
<style>
    .container {
        overflow: hidden;
    }
    .column-left,
    .column-right {
        margin-bottom: -9999px;
        padding-bottom: 9999px;
        width: 50%;
        float: left;
    }
    .column-left {
        background-color: #34538b;
    }
    .column-right {
        background-color: #cd0000;
    }
</style>      
16、正确的塊級元素右對齊

自動值對于保證金是占用剩餘的空間。

<!-- html -->
<div>demo</div>


<!-- CSS -->
<style>
    div {
        width: 100px;
        margin-left: auto;
    }
</style>      
17、圖檔上傳增加框

此技巧主要說明border的顔色預設是繼承自color的。

<!-- html -->
<div class="add"></div>


<!-- CSS -->
<style>
    .add {
        display: inline-block;
        width: 76px; height: 76px;
        color: #ccc;
        border: 2px dashed;
        text-indent: -12em;
        transition: color .25s;
        position: relative;
        overflow: hidden;
    }
    .add:hover {
        color: #34538b;
    }
    .add::before, .add::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
    }
    .add::before {
        width: 20px;
        border-top: 4px solid;
        margin: -2px 0 0 -10px;
    }
    .add::after {
        height: 20px;
        border-left: 4px solid;
        margin: -10px 0 0 -2px;
    }
</style>      
18、不影響背景圖檔位置設定邊距。

和增加點選區域第二種方式一樣。

<!-- html -->
<div class="box"></div>


<!-- CSS -->
<style>
    .box {
        display: inline-block;
        width: 100px;
        height: 100px;
        border-right: 50px solid transparent;
        background-position: 100% 50%;
    }
</style>      
19、border制作梯形,各種三角形
<!-- html -->
<div></div>


<!-- CSS梯形 -->
<style>
    div {
        width: 10px; height: 10px;
        border: 10px solid;
        border-color: #f30 transparent transparent;
    }
</style>
<!-- CSS三角 -->
<style>
    div {
        width: 0;
        border-width: 10px 20px;
        border-style: solid;
        border-color: #f30 transparent transparent;
    } 
</style>
<!-- CSS直角三角 -->
<style>
    div {
        width: 0;
        border-width: 10px 20px;
        border-style: solid;
        border-color: #f30 #f30 transparent transparent;
    } 
</style>      
20、高相容雙欄,一邊等寬一邊自适應,等高布局

缺點是邊框不支援百分比,最多2-3欄。

<!-- html -->
<div class="box">
    <nav>
        <div>123</div>
        <div>123</div>
        <div>123</div>
    </nav>
    <section>
        <div>1234</div>
    </section>
</div>


<!-- CSS -->
<style>
    .box {
        border-left: 150px solid #333;
        background-color: #f0f3f9;
    }
    .box::after {
        content: "";
        display: block;
        clear: both;
    }
    .box > nav {
        width: 150px;
        margin-left: -150px;
        float: left;
    }
    .box > section {
        overflow: hidden;
    }
</style>      
21、内聯元素“近似”垂直居中

而為什麼說“近似”,一句話說清楚,請看開頭。

<!-- CSS -->
<style>
    span {
        line-height: 24px;
    }
</style>      
22、多行内容“近似”垂直居中
<!-- html -->
<div class="box">
    <div class="content">基于行高實作的...</div>
</div> 


<!-- CSS -->
<style>
    .box {
        width: 120px;
        line-height: 120px;
        background-color: #f0f3f9;
    }
    .content {
        display: inline-block;
        line-height: 20px;
        margin: 0 20px;
        vertical-align: middle;
    } 
</style>      
21、容器内圖檔的垂直方向間隙問題

産生的問題和“幽靈空白子系統”和x-height有關,你可以嘗試在img前加入x字元觀察一下。

<!-- html -->
<div class="box">
    <img src="demo.gif">
</div>


<!-- CSS -->
<style>
    .box {
        width: 280px;
        outline: 1px solid #aaa;
        text-align: center;
        /* 解決方案1 */
        font-size: 0;
        /* 解決方案2 */
        line-leight: 0;
    }
    .box > img {
        height: 96px;
        /* 解決方案3 */
        display: block;
    } 
</style>      
22、圖示文字對齊

ex代表的是x-height的高度,根據x字形的不同(如font-family)而不同。

<!-- 方式一 -->
<!-- html -->
<div class="box">
    <p>
        <i class="icon icon-demo"></i>拉拉
    </p>
</div>


<!-- CSS -->
<style>
    .box {
        /* 根據圖檔大小變化 */
        line-height: 20px;
    }
    p {
        font-size: 40px;
    }
    .icon {
        display: inline-block;
        width: 20px;
        height: 20px; 
        white-space: nowrap;
        letter-spacing: -1em;
        text-indent: -999em;
    }
    .icon::before {
        /* 低版本IE7相容 */
        content: '\3000';
    }
    .icon-demo {
        background: url(demo.png) no-repeat center;
    }
</style>


<!-- 方式二 -->
<!-- html -->
<p>文字 <img src="delete.png"></p>


<!-- CSS -->
<style>
    p {
        font-size: 14px;
    }
    p > img {
        width: 16px; height: 16px;
        vertical-align: .6ex;
        position: relative;
        top: 8px;
    }
</style>      
23、永遠居中的彈框

特點是内容和浏覽器尺寸變化都是自動變換大小和位置,可以通過僞元素的高度控制上下位置。

<!-- html -->
<div class="container">
    <div class="dialog">demo</dialog>
</div>


<!-- CSS -->
<style>
    .container {
        position: fixed;
        top: 0; right: 0; bottom: 0; left: 0;
        background-color: rgba(0,0,0,.5);
        text-align: center;
        font-size: 0;
        white-space: nowrap;
        overflow: auto;
    }
    .container::after {
        content: '';
        display: inline-block;
        height: 100%;
        vertical-align: middle;
    }
    .dialog {
        display: inline-block;
        vertical-align: middle;
        text-align: left;
        font-size: 14px;
        white-space: normal;
        /* 彈框樣式 */
        padding: 10px 14px;
        border: 1px solid #000;
        border-radius: 4px;
        background: #fff;
    }
</style>      
24、文字環繞圖檔

float的真正用途。

<!-- html -->
<div class="box">
    <div>
        <img src="demo.gif">
    </div>
    <p>demo,demo,demo,demo,demo,demo,demo</p>
</div>


<!-- CSS -->
<style>
    .box {
        width: 100px;
    }
    img {
        float: left;
        width: 40px;
        height: 40px;
    }
</style>      
25、利用溢出:隐藏自定義滾動條

實際上overflow:hidden是可以滾動的,可以通過錨點,focus,scrollTop滾動。滾動條的實作請自行發揮。

26、通過标簽實作的頁籤效果

與錨點不同的是不會觸發由内到外(多層滾動造成的某種事件冒泡的效果)的頁面跳動(元素上邊與分段上邊對齊),還支援Tab選項的效果;缺點是需要js支援效果。

<!-- html -->
<div class="box">
    <div class="list"><input id="one">1</div>
    <div class="list"><input id="two">2</div>
    <div class="list"><input id="three">3</div>
    <div class="list"><input id="four">4</div>
</div>
<div class="link">
    <label class="click" for="one">1</label>
    <label class="click" for="two">2</label>
    <label class="click" for="three">3</label>
    <label class="click" for="four">4</label>
</div> 


<!-- CSS -->
<style>
    .box {
        height: 10em;
        border: 1px solid #ddd;
        overflow: hidden;
    }
    .list {
        height: 100%;
        background: #ddd;
        position: relative;
    }
    .list > input {
        position: absolute; top:0;
        height: 100%; width: 0;
        border:0; padding: 0; margin: 0;
    } 
</style>      
27、“包含塊”的絕對定位元素“一柱擎天”問題
<!-- html -->
<div class="father">
    <div class="son">拉拉</div>
</div>


<!-- CSS -->
<style>
    .father {
        position: relative;
        width: 20px;
        height: 20px;
    }
    .son {
        position: absolute;
        /* 解決方案 */
        white-space: nowrap;
    }
</style>      
28、“無依賴絕對定位”的表單驗證應用

在一個元素上如果單用(父元素的位置屬性均是替換)“ position:absolute”,事實上元素将原地不動,最終會産生BFC。

<!-- html -->
<div class="group">
    <label class="label"><span class="star">*</span>郵箱</label>
    <div class="cell">
        <input type="email" class="input">
        <span class="remark">郵箱格式不準确(示意)</span>
    </div>
</div>
<div class="group">
    ...
</div>


<!-- CSS -->
    <style>
    .group {
        width: 300px;
    }
    .label {
        float: left;
    }
    .remark {
        position: absolute;
    }
</style>      
29、主體頁面側邊欄

利用text-align和fixed的組合;高度放置0和overflow隐藏目的是為了不影響主體的體驗,而之是以絕對定位元素沒有被隐藏的原因是“如果overflow不是定位元素,同時絕對定位元素和overflow容器同時也沒有定位元素,則溢出無法對絕對定位元素進行剪裁。” —《 CSS世界》。

<!-- html -->
<div class="alignright">
    <span class="follow"></span>
</div>


<!-- CSS -->
<style>
    .alignright {
        height: 0;
        text-align: right;
        overflow: hidden;
        background: blue;
    }
    .alignright:before {
        content: "\2002";
    }
    .follow {
        position: fixed;
        bottom: 100px;
        z-index: 1;
        width: 10px;
        height: 10px;
        border: 1px solid #000;
    } 
</style>      
30、不通過寬度和高度設定預定全占用

利用top和bottom或left和right同時設定的時候會觸發流體特性的特點;與通過“ top:0; left:0; width:100%; height:100%;”,在設定邊距,邊框, padding的時候不會溢出到上方的外面(就算你想到box-sizing,那margin呢?);而之是以用span的原因是想說明絕對定位放置元素的displayplace為block。

<!-- html -->
<span></span>
<!-- CSS -->
<style>
    span {
        position: absolute;
        top:0;
        left:0;
        right:0;
        bottom: 0;
    }
</style>      
31、margin:自動水準垂直居中
<!-- html -->
<div></div>


<!-- CSS -->
<style>
    div {
        width: 300px; height: 200px;
        position: absolute;
        left: 0; right: 0; top: 0; bottom: 0;
        margin: auto;
    }
</style>      
32、紙張卷邊陰影

主要利用“位置:相對; z-index:0;”建立并合并到z-index的負值将陰影放置在“ contaniner”和“ page”之間。

你可以嘗試将關鍵CSS去掉檢視效果。

<!-- html -->
<div class="container">
    <div class="page">
        <h4>demo</h4>
        <p>demo</p>
    </div>
</div>


<!-- CSS -->
<style>
    .container {
        background-color: #666;
        height: 1000px;
        /* 建立層疊上下文,關鍵 */
        position: relative;
        z-index: 0;
    }
    .page {
        width: 600px;
        background-color: #f4f39e;
        background: linear-gradient(to bottom, #f4f39e, #f5da41 60%, #fe6);
        box-shadow: 0 2px 10px 1px rgba(0, 0, 0, .2);
        text-shadow: 0 1px 0 #f6ef97;
        position: relative;
        left: 200px;
        top: 200px;
    }
    .page:before {
        transform: skew(-15deg) rotate(-5deg);
        transform-origin: left bottom;
        left: 0;
    }
    .page:after {
        transform: skew(15deg) rotate(5deg);
        transform-origin: right bottom;
        right: 0;
    }
    /* 邊角卷邊陰影 */
    .page:before, .page:after {
        width: 90%; height: 20%;
        content: "";
        box-shadow: 0 8px 16px rgba(0, 0, 0, .3);
        position: absolute;
        bottom: 0;
        z-index: -1;
    }
</style>      
33、隐藏文字

說這個主要是為了說明,Chrome浏覽器如果字型設定12px以下的大小(新版本已經不限制了),會被自動處理成12px,但是有一個值除外,0。

<!-- CSS -->
<style>
    p {
        font-size: 0;
    }
</style>      
34、解決文本裝飾下劃線和文本重疊

因為是内聯元素,是以完全不用擔心會影響元素高度的問題。

<!-- CSS -->
<style>
    a {
        text-decoration: none;
        border-bottom: 1px solid;
        padding-bottom: 5px;
    }
</style>      
35、自動将輸入的小寫字母轉換大寫
<!-- CSS -->
<style>
    input {
        text-transform: uppercase;
    }
</style>      
36、價格場景下的首個符号選擇器

特點是可以讓html結構活躍幹淨。

<!-- html -->
<p class="price">¥399</p>


<!-- CSS -->
<style>
    .price:first-letter {
        ...
    }
</style>      
37、元素隐藏同時資源不加載

後續可通過script.innerHTML通路。

<!-- html -->
<script type="text/html">
    <img src="1.jpg">
</script>      
38、頭像裁剪矩形镂空效果

主要利用輪廓。

<!-- html -->
<div class="crop">
    <div id="cropArea" class="crop-area"></div>
    <img src="demo.gif">
</div>


<!-- CSS -->
<style>
    .crop {
        width: 200px; height: 200px;
        overflow: hidden;
        position: relative;
    }
    .crop > .crop-area {
        position: absolute; top:0; height: 0;
        width: 80px; height: 80px;
        outline: 200px solid rgba(0,0,0,.5);
        cursor: move;
    } 
</style>      
39、自定義光标

需要注意IE隻支援cur檔案。

<!-- CSS -->
<style>
    .cursor-demo {
        cursor: url(demo.cur);
    }
</style>      
40、修改水準流到垂直流

相容到IE7;此應用涉及到一體的東西,所有水準流的特性都可以應用到垂直流中(稱為水準居中變成了垂直居中)。

<!-- CSS -->
<style>
    .verticle-mode{
        writing-mode: tb-rl;
        -webkit-writing-mode: vertical-rl;
        writing-mode: vertical-rl;
    }
</style>      

本文完〜