CSS定位问题

发布于 2024-09-25 18:31:32 字数 202 浏览 0 评论 0原文

当然,我正在尝试用 HTML 构建一些东西。

然而,我在定位方面遇到了一些严重的问题!

我试图让网站中的元素居中,但如果不牺牲(以某种方式)控制(例如 z-index 和这些元素的宽度),我就无法做到这一点 - 在某些情况下,反之亦然。

理论上,我应该能够使某些东西居中,设置它的宽度,并设置它的 Z 索引。

有人可以帮忙吗?

I'm trying to, of course, build something in HTML.

However, I am running into some serious issues with positioning!

I'm trying to get elements in the website to be centered, but I cannot do that without sacrificing (somehow) controls such as z-index, and width on those elements -- In some cases, vice versa.

Theoretically, I should be able to make something be centered, set it's width, AND set it's Z index all at once.

Can anyone help?

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

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

发布评论

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

评论(1

南汐寒笙箫 2024-10-02 18:31:32

要将某些内容居中,请将其边距设置为auto。要设置对象宽度,请设置它的 width;要设置对象的 z-index,请将其位置设置为 relative 以保持其中心位置,并相应地设置它的 z-index。

div {
    width:40%;
    position:relative;
    z-index:5;
    margin:auto;
    background-color: red;
}

示例: http://jsfiddle.net/wDzkd/

To center something, set it's margins to auto. To set an objects width, set it's width, to set an object's z-index, set it's position to relative to maintain it's centreness, and set it's z-index accordingly.

div {
    width:40%;
    position:relative;
    z-index:5;
    margin:auto;
    background-color: red;
}

Example: http://jsfiddle.net/wDzkd/

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