使用 CSS3 在透明方框中顯示文字


opacity 屬性用於建立透明文字框。使用 background 屬性在透明框中設定影像。由於我們設定了不透明度,它將使文字框中出現的文字和影像透明。

建立一個 div 容器

設定了 div 容器 −

<div class="transparentBox">
   <div class="transparentText">
      <p>This is some random text inside the transparent box</p>
   </div>
</div>

透明文字框

在上述父級 div 中,設定了文字框子級 div −

<div class="transparentText">
   <p>This is some random text inside the transparent box</p>
</div>

設定一張圖片

對於透明框,使用 background 屬性設定影像。影像源設定在 url() 中 −

transparentBox {
   background: url("https://tutorialspoint.tw/images/dbms_icon.svg") repeat;
   border: 2px solid black;
}

透明文字

在框中,設定不透明度屬性以實現透明 −

.transparentText {
   margin: 30px;
   background-color: #ffffff;
   border: 1px solid black;
   opacity: 0.6;
}

示例

要使用 CSS3 設定透明框中的文字,程式碼如下 −

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .transparentBox {
         background: url("https://tutorialspoint.tw/images/dbms_icon.svg") repeat;
         border: 2px solid black;
      }
      .transparentText {
         margin: 30px;
         background-color: #ffffff;
         border: 1px solid black;
         opacity: 0.6;
      }
      .transparentText p {
         margin: 5%;
         font-weight: bolder;
         color: #000000;
         font-size: 20px;
      }
   </style>
</head>
<body>
   <h1>Text in transparent box example</h1>
   <div class="transparentBox">
      <div class="transparentText">
         <p>This is some random text inside the transparent box</p>
      </div>
   </div>
</body>
</html>

更新於:2023 年 12 月 27 日

1K+ 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.