2017-02-08 11 views
2

왜 변환 중심으로 배치하고 50 % 중심을 완벽하게 (상대 위치 부모를 사용하여) 50 % 오른쪽으로하지 않습니까?CSS 중심 지정 변환

동작 예 : 중앙하지 않는

span[class^="icon"] { 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%); 
} 

예 위로 는 (인해 - 마이너스 값) 50 % 좌측에

span[class^="icon"] { 
    position: absolute; 
    top: 50%; 
    right: 50%; 
    transform: translate(-50%, -50%); 
} 

답변

1

translateX(-50%) 때문에 이동 무언가 어느 어떤 것을 중심으로 left: 50%;과 쌍을 이룬다는 의미입니다.

right: 50%;을 사용하려면 translateX(50%)과 함께 사용하십시오.

* {margin:0;} 
 
span { 
 
    position: absolute; 
 
    top: 50%; right: 50%; 
 
    transform: translate(50%,-50%); 
 
    background: black; 
 
    color: white; 
 
} 
 

 
body:after, body:before { 
 
    content: ''; 
 
    position: absolute; 
 
    background: red; 
 
} 
 

 
body:after { 
 
    top: 50%; 
 
    left: 0; right: 0; 
 
    height: 1px; 
 
} 
 
body:before { 
 
    left: 50%; 
 
    top: 0; bottom: 0; 
 
    width: 1px; 
 
}
<span>center me</span>

+0

는'direction'의 RTL에 따라 또는 – Banzay

+0

LTR 위치의이 유형에 direction''의 역할을 명확히 자신의 대답을 남겨 주시기 @Banzay. 'left' 또는'right'를 사용할 때'right' 또는'left'를 지정하기 때문에 문서의'direction'이 무엇인지는 중요하지 않습니다 http://codepen.io/anon/pen/PWdrBG –

+0

더 많은 질문이었습니다. 고맙습니다 :) – Banzay