【问题标题】:Element alignment in G Chrome with absolute positionG Chrome 中的元素对齐与绝对位置
【发布时间】:2011-03-07 08:06:05
【问题描述】:

我希望红色方块与绿色方块完全重叠。 在 FF、IE 和 Opera 中一切正常,但在 Chrome 红色方块中向左对齐,而不是居中。 此外,我只能影响#parent div 及其子元素,因为我制作了一个书签,我无法更改其他页面元素。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
    <title></title>
    <style type="text/css">
        #center { text-align: center; }
        #parent {
            position: absolute;
            display: inline;
        }
        #nested {
            background-color: #c00;
            width: 280px !important;
            height: 210px !important;
        }

    </style>
</head>
<body>
    <div id="center">
        <div id="parent"><div id="nested"></div></div>
        <div style="display: inline-block; width: 280px; height: 210px; background-color: #0c0;"></div>
    </div>
</body>
</html>

请指教!

当我详细说明 chrome 属性时,我发现当您将 #parent 的位置设置为绝对时,chrome 会强制显示阻塞。如何使其内联?

【问题讨论】:

    标签: html css google-chrome alignment css-position


    【解决方案1】:

    我认为这是视觉上您所要求的:

    • 我只更改了 #parent 和 #nested 的 CSS。
    • 在 IE7/IE8、Firefox、Chrome、Opera、Safari 中看起来一样。

    Live Demo

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
        <title></title>
        <style type="text/css">
            #center { text-align: center; }
            #parent {
                position: absolute;
                left: 0;
                right: 0;
                /*top: 0;*/
                text-align: center
            }
            #nested {
                background-color: #c00;
                width: 280px !important;
                height: 210px !important;
                display: inline-block
            }
    
        </style>
    </head>
    <body>
        <div id="center">
            <div id="parent"><div id="nested"></div></div>
            <div style="display: inline-block; width: 280px; height: 210px; background-color: #0c0;"></div>
        </div>
    </body>
    </html>
    

    【讨论】:

    • @Andrey:你之前没有提到过。您在#parent 上使用position: absolute - 当然它在表格单元格内“不起作用”。您可以通过在#center 上添加position: relative 来修复它,但根据您的说法,我不能这样做。
    • 这就是问题所在——我可以针对具体情况解决这个问题,但是如果你不知道上下文就很难做到(
    • @Andrey:所以更彻底地解释你在做什么。你有可用的 JavaScript 来定位东西吗?您正在制作的这个作品可以放置在 any 元素内的页面上anywhere 吗?您是否考虑过使用iframe?没有更多信息,我无法提供更多帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2017-04-28
    • 2016-04-18
    • 1970-01-01
    • 2013-09-05
    • 2013-08-30
    相关资源
    最近更新 更多