如何使用 Internet Explorer 将 div 居中?

发布于 2024-12-05 13:17:20 字数 291 浏览 6 评论 0原文

我在使用 Internet Explorer 和居中时遇到问题,我的问题是如何在没有 CENTER 标签的情况下使 div 居中 它可以在所有浏览器中工作, 因为我正在使用

margin:auto;

它适用于所有浏览器 但它在 Internet Explorer 中不起作用 我正在寻找适用于所有浏览器的东西 有人告诉我要放在

text-align: center;

正文中,但所有文字都放在中间,

所以我该怎么做?

i have trouble with internet explorer and centering , my question is how can i centering a div without the CENTER tag
and it got to work in all the browsers ,
because i was using

margin:auto;

it works in all borwsers
but it does not work in internet explorer
i'm looking for something that will work in all the browsers
someone told me to put

text-align: center;

to the body but than all the text has go to the middle

so how can i do that?

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

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

发布评论

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

评论(5

地狱即天堂 2024-12-12 13:17:20

你很接近了。使用下面的 CSS:

margin:0 auto;

这是一个工作的 jsFiddle。请注意,我给了 div 一个固定的宽度。

You're close. Use the CSS below:

margin:0 auto;

Here's a working jsFiddle. Note that I gave the div a fixed width.

缘字诀 2024-12-12 13:17:20

您需要指定宽度和边距:

div.center { width:980px; margin:0px auto; }

示例 HTML:

<html>
<head></head>
<body>
<div class="center">CONTENT</div>
</body>
</html>

You need to specify a width as well as margin:

div.center { width:980px; margin:0px auto; }

Example HTML:

<html>
<head></head>
<body>
<div class="center">CONTENT</div>
</body>
</html>
思念绕指尖 2024-12-12 13:17:20

IE 对自动边距的支持不稳定(即,怪异模式下的不同行为)。这应该适用于几乎所有情况:

CSS:

.container {
    /* for IE */
    text-align: center;
}
#the-div {
    /* reset text-align */
    text-align: left;
    /* for "good" browsers */
    margin: 0 auto;
}

HTML:

<div class="container">
    <div id="the-div">centered content</div>
</div>

IE has spotty support for auto margins (i.e., different behavior in quirks mode). This should work in pretty much all cases though:

CSS:

.container {
    /* for IE */
    text-align: center;
}
#the-div {
    /* reset text-align */
    text-align: left;
    /* for "good" browsers */
    margin: 0 auto;
}

HTML:

<div class="container">
    <div id="the-div">centered content</div>
</div>
妳是的陽光 2024-12-12 13:17:20

是的,这在 IE 中完美运行。

.container {
    /* for IE */
    text-align: center;
}
#the-div {
    /* reset text-align */
    text-align: left;
    /* for "good" browsers */
    margin: 0 auto;
}

Yes this works perfectly in IE.

.container {
    /* for IE */
    text-align: center;
}
#the-div {
    /* reset text-align */
    text-align: left;
    /* for "good" browsers */
    margin: 0 auto;
}
梦里的微风 2024-12-12 13:17:20

你的CSS很接近,你的问题不是浏览器。简单的修复方法是更改​​内部 div 类。

您的位置绝对为 Left 25%...使用 Left 50%,它会自动更正。

完全删除 left: 50% 并添加 margin: 0 auto;正如之前在另一个答案中所指出的。

Your CSS is close, the your issue isn't the browser. The simple fix is to change the inner div class.

You have your position absolute with Left 25%... Use Left 50% and it will autocorrect.

Remove the left: 50% entirely and add margin: 0 auto; as previously noted on another answer.

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