代码之家  ›  专栏  ›  技术社区  ›  Jronny

Css结合div的技巧

  •  1
  • Jronny  · 技术社区  · 16 年前

    有没有办法把三个div连在一起?

    Hello
    <div class="mainContainer">
        <div class="LeftDiv"></div>
        <div class="CenterDiv">
            <input id="txtTest" type="text"/>
        </div>
        <div class="RightDiv"></div>
    </div>
    World!
    

    Hello<*LeftDiv*><*CenterDiv with the textbox*><*RightDiv*>World

    我尝试使用float:left-on-LeftDiv、CenterDiv和RightDiv,但是css也会影响主容器。我还需要设置LeftDiv和RightDiv 高度和宽度 在css上,但是没有浮动我就做不到。

    提前谢谢。

    编辑:添加问题-当LeftDiv、CenterDiv和RightDiv向左浮动时,为什么mainContainer会受到影响?我只想在不影响父div行为的情况下将三个内部div连接起来。。。


    带显示的div:内联块无法按预期工作。

    Hello
    <span class="mainContainer">
        <span class="LeftDiv"></span><span class="CenterDiv">
            <input id="txtTest" type="text"/>
        </span><span class="RightDiv"></span>
    </span>
    World!
    

    跨距之间也不应该有空格,因为大多数浏览器都会在跨距之间显示一个空格…=)

    (答复供日后参考)

    2 回复  |  直到 14 年前
        1
  •  1
  •   Evgeny    16 年前

    您可以使用显示内联块,这样在div之前就不会有“换行符”

    div.mainContainer, div.mainContainer div
    {
      display:-moz-inline-stack; /* for gecko */
      display: inline-block;
    }
    

    尝试 goog-inline-block 中定义的类 goog/demos/css/common.css

        2
  •  0
  •   Sam Becker    16 年前

    您可以像下面这样将“mainContainer”的所有子div向左浮动,然后您可以根据需要设置高度和宽度要求。

    #mainContainer > div
    {
        float:left;
    }
    #LeftDiv
    {
        height:100px;
        width:100px;
        background-color:red;
    }
    #RightDiv
    {
        height:100px;
        width:100px;
        background-color:green;
    }
    

    嗯,如果你的左、右容器只用于背景图像,你可以考虑只把它做成一个图像,然后把它应用到主容器,

    #mainContainer
    {
        background-image:url(theimage.jpg);
        padding-left:100px; /*The amount of the image to the left */
        padding-right:100px; /* the amount of the image to the right. */
    }