2013-03-19 170 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>