2012-10-31 43 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