스타일 위치 속성
예시
<div> 요소의 위치를 상대 위치에서 절대 위치로 변경합니다.
document.getElementById("myDIV").style.position = "absolute";
정의 및 사용
position 속성은 요소에 사용되는 위치 지정 방법의 유형(정적, 상대, 절대 또는 고정)을 설정하거나 반환합니다.
브라우저 지원
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
통사론
위치 속성을 반환합니다.
object.style.position
위치 속성을 설정합니다.
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
속성 값
Value | Description |
---|---|
static | Elements renders in order, as they appear in the document flow. This is default. |
absolute | The element is positioned relative to its first positioned (not static) ancestor element |
fixed | The element is positioned relative to the browser window |
relative | The element is positioned relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position |
sticky | The element is positioned based on the user's scroll position
A sticky element toggles between |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
기술적 세부 사항
기본값: | 공전 |
---|---|
반환 값: | 요소의 위치 유형을 나타내는 String |
CSS 버전 | CSS2 |
더 많은 예
예시
다른 위치 유형 사용:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
예시
<h2> 요소의 위치를 반환합니다.
alert(document.getElementById("myH2").style.position);
관련 페이지
CSS 튜토리얼: CSS 포지셔닝
CSS 참조: 위치 속성
❮ 스타일 개체