多div居中

发布于 2024-11-16 12:55:57 字数 1561 浏览 4 评论 0原文

我看过很多关于此的帖子,我不知道为什么它对我不起作用。

.firstCell
{
        float:left;
        width:40%;
    text-align:right;
    align:right;
    border:1px solid white;
}

.cell
{
    float:left;
    width:auto;
    align:left;
    text-align:left;
    border:1px solid white;
}

.newRow
{
    clear:both;
    width:100%;
}

.container
{
        width:100%;
        background-color:#DEEFF8;
        margin:0px auto;
}

所以,我基本上有这个:

<div class="container">
*Within this I have sections of like a form*
        <div style="width:400px;border:1px solid black;">
            <div class='firstCell'>File Name:</div>
            <div class='cell'><html:text property="fileName" /></div>
            <div class='cell' style='color:red;'>(Max 50 character)</div>
            <div class='newRow'></div>
            <div class='firstCell'>Copy Book Name:</div>
                <div class='cell'><html:text property="copyBookName"/></div>
            <div class='newRow'></div>
                <div class='firstCell'><html:button property="populateFields" value="Populate Fields" onclick="showFields();"/></div>
            <div class='newRow'></div>
            <div class='newRow'></div>

        </div>

        *So this is one section, what I would like to happen is to position my form elements in this and then have it all be centered on the main div



</div>

I've looked at so many posts on this I'm at a loss as to why its not working for me.

.firstCell
{
        float:left;
        width:40%;
    text-align:right;
    align:right;
    border:1px solid white;
}

.cell
{
    float:left;
    width:auto;
    align:left;
    text-align:left;
    border:1px solid white;
}

.newRow
{
    clear:both;
    width:100%;
}

.container
{
        width:100%;
        background-color:#DEEFF8;
        margin:0px auto;
}

So, I basically have this:

<div class="container">
*Within this I have sections of like a form*
        <div style="width:400px;border:1px solid black;">
            <div class='firstCell'>File Name:</div>
            <div class='cell'><html:text property="fileName" /></div>
            <div class='cell' style='color:red;'>(Max 50 character)</div>
            <div class='newRow'></div>
            <div class='firstCell'>Copy Book Name:</div>
                <div class='cell'><html:text property="copyBookName"/></div>
            <div class='newRow'></div>
                <div class='firstCell'><html:button property="populateFields" value="Populate Fields" onclick="showFields();"/></div>
            <div class='newRow'></div>
            <div class='newRow'></div>

        </div>

        *So this is one section, what I would like to happen is to position my form elements in this and then have it all be centered on the main div



</div>

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

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

发布评论

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

评论(2

盛夏已如深秋| 2024-11-23 12:55:57

你的主 .container 有 100% 宽度,如果你将它居中也没关系,它仍然会从最左边开始绘制。其中负责左对齐框的 div 没有 id/class,并且您没有对其进行对齐。从技术上讲,您的主容器是居中的,但其中的所有内容都是左对齐的。

Your main .container has 100% width, it doesn't matter if you center it, it will still start drawing it from the very left. The div inside of it that's responsible for the left-aligned box has no id/class, and you're doing no aligning on it. Technically your main container is centered, but everything inside of it is left-aligned.

摇划花蜜的午后 2024-11-23 12:55:57

你的意思是这样吗? http://jsfiddle.net/36ujG/

有点难以理解你想要居中的内容。我所做的只是将 margin: 0 auto 添加到容器的子 div 中。

Do you mean like this? http://jsfiddle.net/36ujG/

It's a little hard to understand what you are trying to center. All I did was added margin: 0 auto to the child div of container.

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