简体   繁体   English

Position 文本居中在图像顶部

[英]Position text centered on top of image

I'm creating a navigation bar where, initially, you have images.我正在创建一个导航栏,最初你有图像。 Upon hovering over them, the image's opacity goes down to 0 and text's goes up to 1. So the text should replace the image.将鼠标悬停在它们上方时,图像的不透明度会下降到 0,而文本的不透明度会上升到 1。所以文本应该替换图像。 I have done so, but the problem I'm trying to solve is to position text on top of the image centered both horizontally and vertically.我已经这样做了,但我要解决的问题是在图像顶部的 position 文本水平和垂直居中。

Only the first one of the list has CSS applied, thought I would figure out on one element first and then apply solution to the rest later.只有列表中的第一个应用了 CSS,以为我会先找出一个元素,然后再将解决方案应用于 rest。

Here's my codepen: https://codepen.io/Cryptooo/pen/NWGwQwv这是我的代码笔: https://codepen.io/Cryptooo/pen/NWGwQwv

 .menu-icons li { margin-top: 50%; }.menu-icons { display: flex; flex-direction: column; align-items: center; }.menu-center { display: flex; flex-direction: column; align-items: center; } #home { height: 25px; width: 27px; }.menu-center #home, .menu-center.home-txt { transition: opacity 0.3s ease-in-out; }.menu-center.home-txt { position: absolute; color: #08efcc; opacity: 0; }.menu-center:hover #home { opacity: 0; }.menu-center:hover.home-txt { opacity: 1; }
 <nav class="menu-section"> <ul class="menu-icons"> <div class="menu-center"> <li> <a href="#"><img id="home" src="/Images/Home.png"></a> </li> <div class="home-txt">Home</div> </div> <div class="menu-center"> <li> <a href="#"><img id="about" src="/Images/About.png"></a> </li> <div class="about-txt">About</div> </div> <div class="menu-center"> <li> <a href="#"><img id="skills" src="/Images/Skills.png"></a> </li> <div class="skills-txt">Skills</div> </div> <div class="menu-center"> <li> <a href="#"><img id="work" src="/Images/Work.png"></a> </li> <div class="work-txt">Work</div> </div> <div class="menu-center"> <li> <a href="#"><img id="contact" src="/Images/Contact.png"></a> </li> <div class="contact-txt">Contact</div> </div> </ul> </nav>

Here is how it works for me, assuming I understand your question!假设我理解您的问题,这就是它对我的工作方式!

 .menu-icons li { margin-top: 50%; list-style-type: none; }.menu-icons { display: flex; flex-direction: column; align-items: center; }.menu-center { position: relative; display: flex; flex-direction: column; align-items: center; } #home { height: 25px; width: 27px; }.menu-center #home, .menu-center.home-txt { transition: opacity 0.3s ease-in-out; }.menu-center.home-txt { position: absolute; left: 0; width: 100%; top: 0; height: 100%; display: flex; align-items: center; justify-content: center; color: #08efcc; opacity: 0; }.menu-center:hover #home { opacity: 0; }.menu-center:hover.home-txt { opacity: 1; }
 <nav class="menu-section"> <ul class="menu-icons"> <div class="menu-center"> <li> <a href="#"><img id="home" src="/Images/Home.png"></a> </li> <div class="home-txt">Home</div> </div> <div class="menu-center"> <li> <a href="#"><img id="about" src="/Images/About.png"></a> </li> <div class="about-txt">About</div> </div> <div class="menu-center"> <li> <a href="#"><img id="skills" src="/Images/Skills.png"></a> </li> <div class="skills-txt">Skills</div> </div> <div class="menu-center"> <li> <a href="#"><img id="work" src="/Images/Work.png"></a> </li> <div class="work-txt">Work</div> </div> <div class="menu-center"> <li> <a href="#"><img id="contact" src="/Images/Contact.png"></a> </li> <div class="contact-txt">Contact</div> </div> </ul> </nav>

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

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