【问题标题】:centering divs does not work as it should居中 div 不能正常工作
【发布时间】:2010-10-27 20:56:56
【问题描述】:

我一直在寻找解决方案,并尝试实施经常被建议的内容,但我无法将一个 div 水平居中于另一个 div 中。

对于我的 CMS,我想在页面底部显示最多四个信息块。所以,我试图在两个容器 div 中放置零到四个 div。四个 div 的宽度分别为可用宽度的 19%、33% 或 49%,具体取决于页面上显示的 div 数量。内容器应该在外容器内水平居中。因此,最多四个 div 的组当然应该在水平中心结束。由于内部容器与主要内容的宽度相同,加上其上方的两列居中,因此它应该垂直排列。外部容器占据整个页面宽度并具有背景图像。

我的代码现在如下:

<!-- BEGIN USER MODULES -->
<tr>
  <td align="left" valign="top">

    <?php if ( $this->countModules( 'user1 and user2' ) || $this->countModules( 'user1 and user3' ) || $this->countModules( 'user1 and user4' ) || $this->countModules( 'user2 and user3' ) || $this->countModules( 'user2 and user4' ) || $this->countModules( 'user3 and user4' ) ) : ?>
        <style type="text/css">#user1, #user2, #user3, #user4 { width:49%; }</style>
    <?php endif; ?>
    <?php if ( $this->countModules( 'user1 and user2 and user3' ) || $this->countModules( 'user1 and user2 and user4' ) || $this->countModules( 'user1 and user3 and user4' ) || $this->countModules( 'user2 and user3 and user4' ) ) : ?>
        <style type="text/css">#user1, #user2, #user3, #user4 { width:33%; }</style>
    <?php endif; ?>
    <?php if ( $this->countModules( 'user1 and user2 and user3 and user4' ) ) : ?>
        <style type="text/css">#user1, #user2, #user3, #user4 { width:19%; }</style>
    <?php endif; ?>

    <?php if ($this->countModules( 'user1 or user2 or user3 or user4' )) : ?><div id="wrap1234"><div id="user1234">
        <?php if($this->countModules('user1')) : ?><div id="user1" class="module_bc"><jdoc:include type="modules" name="user1" style="xhtml" /></div><?php endif; ?>
        <?php if($this->countModules('user2')) : ?><div id="user2" class="module_bc"><jdoc:include type="modules" name="user2" style="xhtml" /></div><?php endif; ?>
        <?php if($this->countModules('user3')) : ?><div id="user3" class="module_bc"><jdoc:include type="modules" name="user3" style="xhtml" /></div><?php endif; ?>
        <?php if($this->countModules('user4')) : ?><div id="user4" class="module_bc"><jdoc:include type="modules" name="user4" style="xhtml" /></div><?php endif; ?>
    </div><div class="clear"></div></div><?php endif; ?>

    </td>
</tr>

在我的样式表中我有这个:

#wrap1234 { background:transparent url(images/header_bg.png) no-repeat scroll 0 0; border-bottom:1px solid #444444; border-top:1px solid #444444; margin:25px 0 10px; padding:5px 0; text-align:center; align:center;}

#user1234 { width:1420px; margin-left:auto; margin-right:auto; }

#user1, #user2, #user3, #user4 { float:left; margin:5px 0; padding:5px 0; text-align:left; }

放置所有这些的表和正文如下,跳过直接分层线之外的所有内容:

<body><div id="wrapper_main"><center><table border="0" cellpadding="0" cellspacing="0" width="<?php echo $this->params->get('width'); ?>" id="main_table"><tbody> 

body 和 table 的 css 如下。使用 Firebug,我在其中找不到任何在关闭时会产生影响的东西。

html, body, form, fieldset{margin:0; padding:0;}
body {background:#222222 none repeat scroll 0 0; color:#777777; font-family:Helvetica,Tahoma,sans-serif; font-size:0.72em; height:100%; text-align:center;}
#wrapper_main {background:#FFFFFF url(images/wrapper_main_bg.gif) repeat-x scroll left top; border-bottom:2px solid #CCCCCC; padding-bottom:20px; position:relative; width:100%; z-index:1;}
td, div {font-size:100%;}

实际页面可在我的开发网站 jldev d o t joomlaloft.com 上找到。

如您所见,我已经给内部容器一个固定宽度以及左右边距自动,这通常被建议作为水平居中 div 的方式。但是,其中包含 div 的内部容器最终会左对齐。

这可以工作吗?或者,我应该尝试另一种方法,例如在最左边和最右边的信息块 div 上放置一个左右可变的边距?

我已经看到 stackoverflow 上有很多非常好的答案,所以我希望有人能够告诉我哪里出错了。因为它是我没有灵感......在此先感谢您提供的任何帮助!

顺便说一句,这一定是我见过的最直观、最实用的论坛之一!

【问题讨论】:

    标签: php css html


    【解决方案1】:

    内部居中的 div 不能比它包含的 div 宽。在我的浏览器中,您的网站是 1200 像素宽,#user1234 是 1420 像素;

    将#user1234 设置为宽度:90%;并且每个用户 div 到宽度:25%;当有 4 个 div 时。把溢出:隐藏;在 #user1234 上清除浮动。

    #user1234 div 似乎没有居中,因为 #user4 div 文本没有填满可用空间。放背景色:#c00;在#user1234 上查看它的边界在哪里。 (注意:你必须有 overflow:hidden 否则 #user1234 没有高度,你不会看到背景颜色)。

    Re: 之前的评论 - 在同一个标​​签上使用宽度和边距没有问题。

    【讨论】:

    • 我将投票给这个最佳答案,因为它指出了真正的问题:1420px 的宽度完全被错误计算了。将宽度设置为 1000 像素的正确数量后,一切正常。谢谢!还要感谢 ncallaway 的替代方案,否则这将是最好的答案。
    【解决方案2】:

    我不确定你为什么要使用 width:1420px 。请关注thisthis 站点以获取正确的css 布局教程。另一种选择是使用css grid

    我个人建议,将表格取出并切换到流体网格,这将最适合您的网站。它有一点学习曲线,但相信我,以后你会更快乐。在您的网站上完成了很多工作;尽管 CSS 可以改得更好。如果您需要即时帮助,CSS freenode 通常会提供很好的建议和批评。

    还有一点,不建议在同一个标​​签中同时使用width和margin

    【讨论】:

    • 感谢这些很棒的链接!尽管其他答案作为短期解决方案更实用,但这些 lihnks 让我大开眼界,并且非常适合我在下一阶段制作无表格网站的计划。
    【解决方案3】:

    当我想让一个 DIV 居中时,我所做的就是相对定位它。将 left 设置为 50% 并将 margin-left 设置为 div 宽度的 1/2 将使 div 在容器中完美居中。我过去使用过它,据我所知,它适用于大多数浏览器(IE6+ 和非 IE 浏览器)。虽然可能有更好的方法来做到这一点。

    #user1234 { position: relative; 
                left: 50%;
                margin-left: -710px;
                width:1420px;}
    

    【讨论】:

    • 这几乎对我有用,但我不得不将左边距设置得更低。直到艾米丽的回答,才终于明白,宽度设置完全算错了。使用正确的宽度,这是一个很好的选择。
    • 是的。艾米丽的回答真正触及了为什么你没有工作的核心。很高兴你得到了一些有用的东西!
    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2010-11-30
    • 2021-10-02
    • 2017-08-06
    • 2013-11-22
    • 1970-01-01
    • 2013-09-10
    • 2017-04-05
    相关资源
    最近更新 更多