자바 스크립트로 div를 세로로 가운데에 놓으려고합니다. 텍스트가 바뀌기 때문에 고정 된 높이를 사용할 수 없습니다.자바 스크립트로 세로 중심 div (가변 높이)
Jquery없이이 작업을 수행하고 싶습니다.
#box2 {
width: 100%;
height: 100%;
position:relative;
background-color: orange;
}
#informationBox {
padding: 0.5em;
background-color: #fff;
border-radius: 12pt;
border: solid black 3pt;
max-width: 683px;
margin: 0 auto;
text-align: center;
}
자바 스크립트 :
var container = document.getElementById("#box2");
var inner = document.getElementById("#informationBox");
var inHeight = inner.offsetHeight;
container.style.height=(window.innerHeight);
container.style.width=window.innerWidth;
var conHeight=container.offsetHeight;
inner.style.marginTop=((conHeight-inHeight)/2);
어떤 도움이 될 것이다 당신은 거의 그것을 가지고, 당신은 단지 몇 가지를 변경해야합니다 :)
http://jsfiddle.net/tmyie/EttZQ/
실제 사이트에서 컨테이너는 상대적으로 설정됩니다. 이렇게하면 jsfiddle에서 코드가 분리됩니다. 컨테이너가 상대적인 경우 솔루션이 있습니까? –