1. 程式人生 > >CSS3實現背景透明文字不透明

CSS3實現背景透明文字不透明

put fim webkit php 找到 lin har strong 如果

  最近遇到一個需求,如下圖,input框要有透明效果

  技術分享圖片

  首先想到的方法是CSS3的 opacity屬性,但事實證明我想的太簡單了

  這個屬性雖然讓input框有透明效果,同時文字也會有透明效果,導致文字不清晰,如下圖

  技術分享圖片

  於是我開始思考其它方式,最後想到了通過透明背景圖來實現

  背景圖雖然能夠解決問題,但會造成空div,並且還需要設置定位

  感覺把簡單的事情變得復雜了,我始終認為有更簡單的方法

  最後終於被我找到了完美解決的方法——用CSS3的 rgba 來設置

  background-color: rgb(255,255,255,0.4); 這種方法在PC端不支持IE8及以下

  註:上面截圖的例子是在移動端,如果在PC端,要考慮 IE8 兼容的話,還需要用到 IE濾鏡

    如果你想要黑色透明背景,background-color:rgb(0,0,0,0.4);

    技術分享圖片

  下面附上一個demo供大家測試

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"
/> <title>背景透明,文字不透明</title> <style> * { padding: 0; margin: 0; } .container { width: 100%; height: 400px; background: url(‘https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542221600416&di=a1cf40bf75fea932e6f273bcfcbf6162&imgtype=0&src=http%3A%2F%2Fi1.img.969g.com%2Fpub%2Fimgx2018%2F05%2F15%2F503_232836_39b3e.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(255,255,255,0.4); } .demo p { color: #000; font-size: 18px; font-weight: 600; } </style> </head> <body> <div class="container"> <div class="demo"> <p>草薙金VS八神</p> </div> </div> </body> </html>

  參考原文:http://www.php.cn/css-tutorial-404918.html

  

  

  

CSS3實現背景透明文字不透明