2012-10-31 50 views
2

我在這裏有一個小問題。我想以紅色div爲中心,並將兩個綠色div分配到右側。兩個右邊的div似乎下降了?用兩個右側花車在一個div中居中div

http://jsbin.com/ewihuh/1/edit

HTML

<div class="headertop"> 
<div class="centerblock">Centered</div> 
<div class="right1">right 1</div> 
<div class="right2">right 2</div> 
</div> 

CSS

.headertop { 
width:100%; 
height:30px; 
background:black; 
} 

.centerblock { 
color:white; 
text-align:center; 
background:red; 
width: 200px; 
margin: 0px auto; 
} 

.right1, .right2 { 
color:white; 
float:right; 
width:100px; 
background:green; 
} 
+1

包裝你'right1'和'right2'成'div'可以使生活更輕鬆:http://jsbin.com/ewihuh/3/edit – Passerby

+0

@Passerby完美。我怎麼能讓兩個右邊的花車彼此相鄰? – user1100603

回答

3

Live Demo

改變現在your html code有些變化css

的CSS

.headertop { 
    width:100%; 
    background:black; 
    text-align:center; 
} 
.centerblock { 
    color:white; 
    text-align:center; 
    background:red; 
    width: 200px; 
    margin:0 auto; 
} 
.right1, .right2{ 
    color:white; 
    float:right; 
    width:100px; 
    background:green; 
} 

HTML

<div class="headertop"> 
    <div class="right1">right 1</div> 
    <div class="right2">right 2</div> 
    <div class="centerblock">Centered</div> 
</div> 
+0

完美!爲什麼父母上的「overflow:hidden」,.headertop? – user1100603

+1

@ user1100603很抱歉刪除overflow:hidden; –

1

HTML

<div class="headertop"> 
    <div class="centerblock">Centered</div> 
    <div class="rights"> 
    <div class="right1">right 1</div> 
    <div class="right2">right 2</div> 
</div> 
</div> 

CSS

.headertop { 
    width:100%; 
    height:30px; 
    background:black; 
    text-align:center; 
    position:relative; 
    } 

.centerblock { 
    color:white; 
    text-align:center; 
    background:red; 
    width: 200px; 
    margin: 0 auto; 
} 

.rights { 
position:absolute; 
right:0; 
top:0; 
width:100px; 
} 

.right1, .right2 { 
color:white; 
width:50px; 
background:green; 
float:left; 
} 

DEMO:http://jsbin.com/ewihuh/7/edit