DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
用 CSS3 屬性打造精緻圖片樣式:圓角、陰影與漸層的 16 種玩法
編輯:HTML5詳解     

在 CSS3 裡,如果你直接把 box-shadow 和 border-radius 套在 <img> 上,瀏覽器的渲染結果往往不如預期。但同樣的樣式如果套在一個以 background-image 呈現圖片的容器上,就能正確渲染。

接下來示範如何運用 box-shadow、border-radius 與 transition 變化出多種圖片樣式。

問題在哪裡

先對第一行圖片套用圓角與內嵌陰影:

.normal img {
    border: solid 5px #000;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    -webkit-box-shadow: inset 0 1px 5px rgba(0,0,0,.5);
    -moz-box-shadow: inset 0 1px 5px rgba(0,0,0,.5);
    box-shadow: inset 0 1px 5px rgba(0,0,0,.5);
}

Firefox 畫出了圓角,卻忽略了內嵌陰影;Chrome 與 Safari 則兩者都沒畫出來。

Firefox 效果:

Chrome / Safari 效果:

解法:改用 background-image

要讓圓角與內嵌陰影都能正常運作,得把圖片從 <img> 換成容器的背景圖。

用 jQuery 動態轉換

如果頁面上已經有大量 <img>,手動改寫不現實。這段腳本會為每張圖片包上一層 <span>,並把該 span 的背景設為原圖路徑:

<script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("img").load(function () {
        $(this).wrap(function () {
            return '<span class="image-wrap ' + $(this).attr('class')
                 + '" style="position:relative; display:inline-block;'
                 + 'background:url(' + $(this).attr('src') + ') no-repeat center center;'
                 + 'width: ' + $(this).width() + 'px;'
                 + 'height: ' + $(this).height() + 'px;" />';
        });
        $(this).css("opacity", "0");
    });
});
</script>

輸出的結構長這樣:

<span class="image-wrap " style="position:relative; display:inline-block;
      background:url(image.jpg) no-repeat center center;
      width: 150px; height: 150px;">
  <img src="image.jpg" style="opacity: 0;">
</span>

關鍵是最後那行 opacity: 0:原圖還留在 DOM 裡(保留語意與無障礙性),但視覺上完全交給外層 span 的背景來呈現,樣式就套得上去了。

圓形圖片

.circle .image-wrap {
    -webkit-border-radius: 50em;
    -moz-border-radius: 50em;
    border-radius: 50em;
}

給一個遠大於實際尺寸的圓角值,瀏覽器就會自動收斂成正圓。這比計算半徑方便,也不需要知道圖片實際邊長。

卡片風格

.card .image-wrap {
    -webkit-box-shadow: inset 0 0 1px rgba(0,0,0,.8),
                        inset 0 2px 0 rgba(255,255,255,.5),
                        inset 0 -1px 0 rgba(0,0,0,.4);
    -moz-box-shadow: inset 0 0 1px rgba(0,0,0,.8),
                     inset 0 2px 0 rgba(255,255,255,.5),
                     inset 0 -1px 0 rgba(0,0,0,.4);
    box-shadow: inset 0 0 1px rgba(0,0,0,.8),
                inset 0 2px 0 rgba(255,255,255,.5),
                inset 0 -1px 0 rgba(0,0,0,.4);
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}

三層內嵌陰影的分工:最外圈一點深色描邊定形、頂部一道淺色高光模擬光源、底部一道深色暗示厚度。這就是擬物化「纸片感」的基本配方。

浮雕風格

.embossed .image-wrap {
    -webkit-box-shadow: inset 0 0 2px rgba(0,0,0,.8),
                        inset 0 2px 0 rgba(255,255,255,.5),
                        inset 0 -7px 0 rgba(0,0,0,.6),
                        inset 0 -9px 0 rgba(255,255,255,.3);
    -moz-box-shadow: inset 0 0 2px rgba(0,0,0,.8),
                     inset 0 2px 0 rgba(255,255,255,.5),
                     inset 0 -7px 0 rgba(0,0,0,.6),
                     inset 0 -9px 0 rgba(255,255,255,.3);
    box-shadow: inset 0 0 2px rgba(0,0,0,.8),
                inset 0 2px 0 rgba(255,255,255,.5),
                inset 0 -7px 0 rgba(0,0,0,.6),
                inset 0 -9px 0 rgba(255,255,255,.3);
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}

比卡片風格多了一道底部厚邊,讓圖片看起來像是從紙面上壓印出來的。

柔性浮雕風格

相對於標準浮雕,這個版本為每層陰影加了模糊半徑。

