如何使用 CSS 建立具有透明背景文字的影像?
我們可以輕鬆建立帶有透明背景文字的影像。使用 CSS 背景屬性設定不透明度為 0.5 的黑色背景。使用 position 屬性對齊它,並將其放置在影像的底部。在那裡放置文字,即影像上的透明背景文字。讓我們看看如何使用 HTML 和 CSS 建立帶有透明背景文字的影像。
設定影像容器
設定影像容器,在其內顯示影像和一些內容。此內容進入底部 −
<div class="imageContainer">
<img src="https://tutorialspoint.tw/static/images/home/hero-banner.png">
<div class="imageContent">
<h1>A Man</h1>
<h3>A man sitting on a chair with a laptop.</h3>
</div>
</div>
對齊影像容器
為設定影像容器,使用具有值 relative 的 position 屬性 −
.imageContainer {
display: inline-block;
position: relative;
}
對齊影像上的內容
使用具有值 absolute 的 position 屬性對齊影像上的內容。內容在影像底部;因此,將 bottom 屬性設定為值 0 −
.imageContent {
position: absolute;
bottom: 0;
background: rgba(29, 6, 43, 0.5);
color: #f1f1f1;
width: 100%;
padding: 20px;
}
示例
以下是如何使用 CSS 建立帶有透明背景文字的影像的程式碼 −
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial;
font-size: 17px;
}
.imageContainer {
display: inline-block;
position: relative;
}
.imageContent {
position: absolute;
bottom: 0;
background: rgba(29, 6, 43, 0.5);
color: #f1f1f1;
width: 100%;
padding: 20px;
}
</style>
</head>
<body>
<h2>Image with Transparent Text</h2>
<div class="imageContainer">
<img src="https://tutorialspoint.tw/static/images/home/hero-banner.png">
<div class="imageContent">
<h1>A Man</h1>
<h3>A man sitting on a chair with a laptop.</h3>
</div>
</div>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP