CSS 将一个 div 直接放在另一个 div 上

发布于 2024-11-26 05:09:42 字数 643 浏览 0 评论 0原文

例如,我有一个 div,里面有一些东西,用户可以选择单击“x”来表示“这不适用于我”。

我不想删除 div,而是想在它上面播放一个半透明的 div。

我首先使用一些复杂的 javascript 来确定相关 div 的大小和位置,并在其上创建一个新的。脚本给出的尺寸和位置在我看来大致正确,但重叠 div 被放置在错误的位置。

然后我意识到(可能)有一种更简单的方法可以做到这一点。

我将一个带有“blackout”类的 div 放在我想要黑掉的 div 中。 blackout css 类的可见性设置为隐藏,因此 javascript 会在需要时将其设置为可见。

我遇到的问题是,即使使用这种方法,我似乎也无法让它精确填充父 div 的矩形。

我让

.blackout
{
  position: absolute;

  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;

  background-color: black;

  opacity: 0.5;
  filter: alpha(opacity = 50);
}

This 填满了整个屏幕,而不仅仅是父 div。

我需要更改什么才能使其仅填充父 div?

I have a div which has some stuff in it, and the user has the option of clicking an 'x' to say "This is not applicable to me", for example.

Rather than delete the div, I want to play a translucent div on top of it.

I started off with some complicated javascript to determine the size and location of my div in question, and create a new one on top of it. The script was giving a size and location which looked approximately right to my eye, but the overlap div was being put in the wrong spot.

Then I realised that there is (probably) a much simpler way to do this.

I put a div with class "blackout" inside the div I want to black out. The blackout css class has a visibility set to hidden, so javascript will set that to visible when needed.

The issue I'm having is that even with this method, I can't seem to get it to precisely fill the rectangle the parent div has.

I had

.blackout
{
  position: absolute;

  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;

  background-color: black;

  opacity: 0.5;
  filter: alpha(opacity = 50);
}

This filled up the whole screen rather than just the parent div.

What do I need to change to make it fill the parent div only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奢华的一滴泪 2024-12-03 05:09:42

这填满了整个屏幕,而不仅仅是父 div。

我需要更改什么才能使其仅填充父级 div?

您需要将 position:relative 添加到父 div 中。

这会将父 div 设置为 .blackout 的“包含块”:

如果position属性的值为absolute,则包含
块是最近的定位祖先,换句话说,最近的
position 属性具有以下值之一的祖先:absolute
固定,或相对

在这里阅读更多内容:http://reference.sitepoint.com/css/containingblock

This filled up the whole screen rather than just the parent div.

What do I need to change to make it fill the parent div only?

You need to add position: relative to the parent div.

That will set the parent div as the "containing block" for .blackout:

If the value of the position property is absolute, the containing
block is the nearest positioned ancestor—in other words, the nearest
ancestor whose position property has one of the values absolute,
fixed, or relative.

Read more here: http://reference.sitepoint.com/css/containingblock

最近可好 2024-12-03 05:09:42

使用“position:absolute”将其相对于下一个“position:relative”div 定位。如果没有一套,那么它将使用身体。

您需要使父 div CSS 包含“position:relative”

Using "position:absolute" positions it in relation to the next "position:relative" div. If there isn't one set then it will use the body.

You need to make the parent div CSS contain "position:relative"

小耗子 2024-12-03 05:09:42

在父 div 的 CSS 上:

overflow: hidden;

应该可以工作

On the parent div's CSS:

overflow: hidden;

should work

汹涌人海 2024-12-03 05:09:42

position:relative 添加到父级 div,overflow:hidden 只会隐藏父级 div 的外部

Add position: relative to the parent div, overflow: hidden will only hide the outside of your parent's div

何止钟意 2024-12-03 05:09:42

position:absolute; 更改为 position:relative;

Change position: absolute; to position: relative;

2024-12-03 05:09:42

将子

widthheight 设置为 100% 并删除无用的标记。

http://jsfiddle.net/MvPHj/

Set the child <div> width and height to be 100% and remove useless markup.

http://jsfiddle.net/MvPHj/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文