.soft-embossed .image-wrap {
    -webkit-box-shadow: inset 0 0 4px rgba(0,0,0,1),
                        inset 0 2px 1px rgba(255,255,255,.5),
                        inset 0 -9px 2px rgba(0,0,0,.6),
                        inset 0 -12px 2px rgba(255,255,255,.3);
    -moz-box-shadow: inset 0 0 4px rgba(0,0,0,1),
                     inset 0 2px 1px rgba(255,255,255,.5),
                     inset 0 -9px 2px rgba(0,0,0,.6),
                     inset 0 -12px 2px rgba(255,255,255,.3);
    box-shadow: inset 0 0 4px rgba(0,0,0,1),
                inset 0 2px 1px rgba(255,255,255,.5),
                inset 0 -9px 2px rgba(0,0,0,.6),
                inset 0 -12px 2px rgba(255,255,255,.3);
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}

模糊半徑讓邊緣由硬轉柔,質感更接近布料或霧面材質。

挖空風格

.cut-out .image-wrap {
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.2),
                        inset 0 4px 5px rgba(0,0,0,.6),
                        inset 0 1px 0 rgba(0,0,0,.6);
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.2),
                     inset 0 4px 5px rgba(0,0,0,.6),
                     inset 0 1px 0 rgba(0,0,0,.6);
    box-shadow: 0 1px 0 rgba(255,255,255,.2),
                inset 0 4px 5px rgba(0,0,0,.6),
                inset 0 1px 0 rgba(0,0,0,.6);
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}

上緣一道深內陰影加上模糊的向下擴散,視覺上就變成「挖進去」而非「疊上去」。

變形與發光

這個範例為容器加上 transition,滑鼠移入時從圓角矩形平滑變成正圓,同時用多層 box-shadow 製造光暈。

.morphing-glowing .image-wrap {
    -webkit-transition: 1s;
    -moz-transition: 1s;
    transition: 1s;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}
.morphing-glowing .image-wrap:hover {
    -webkit-box-shadow: 0 0 20px rgba(255,255,255,.6),
                        inset 0 0 20px rgba(255,255,255,1);
    -moz-box-shadow: 0 0 20px rgba(255,255,255,.6),
                     inset 0 0 20px rgba(255,255,255,1);
    box-shadow: 0 0 20px rgba(255,255,255,.6),
                inset 0 0 20px rgba(255,255,255,1);
    -webkit-border-radius: 60em;
    -moz-border-radius: 60em;
    border-radius: 60em;
}

外發光加內發光的組合,讓圖片邊緣整體亮起來。記得把 transition 寫在常態而非 hover 上,這樣移出時也會有同樣順滑的收放。

高光效果

利用 :after 偽元素在圖片上半部疊一層由上而下淡出的白色漸層。

.glossy .image-wrap {
    -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);
    -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);
    box-shadow: inset 0 -1px 0 rgba(0,0,0,.5);
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}
.glossy .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 50%;
    top: 0;
    left: 0;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);
    background: -webkit-gradient(linear, left top, left bottom,
                color-stop(0%,rgba(255,255,255,0.7)),
                color-stop(100%,rgba(255,255,255,.1)));
    background: linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);
}

偽元素記得補 content: ' ' 與定位,否則不會生成。上緣的圓角也要跟容器對齊,不然會露出直角。

倒影效果

把高光邏輯移到下方,並改為深色漸層,就得到倒影。

.reflection .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 30px;
    bottom: -31px;
    left: 0;
    -webkit-border-top-left-radius: 20px;
    -webkit-border-top-right-radius: 20px;
    -moz-border-radius-topleft: 20px;
    -moz-border-radius-topright: 20px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    background: -moz-linear-gradient(top, rgba(0,0,0,.3) 0%, rgba(255,255,255,0) 100%);
    background: -webkit-gradient(linear, left top, left bottom,
                color-stop(0%,rgba(0,0,0,.3)),
                color-stop(100%,rgba(255,255,255,0)));
    background: linear-gradient(top, rgba(0,0,0,.3) 0%, rgba(255,255,255,0) 100%);
}
.reflection .image-wrap:hover {
    position: relative;
    top: -8px;
}

注意倒影只需要上方圓角,下方保持直角才能與主體自然接連。

高光加倒影

同時啟用 :before 與 :after,一個負責上方高光、一個負責下方倒影。

.glossy-reflection .image-wrap {
    -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5),
                        inset 0 1px 0 rgba(255,255,255,.6);
    -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,.5),
                     inset 0 1px 0 rgba(255,255,255,.6);
    box-shadow: inset 0 -1px 0 rgba(0,0,0,.5),
                inset 0 1px 0 rgba(255,255,255,.6);
    -webkit-transition: 1s;
    -moz-transition: 1s;
    transition: 1s;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}
.glossy-reflection .image-wrap:before {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 50%;
    top: 0;
    left: 0;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);
    background: -webkit-gradient(linear, left top, left bottom,
                color-stop(0%,rgba(255,255,255,0.7)),
                color-stop(100%,rgba(255,255,255,.1)));
    background: linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,.1) 100%);
}
.glossy-reflection .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 30px;
    bottom: -31px;
    left: 0;
    -webkit-border-top-left-radius: 20px;
    -webkit-border-top-right-radius: 20px;
    -moz-border-radius-topleft: 20px;
    -moz-border-radius-topright: 20px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    background: -moz-linear-gradient(top, rgba(230,230,230,.3) 0%, rgba(230,230,230,0) 100%);
    background: -webkit-gradient(linear, left top, left bottom,
                color-stop(0%,rgba(230,230,230,.3)),
                color-stop(100%,rgba(230,230,230,0)));
    background: linear-gradient(top, rgba(230,230,230,.3) 0%, rgba(230,230,230,0) 100%);
}

