css - 以可变宽度水平居中 div 在 IE 中不起作用

标签 css internet-explorer html

我练习了将没有宽度的 div 居中,并找到了适用于所有常见浏览器的解决方案。但是当我把这个解决方案放到真实的页面样式中时,它在 IE 中不起作用。

在 IE、Chrome 和 Firefox 中完美运行的实践解决方案如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Centerbox</title>
    <link type="text/css" rel="stylesheet" href="centerbox.css" media="all" />
</head>

<body>
    <div class="centerbox-outer">
        <div class="centerbox-inner">
            <p>Sample content that is not fixed width</p>
            <p>Some more content</p>
            <form>
                <input type="text" name="sampleinput" />
                <input type="submit" name="go" />
            </form>
        </div>
    </div>
</body>
</html>

中心框.css

div.centerbox-outer{
    text-align: center;
    margin: 0 auto;
}

div.centerbox-inner{
    text-align: justify;
    background-color: gray;
    display: inline-block;
}

不支持 IE 的页面在这里:[链接已删除]

有人知道我在那里缺少什么吗?

最佳答案

进行了一些研究并找到了使用相对位置的合适解决方案。这似乎在常用浏览器中完美运行。

样式如下:

div.centerbox-outer{
    margin: 0 auto;
    float: left;
    left: 50%;
    position: relative;
}

div.centerbox-inner{
    text-align: justify;
    background-color: gray;
    float: left;
    position: relative;
    right: 50%;
}

关于css - 以可变宽度水平居中 div 在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3206735/

相关文章:

html - CSS样式在不同大小的窗口中不对称

html - 如何动态填充列中的空白区域

Jquery:如果和其他

html - 计算重叠的 rgba 值

javascript - 如何重用 JQuery 的结果进行 JavaScript 文本匹配?

javascript - 重新创建 "Styles"Office 功能区的下拉区域?

asp.net-mvc - IE 11 + SignalR 不工作

javascript - IE : "Function expected"start() '&lt;input onclick=" 中的错误 ;"... '

javascript - chrome 的自定义滚动条

html - Z-index 适用于 Firefox 和 IE,但不适用于 Chrome