2017-02-19 1 views
0

그래서 div가 있고 클래스로 위치를 변경하려면이 코드를 사용하십시오.클래스의 위치를 ​​변경하는 방법

<h1 class="test">Hi!</h1> 

<script> 
x = document.getElementsByClassName('test') 

x[0].style.top = 500; 

</script> 

그러나 위치가 동일하게 유지되는 이유는 무엇입니까? 모든

답변

1

첫째, 기본 요소 positionstatic이며, 그 속성을 시각적으로 적용 할 예정되지 않은 top, right, bottom, leftCSS에 어떤 조작을 의미한다.

relative absolute fixed

당신은 에 대해 자세히 알아볼 수 있습니다 : 당신이 top 속성을 변경하고자한다면

그래서, 당신은 또한 다음 값 중 하나와 요소 position을 변경해야 CSS 위치 in Developer Mozilla

그래서 다음 코드를 사용하십시오. 다음 :

<h1 class="test">Hi!</h1> 

<script> 
    x = document.getElementsByClassName('test') 

    x[0].style.position = 'relative'; // Changed the position property to relative. 
    x[0].style.top = '500px'; // Must be wrapped in quotes, and append the measurement unit in the value in this case the 'px'. 

</script> 

또한 CSS 측정에 대해 자세히 알아볼 수 있습니다 단위here

관련 문제