【发布时间】:2021-10-01 16:19:22
【问题描述】:
我得到了以下代码:
.container {
color: white;
height: 80vh;
margin: 10vh 0 0 25vw;
position: relative;
width: 50vw;
}
.container::after {
content: "";
background: url('https://i.ibb.co/ngx7VSR/photo-2021-04-19-21-24-36.jpg');
background-position: center;
background-size: cover;
filter: brightness(.5);
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
z-index: -1;
}
.container_inside {
align-items: center;
display: flex;
flex-direction: column;
font-size: 1.5em;
height: calc(100% - 4em);
justify-content: center;
padding: 2em;
text-align: center;
width: calc(100% - 4em);
}
.small {
color: #e9e9e9;
font-size: .7em;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The background</title>
</head>
<body>
<div class="container">
<div class="container_inside">
<p>Any fool can write code that a computer can understand. Good programmers write code that humans can
understand.</p>
<p class="small">Martin Fowler</p>
</div>
</div>
</body>
</html>
这里有两个目标:
- 让它与
::after一起工作,而不是::before - CSS 中某处缺少一行代码,它的缺失会破坏一切。将缺少的属性添加到正确的位置,使其看起来符合预期。
我找到了缺失的代码行 (content: "";),并将 ::before 替换为 ::after,这就是我得到的地方:
.container::after {
content: "";
background: url('https://i.ibb.co/ngx7VSR/photo-2021-04-19-21-24-36.jpg');
background-position: center;
background-size: cover;
filter: brightness(.5);
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
z-index: -1;
}
我正在努力让背景图像扩展到它的全尺寸,否则任务就解决了。代码编辑器告诉我任务 2 是正确的,但由于某种原因,任务 1 未被接受,我认为它与图像大小有关,但我可能错了。
理想情况下应该是这样的:https://ucarecdn.com/d24c1329-c0a3-4bd6-948c-5cfd83466c86/
非常感谢您对此的任何帮助!
【问题讨论】:
-
如果你想要全宽,为什么要在容器上使用
width:50vw?
标签: css background-image pseudo-element