简体   繁体   English

有没有办法在没有背景图像或 position: relative 的情况下将文本放在图像上?

[英]Is there a way to place text over image without background-image, or position: relative?

My situation is that I don't want it as a background-image because I'm changing the opacity on hover, and so that it didn't affect the text as well I made them separate elements.我的情况是我不希望它作为背景图像,因为我正在更改 hover 的不透明度,因此它也不会影响文本,我将它们设为单独的元素。 However, moving them using position: relative;但是,使用 position 移动它们:相对; means that I am left with blank space where their supposed to be in page flow.意味着我留下了他们应该在页面流中的空白空间。

Any help would be greatly appreciated.任何帮助将不胜感激。

 .flex-container { display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-evenly; width: 100%; } div { width: 24%; min-width: 400px; height: 25em; transition: 200ms; margin: 1rem 0; } div.background-img { width: 100%; height: 100%; } #img-1 { background-image: url("imgs/tokyo.jpg"); } /* Later in code */.background-img:hover { transition: 200ms; filter: brightness(50%); outline: 1px white solid; outline-offset: -20px; border-radius: 5%; } div.hidden-text { transition: 200ms; position: relative; bottom: 100%; color: white; opacity: 0; pointer-events: none; } div:hover.hidden-text { transition: 200ms; color: rgb(255, 255, 255, 1); opacity: 1; }
 <section class="flex-container"> <div> <h1>Holiday Destinations</h1> <span>This is a collection of locations that I either would love to visit, or loved visiting.</span> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quaerat totam eum asperiores assumenda, amet rem molestiae pariatur at nulla sequi debitis itaque voluptatem modi corrupti fugiat sed quod dolores perspiciatis maiores in. Tenetur molestiae eum cupiditate obcaecati consectetur, Culpa consequatur doloribus? non voluptates quia commodi dolore mollitia cupiditate modi dolorem. </p> </div> <div> <div class="background-img" id="img-1"></div> <h2 class="hidden-text">Tokyo - Japan</h2> <p class="hidden-text">Lorem ipsum dolor sit amet.</p> </div> </section>

Website here --> https://dex-hewitt.github.io/holiday-destinations网站在这里 --> https://dex-hewitt.github.io/holiday-destinations

Thank you!谢谢!

Also please let me know if I've written question poorly or not included any necessary parts I am new to stackoverflow.另外,如果我写的问题很差,或者没有包含任何必要的部分,请告诉我我是 stackoverflow 的新手。

You can approach it this way if you don't want it to be as a background.如果您不希望它作为背景,您可以通过这种方式处理它。 Let me know让我知道

 .flex-container { display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-evenly; width: 100%; } div { width: 24%; min-width: 400px; height: 25em; transition: 200ms; margin: 1rem 0; } div.background-img { width: 100%; height: 100%; } div img { width: 500px; height: 500px; object-fit: cover; } /* Later in code */.background-img:hover { transition: 200ms; filter: brightness(50%); outline: 1px white solid; outline-offset: -20px; border-radius: 5%; } div.hidden-text { transition: 200ms; position: relative; bottom: 100%; color: white; opacity: 0; pointer-events: none; text-align: center; } div:hover.hidden-text { transition: 200ms; color: rgb(255, 255, 255, 1); opacity: 1; }
 <section class="flex-container"> <div> <h1>Holiday Destinations</h1> <span>This is a collection of locations that I either would love to visit, or loved visiting.</span> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quaerat totam eum asperiores assumenda, amet rem molestiae pariatur at nulla sequi debitis itaque voluptatem modi corrupti fugiat sed quod dolores perspiciatis maiores in. Tenetur molestiae eum cupiditate obcaecati consectetur, Culpa consequatur doloribus? non voluptates quia commodi dolore mollitia cupiditate modi dolorem: </p> </div> <div> <img class="background-img" src="https.//thumbs.dreamstime.com/b/shinjuku-gyoen-national-garden-tokyo-japan-large-park-shibuya-lake-asian-153188061.jpg" > <h2 class="hidden-text">Tokyo - Japan</h2> <p class="hidden-text">Lorem ipsum dolor sit amet.</p> </div> </section>

Sorry all, a typo mistake on my end.对不起,我的错字错误。 While I thought it was the texts border box causing the blank space, I found it to be a missing "/" symbol on a lower div.虽然我认为是文本边框框导致了空白,但我发现它是下部 div 上缺少的“/”符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM