简体   繁体   English

如何在 hover 上的 div 内容的边距之间显示 div 作为叠加层

[英]How to display a div as overlay in between the margin of the div contents on hover

I'm working on adding an overlay to my div contents.我正在为我的 div 内容添加一个叠加层。

As part of this, I have created a.main div with 4 boxes (A, B, C, D).作为其中的一部分,我创建了一个包含 4 个框(A、B、C、D)的主分区。

When I mouse hover on the main div I wanted to show a div as overlay on top of my main div like this, as shown in the image.当我将 hover 鼠标放在主 div 上时,我想像这样在我的主 div 之上显示一个 div,如图所示。

I have given a margin: 10px between my contents in the main div (all flex layout).我在主 div(所有 flex 布局)中的内容之间留了一个边距:10px。

How can I show the overlay div in the margin spaces between my divs?如何在我的 div 之间的边距空间中显示覆盖 div?

Attaching my JSFiddle example:附上我的 JSFiddle 示例:

 .main { display: flex; flex-direction: row; flex-wrap: wrap; justify-content: space-between; margin-top: 20px; align-items: center; padding-bottom: 10px; padding-top: 10px; }.main div { display: flex; align-items: center; justify-content: center; cursor: pointer; flex: 1 1 30%; height: 100px; margin: 10px; box-sizing: border-box; } /*.main:hover { background: red; } */.overlay-main { display: none; }.main:hover +.overlay-main { display: flex; background: red; position: absolute; height: 500px; width: 500px; }
 <div class="main"> <div style="background-color:coral;">A</div> <div style="background-color:lightblue;">B</div> <div style="background-color:khaki;">C</div> <div style="background-color:pink;">D</div> </div> <div class="overlay-main"> <span>add row</span> <span>add column</span> </div>

https://jsfiddle.net/deepakpookkote/ears2zyg/14/ https://jsfiddle.net/deepakpookkote/ears2zyg/14/

the attached image is my expected result on hover所附图片是我在 hover 上的预期结果

在此处输入图像描述

You can do that using: :before and :after :您可以使用:before:after来做到这一点:

div.main:hover div::before{
  content: "add column";
  display: block;
  position: absolute;
  left: 0;
  top: 50%;
  height: 100%;
  transform: translate(-100%, -50%);
  word-break: keep-all;
  white-space: nowrap;
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-size: 0.8em;
  letter-spacing: -2px;
}

div.main:hover div::after
{
  content: "add row";
  display: block;
  position: absolute;
  top: 100%;
  width: 100%;
  font-size: 0.9em;
  text-align: center;
}

Here is a working Example: jsFiddle这是一个工作示例: jsFiddle

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

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