【问题标题】:CSS positioning divs next to each otherCSS定位div彼此相邻
【发布时间】:2013-03-20 02:08:06
【问题描述】:

我在 div 中放置 2 个 div 时遇到问题。我想有 2 个 div 并排,但我不知道怎么做。这是我的html

<div id="game">
    <div id="choice" onmouseover="npcRoll()">
        <p>Chosse your weapon!</p>
        <button id="rock" onClick="choose(1)">Rock</button>
        <button id="paper" onClick="choose(2)">Paper</button>
        <button id="scissors" onClick="choose(3)">Scissors</button>
        <p>You chose <span id="userChoice">none</span>!</p>
    </div>
    <div id="confirm">    
    </div>
</div>

这是我的 CSS:

#choice {
    border: 2px solid #87231C;
    border-radius: 12px;
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
    background-color: #FF5A51;
    width: 350px;
}
#game {
    border: 2px solid #fff;
    border-radius: 15px;
    background-color: white;
    width: 500px;
    margin: 0 auto;
}
#confirm {
    border: 2px solid #00008B;
    border-radius: 12px;
    border-top-left-radius: 0px;
    border-bottom-left-radius: 0px;
    background-color: #1E90FF;
    width: 142px;
    height: 100px;
}
body {
    background-color: #DFEFF0;
    text-align: center;
}

button {
    font-size: 22px;
    border: 2px solid #87231C;
    border-radius: 100px;
    width: 100px;
    height: 100px;
    color: #FF5A51;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
    padding-top: 36px;
}

button:active {
    font-size: 22px;
    border: 2px solid #328505;
    color: #32A505;
    border-radius: 100px;
    width: 100px;
    height: 100px;
    padding-top: 36px;
}

您可以在这里查看它的外观。 http://jsfiddle.net/VcU7J/

感谢您的帮助!

编辑:我还尝试将浮动元素添加到 CSS 中,但它搞砸了。 :/

【问题讨论】:

    标签: css


    【解决方案1】:

    有几种方法可以做到这一点。最传统的是使用 CSS 的 float 属性你的两个 div:

    CSS

    #choice {
        border: 2px solid #87231C;
        border-radius: 12px;
        border-top-right-radius: 0px;
        border-bottom-right-radius: 0px;
        background-color: #FF5A51;
        width: 350px;
    
         float:left; 
    }
    #game {
        border: 2px solid #fff;
        border-radius: 15px;
        background-color: white;
        width: 500px;
        margin: 0 auto;
    
        /* this is needed to make sure your container background 
          "contains" your floated divs */ 
        overflow:auto; 
    
    }
    #confirm {
        border: 2px solid #00008B;
        border-radius: 12px;
        border-top-left-radius: 0px;
        border-bottom-left-radius: 0px;
        background-color: #1E90FF;
        width: 142px;
        height: 100px;
    
        float:left      
    }
    

    fiddle

    更多关于花车的信息here

    【讨论】:

      【解决方案2】:

      使用css floats 将 div 放置在彼此旁边,但不要忘记在完成后清除浮动。

      #game {
          float:left;
      }
      #confirm {
          float: right;
      }
      .clear {
          clear: both;
      }
      

      然后html看起来像:

      <div id="game">
          <div id="choice" onmouseover="npcRoll()">
              <p>Chosse your weapon!</p>
              <button id="rock" onClick="choose(1)">Rock</button>
              <button id="paper" onClick="choose(2)">Paper</button>
              <button id="scissors" onClick="choose(3)">Scissors</button>
              <p>You chose <span id="userChoice">none</span>!</p>
          </div>
          <div id="confirm">    
          </div>
          <div class="clear"></div>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        相关资源
        最近更新 更多