CSS3實現背景透明文字不透明的效果

來源:互聯網
上載者:User
這篇文章主要介紹了CSS3實現背景透明文字不透明的範例程式碼的相關資料,內容挺不錯的,現在分享給大家,也給大家做個參考。

最近遇到一個需求,要在圖片上顯示帶有半透明背景的文字,效果如所示:

需求.png

看到這個需求之後,第一反應是使用CSS3中的opacity設定元素的透明度。

<!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>        * {            padding: 0;            margin: 0;        }        .container {            width: 600px;            height: 400px;            background: url('https://img1.dongqiudi.com/fastdfs3/M00/18/56/ChOxM1stHByARuNmAAGsJDKXtuM269.jpg') no-repeat;            background-size: cover;            -webkit-background-size: cover;            -o-background-size: cover;            background-position: center 0;        }        .demo {            position: absolute;            width: 260px;            height: 60px;            top: 260px;            line-height: 60px;            text-align: center;            background-color: black;            opacity: 0.5;        }        .demo p {            color: #FFF;            font-size: 18px;            font-weight: 600;        }    </style></head><body>    <p class="container">        <p class="demo">            <p>2018世界盃已開幕:10天</p>        </p>    </p></body></html>

效果如下:

背景透明,文字也透明.png

這樣貌似也滿足了需求,不過並不完美,設定opacity之後,整個元素都半透明了,造成文字顯得模糊,這樣的解決方式並不可取。

其實實現透明的CSS方法並不只有設定opacity一種方式。還有另外兩種:

  • css3的rgba(red, green, blue, alpha),alpha的取值從 0 到 1,如rgba(255,255,255,0.8)

  • IE專屬濾鏡 filter:Alpha(opacity=x),x 的取值從 0 到 100,如filter:Alpha(opacity=80)

在這裡我採用了設定rgba的方式:

<!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>        * {            padding: 0;            margin: 0;        }        .container {            width: 600px;            height: 400px;            background: url('https://img1.dongqiudi.com/fastdfs3/M00/18/56/ChOxM1stHByARuNmAAGsJDKXtuM269.jpg') no-repeat;            background-size: cover;            -webkit-background-size: cover;            -o-background-size: cover;            background-position: center 0;        }        .demo {            position: absolute;            width: 260px;            height: 60px;            top: 260px;            line-height: 60px;            text-align: center;            background-color: rgba(0,0,0,0.5);        }        .demo p {            color: #FFF;            font-size: 18px;            font-weight: 600;        }    </style></head><body>    <p class="container">        <p class="demo">            <p>2018世界盃已開幕:10天</p>        </p>    </p></body></html>

效果如下:

背景透明,文字不透明.png

這樣設定之後,文字顯得清晰了許多。

小結

其實要實現這個需求,並不只有這一種思路,還可以用兩個p放在同一個位置,一個是半透明的背景p,一個是文字p,一樣可以解決問題,但是需要寫絕對位置或負margin,並出現空內容的p,這種方法在有些情境下會顯得略微複雜,如下樣本所示,所以在實際需求情境中還是要具體問題具體分析。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.