简体   繁体   English

叠加出现在Hover上 - 纯CSS

[英]Overlay appear on Hover - Pure CSS

As you can see in the following snippet, I have a category-box and on the hover event of that box I want appear a overlay. 正如您在下面的代码片段中看到的,我有一个category-box ,在该category-boxhover事件中,我想要显示一个叠加层。

Here is the CODEPEN 这是CODEPEN

The problem here is, I want the overlay to start from the middle of the category-box and appear. 这里的问题是,我希望叠加层从category-box的中间开始并出现。 But here as you can see it start from somewhere in the middle. 但是在这里,你可以看到它从中间的某个地方开始。

 .category-box { padding: 15px 0; position: relative; } .category-img { overflow: hidden; } .category-img img { width: 100%; object-fit: cover; object-position: center; -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box .category-img img { height: 500px; } .category-content { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #000000; color: white; padding: 25px; text-align: center; min-width: 260px; min-height: 125px; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; justify-content: center; align-items: center; z-index: 1; } .category-overlay { position: absolute; left: 0; height: 0; width: 0; background-color: rgba(0, 0, 0, 0.45); top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box:hover .category-overlay { top: 15px; transform: none; left: 0; } .category-box:hover .category-overlay { height: calc(100% - 30px); width: 100%; } .category-box:hover .category-img img { -webkit-transform: scale(1.2); -moz-transform: scale(1.2); -ms-transform: scale(1.2); transform: scale(1.2); } 
 <div class="category-box category-big-box"> <div class="category-img"> <img src="https://timeless-dubai.com/wp-content/uploads/2018/07/i3.jpg" alt="Category Image"> </div> <div class="category-content"> some content </div> <div class="category-overlay"></div> </div> 

You can just remove transform: translate(-50%, -50%); 你可以删除transform: translate(-50%, -50%); on .category-overlay : .category-overlay

 .category-box { padding: 15px 0; position: relative; } .category-img { overflow: hidden; } .category-img img { width: 100%; object-fit: cover; object-position: center; -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box .category-img img { height: 500px; } .category-content { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #000000; color: white; padding: 25px; text-align: center; min-width: 260px; min-height: 125px; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; justify-content: center; align-items: center; z-index: 1; } .category-overlay { position: absolute; left: 0; height: 0; width: 0; background-color: rgba(0, 0, 0, 0.45); top: 50%; left: 50%; /*-webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%);*/ -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box:hover .category-overlay { top: 15px; transform: none; left: 0; } .category-box:hover .category-overlay { height: calc(100% - 30px); width: 100%; } .category-box:hover .category-img img { -webkit-transform: scale(1.2); -moz-transform: scale(1.2); -ms-transform: scale(1.2); transform: scale(1.2); } 
 <div class="category-box category-big-box"> <div class="category-img"> <img src="https://timeless-dubai.com/wp-content/uploads/2018/07/i3.jpg" alt="Category Image"> </div> <div class="category-content"> some content </div> <div class="category-overlay"></div> </div> 

If you want to be 100% sure that an absolute positioned element is centered, you can use these properties: 如果您想100%确定绝对定位元素是居中的,您可以使用以下属性:

/* Center horizontally */
right: 0;
left: 0;
margin: auto;

/* Center vertically */
top: 50%;
transform: translateY(-50%);

 #container { width: 100px; height: 100px; background: red; position: relative; } #child { width: 30px; height: 30px; background: yellow; position: absolute; right: 0; left: 0; margin: auto; top: 50%; transform: translateY(-50%) } 
 <div id="container"> <div id="child"></div> </div> 

in .category-overlay use this .category-overlay使用此功能

  top: calc(50% + 88px);
  left: calc(50% + 155px);

The 88px and 155px are half of the box size so it should be the middle of your content. 88px155px是盒子大小的一半,所以它应该是你内容的中间位置。

 .category-box { padding: 15px 0; position: relative; } .category-img { overflow: hidden; } .category-img img { width: 100%; object-fit: cover; object-position: center; -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box .category-img img { height: 500px; } .category-content { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #000000; color: white; padding: 25px; text-align: center; min-width: 260px; min-height: 125px; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; justify-content: center; align-items: center; z-index: 1; } .category-overlay { position: absolute; left: 0; height: 0; width: 0; background-color: rgba(0, 0, 0, 0.45); top: calc(50% + 88px); left: calc(50% + 155px); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); -webkit-transition: ease 0.4s all; -moz-transition: ease 0.4s all; -ms-transition: ease 0.4s all; transition: ease 0.4s all; } .category-big-box:hover .category-overlay { top: 15px; transform: none; left: 0; } .category-box:hover .category-overlay { height: calc(100% - 45px); width: 100%; } .category-box:hover .category-img img { -webkit-transform: scale(1.2); -moz-transform: scale(1.2); -ms-transform: scale(1.2); transform: scale(1.2); } 
 <div class="category-box category-big-box"> <div class="category-img"> <img src="https://timeless-dubai.com/wp-content/uploads/2018/07/i3.jpg" alt="Category Image"> </div> <div class="category-content"> some content </div> <div class="category-overlay"></div> </div> 

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

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