膠帶風格

.tape .image-wrap {
    -webkit-box-shadow: inset 0 0 2px rgba(0,0,0,.7),
                        inset 0 2px 0 rgba(255,255,255,.3),
                        inset 0 -1px 0 rgba(0,0,0,.5),
                        0 1px 3px rgba(0,0,0,.4);
    -moz-box-shadow: inset 0 0 2px rgba(0,0,0,.7),
                     inset 0 2px 0 rgba(255,255,255,.3),
                     inset 0 -1px 0 rgba(0,0,0,.5),
                     0 1px 3px rgba(0,0,0,.4);
    box-shadow: inset 0 0 2px rgba(0,0,0,.7),
                inset 0 2px 0 rgba(255,255,255,.3),
                inset 0 -1px 0 rgba(0,0,0,.5),
                0 1px 3px rgba(0,0,0,.4);
}
.tape .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 60px;
    height: 25px;
    top: -10px;
    left: 50%;
    margin-left: -30px;
    border: solid 1px rgba(137,130,48,.2);
    background: -moz-linear-gradient(top, rgba(254,243,127,.6) 0%, rgba(240,224,54,.6) 100%);
    background: -webkit-gradient(linear, left top, left bottom,
                color-stop(0%,rgba(254,243,127,.6)),
                color-stop(100%,rgba(240,224,54,.6)));
    background: linear-gradient(top, rgba(254,243,127,.6) 0%, rgba(240,224,54,.6) 100%);
    -webkit-box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 1px 0 rgba(0,0,0,.2);
}

用 margin-left 負值搭配 left: 50% 做水平居中,再用半透明的黃色漸層加上傾斜角度(可自行加 transform: rotate(-2deg))就會更寫實。

變形與著色

滑鼠移入時,形狀由圓角矩形轉為正圓,同時 :after 上浮現由中心向外擴散的放射狀漸層。

.morphing-tinting .image-wrap {
    position: relative;
    -webkit-transition: 1s;
    -moz-transition: 1s;
    transition: 1s;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
}
.morphing-tinting .image-wrap:hover {
    -webkit-border-radius: 30em;
    -moz-border-radius: 30em;
    border-radius: 30em;
}
.morphing-tinting .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    -webkit-transition: 1s;
    -moz-transition: 1s;
    transition: 1s;
    -webkit-border-radius: 30em;
    -moz-border-radius: 30em;
    border-radius: 30em;
}
.morphing-tinting .image-wrap:hover:after {
    background: -webkit-gradient(radial, 50% 50%, 40, 50% 50%, 80,
                from(rgba(0,0,0,0)), to(rgba(0,0,0,1)));
    background: -moz-radial-gradient(50% 50%, circle,
                rgba(0,0,0,0) 40px, rgba(0,0,0,1) 80px);
}

羽化邊緣圓形

同樣是放射狀漸層,但這次反過來用白色由透明漸向不透明,形成一層柔邊遮罩。

.feather .image-wrap {
    position: relative;
    -webkit-border-radius: 30em;
    -moz-border-radius: 30em;
    border-radius: 30em;
}
.feather .image-wrap:after {
    position: absolute;
    content: ' ';
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: -webkit-gradient(radial, 50% 50%, 50, 50% 50%, 70,
                from(rgba(255,255,255,0)), to(rgba(255,255,255,1)));
    background: -moz-radial-gradient(50% 50%, circle,
                rgba(255,255,255,0) 50px, rgba(255,255,255,1) 70px);
}

關鍵是兩個半徑參數的差距——內圈 50px 完全透明,外圈 70px 全不透明,中間那 20px 就是羽化的柔邊寬度。想讓柔邊更寬,把差距拉大即可。

瀏覽器相容性

這套做法在支援 border-radius、box-shadow、:before、:after 的瀏覽器上都能正常運作。在不支援這些特性的環境中,最壞的情況就是顯示原始圖片,屬於可接受的降級。

實務上你不需要再寫 -webkit- 與 -moz- 前綴了——這些屬性早已成為標準,主流瀏覽器都直接支援無前綴寫法。上面保留前綴是為了對應原始範例的時代背景。

打造你自己的樣式

掌握 :before 與 :after 這兩個免費的額外元素,加上多層 box-shadow 與漸層的組合,變化空間其實非常大。上面十六個範例只是起點,你可以試著把它們混搭——例如「膠帶」加「卡片」、或「倒影」加「變形」——往往會得到更有趣的結果。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved