html - 无法在我的背景图像上使用不透明度
问题描述
只是试图使我的背景图像不透明但仍然能够正常查看文本并在其上书写,我找到了查看文本但无法在其上书写的方法,当它可以查看文本并在其上书写时,它只是一半的屏幕或背景图像并没有覆盖所有内容,所以我只是想看看我是否可以在这里找到一些帮助。
* { margin: 0; padding: 0; }
/* Centering */
body
{
text-align: center;
}
/* Title Size and Font */
#title
{
font-size: 40px;
font-family: Mv Boli, sans-serif, arial;
}
/* Description Size and Font */
#description
{
font-size: 20px;
font-family: Mv Boli, sans-serif, arial;
}
/* Background Image */
.background-image
{
position: fixed;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
opacity: 0.5;
z-index: -5000;
background-image: url("https://material.angular.io/assets/img/examples/shiba2.jpg");
background-repeat: no-repeat;
background-size: cover;
}
/* Survey Form Styling */
#survey-form
{
background: hsl(204, 94%, 65%);
max-width: 500px;
margin: 50px auto;
padding: 25px;
}
<body>
<h1 id="title">Survey Form for Tech</h1>
<p id="description">Help us improve your experience!</p>
<!-- Background Image -->
<div class="background-image"></div>
<!-- Survey Form -->
<form id="survey-form">
<div class="form-flow">
<label for="name" id="name-label">Name</label>
<input id="name" type="text" required="" placeholder="Enter your name">
</div>
</form>
</body>
一切都解决了,我有 2 个指向图像的 URL,所以它们会发生冲突。
解决方案
在这种情况下,您可以::after
为您的背景图像和不透明度设置伪元素。
* {
padding: 0;
margin: 0;
}
.background-image{
Position:fixed;
Top: 0;
Bottom: 0;
Width: 100%;
Height: 100%;
}
.background-image:after {
position: absolute;
content: '';
background: url('https://via.placeholder.com/150/0000FF/808080') no-repeat center center / cover;
width: 100%;
height: 100%;
opacity: 0.5;
}
#survey-form {
//background: hsl(204, 94%, 65%);
background: red;
padding: 0;
margin: 50px auto;
position: absolute;
z-index: 1;
width: 500px;
height: 300px;
left: 50%;
transform: translateX(-50%);
padding: 20px;
}
<div class="background-image">
<div id="survey-form">
<p>Text 1</p>
</div>
</div>
推荐阅读
- unit-testing - SQL Cover 中的行与语句覆盖率
- php - 如何创建多项选择下拉phpspreadsheet
- c# - 如何强制 ListView 在同一行显示来自 ObservableCollection 不同元素的两个不同数据
- html - 背景颜色的CSS边框问题
- python - Python:读取 csv 文件并绘图,但列与包含单词和数字的 column1 结合
- python-3.x - AssertionError:找不到类 NSProcessInfo
- javascript - 移动 Divs 随机 JS
- asp.net-core - ASP Core 3.1 将未经授权的用户重定向到自定义页面
- sql - 带有桥接表的 Power BI 报表
- javascript - D3 js反应类组件