gpt4 book ai didi

CSS - 仅放大背景(无叠加文本)

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:47 25 4
gpt4 key购买 nike

我正在尝试对背景图像进行放大效果。下面的代码代表一个带有图像背景的可点击元素。我遇到的问题是它放大了该 div 中的所有内容,包括子 div。我只想放大背景图像(保持覆盖文本完好无损)

<a class="outer-tile tile-text" href="#">
<div class="inner-tile d-flex flex-column align-items-start" style="background-image: url('https://picsum.photos/200/300')">
<div class="mb-auto p-2">
<span class="b-dark p-1">MyTag</span>
</div>
<div class="b-dark w-100 p-2">
<h3>My Title</h3>
<p>This is the content</p>
</div>
</div>
</a>


.outer-tile {
overflow: hidden;
height: 400px;
}

.inner-tile {
height: 100%;
width: 100%;
background-size: cover;
background-position: center;
transition: all 0.5s ease;
}

.inner-tile:hover {
transform: scale(1.2);
}

这是一个 codepen :

如何只放大背景而不放大文本?

最佳答案

您可以将背景图像放置在文本元素的兄弟元素上,而不是父元素。然后用positioning和z-index放在他们后面:

.outer-tile {
overflow: hidden;
height: 400px;
position:relative;
}

.tile-underlay {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
z-index:-1;
background-size: cover;
background-position: center;
transition: all 0.5s ease;
}

.outer-tile:hover .tile-underlay {
transform: scale(1.2);
}

<a class="outer-tile tile-text" href="#">
<div class="tile-underlay d-flex flex-column align-items-start" style="background-image: url('https://picsum.photos/200/300')"></div>
<div class="mb-auto p-2">
<span class="b-dark p-1">MyTag</span>
</div>
<div class="b-dark w-100 p-2">
<h3>My Title</h3>
<p>This is the content</p>
</div>

</a>

https://codepen.io/anon/pen/NLyYKY

关于CSS - 仅放大背景(无叠加文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com