2013-03-19 166 views
0

我有定位2 div div内的问题。我想有2个div彼此相邻,但我不知道如何。这里是我的htmlCSS定位div彼此相邻

<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,但它拧得更多。 :/

回答

2

有几种方法可以做到这一点。最传统的法是使用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

1

使用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>