gpt4 book ai didi

HTML z-index 和定位

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:44 25 4
gpt4 key购买 nike

我正在尝试制作一个基于 html/css 的扑克程序,目前我正在尝试弄清楚如何将筹码放在 table 上或将我的聊天窗口移动到 table 上。

我的代码在 index.html 中

<body>
<div id="content">
<div id="table">
<div id="boardImage"><img src="./img/poker_table_new.png" /></div>
</div>

<div id="chat">
<textarea id="chatBox"></textarea>
<input id="message" type="text">
<input id="sendButton" type="submit" value="Send">
</div>
</div>

在 CSS 中

#content {
position:relative;
z-index:-1;
}
#table {
position:inherit;
z-index:-1;
}

#chat {
z-index:2;
position:inherit;
left:500px;

}
#boardImage {
position:inherit;
z-index:-1;
}
#chatBox {
position:inherit;
z-index:2;
width: 300px;
height: 300px;

}

基本上,我试图将聊天框移动到我的桌面图片上,但它并没有移动到它上面。

我不确定我是否应该为我的扑克程序使用相对位置?还是我正确使用了 z-index?我必须为所有的 div 设置 z-index 吗?

目前在 html 顶部有一个扑克牌 table ,当我向下滚动时,有我的聊天框,但它们应该在彼此上。

我有太多相同的代码吗?太多写 z-index?和定位我的扑克程序,我必须用像素移动所有东西吗?哪种定位方式最好?稍后我必须开始移动 table 上的筹码和卡片等。

图片:enter image description here

最佳答案

z-index 仅适用于定位元素。 position: relative 位置根据该元素最初的位置,但您没有指定 topleft 之类的内容,因此它保留在同一个地方——所以是的,你使用的都是正确的!

我在编写 CSS 时的方法是编写有效的内容(您已在此处完成),然后在我重复的任何地方“分解”。您有很多 position: inherit;,因此您可以将它们组合成一个规则。例如:

.inherit-position {
position: inherit;
}

然后您可以从 CSS 中删除重复的 position 样式,并只为这些 div 元素提供一个额外的类,如下所示:

<div id="chat" class="inherit-position"></div>

简而言之,您在这里根本没有做错任何事情——但是您的 CSS 可以通过发现任何重复并尝试消除它来稍微改进。

关于HTML z-index 和定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com