2014-03-25 4 views
1

나는 div 요소를 가운데에 놓고 다른 하나는 같은 수직 정렬로 오른쪽에 div 요소를 배치하고 싶습니다. 두 요소를 중심에 두지 않고 진행하는 방법을 모르겠습니다.하나의 가운데에 두 개의 div 요소 배치하기

여기 내 코드입니다.

<div class="container"> 
<div class="center"></div> 
<div class="right"></div> 
</div> 

.center { 
    width: 100px; 
    height: 100px; 
    margin: auto; 
    background-color: red; 
} 

.right { 
    width: 100px; 
    height: 100px; 
    background-color: blue; 
} 

http://jsfiddle.net/KWsnh/

답변

0

당신은 calc을 사용할 수 있습니다 이를 위해 :

FIDDLE

.container{ 
    text-align:center; 
    position: relative; 
} 
.center { 
    width: 100px; 
    height: 100px; 
    background-color: red; 
    display: inline-block; 
} 

.right { 
    width: 100px; 
    height: 100px; 
    background-color: blue; 
    position:absolute; 
    top:0; 
    left: calc(50% + 50px); // (100% - 100px)/2 + 100px (offset) = 50% + 50px 
} 

PS : Browser support for calc 요즘 아주 좋다.

0

Demo Fiddle

HTML

<div class="container"> 
    <div class='vcenter'> 
     <div class="center"></div> 
     <div class="right"></div> 
    </div> 
</div> 

CSS

html, body { 
    margin:0; 
    padding:0; 
    height:100%; 
    width:100%; 
} 
body { 
    display:table; 
} 
.container { 
    display:table-cell; 
    vertical-align:middle; 
} 
.center { 
    width: 100px; 
    height: 100px; 
    margin: 0 auto; 
    background-color: red; 
} 
.vcenter { 
    display:block; 
    width:100%; 
    position:relative; 
} 
.right { 
    width: 100px; 
    height: 100px; 
    background-color: blue; 
    position:absolute; 
    right:0; 
    top:0; 
} 
관련 문제