화면을 스크롤 해도 위치가 변경되지 않고 고정
됨.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
height: 1000vh;
}
div {
width: 200px;
height: 200px;
}
#first {
background-color: red;
}
#second {
position: fixed;
background-color: blue;
}
</style>
</head>
<body>
<div id = "first"></div>
<div id = "second"></div>
</body>
</html>
id = first인 box 바로 아래에 id = second인 box가 생기고(서로 같은 레이어에 있어서 겹치는 현상이 없음) , 스크롤을 내리면 second box의 위치는 고정
하지만 second에 top , left , bottom , right 등의 옵션을 주게 되면 위치가 무시되고 두 박스가 겹치게 됨.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
height: 1000vh;
}
div {
width: 200px;
height: 200px;
}
#first {
background-color: red;
}
#second {
top : 50px;
position: fixed;
background-color: blue;
}
</style>
</head>
<body>
<div id = "first"></div>
<div id = "second"></div>
</body>
</html>
position : relative;
옵션을 주면 top, bottom , left , right 옵션을 주어서 자신의 초기 위치를 기준으로 상대적으로 위치 변경 가능 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: 100vh;
height: 100vh;
}
div {
width: 100px;
height: 100px;
background-color: red;
}
#child {
position: relative;
top : -10px;
width: 40px;
height: 40px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<div id = "child"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: 100vh;
height: 100vh;
}
div {
width: 100px;
height: 100px;
background-color: red;
}
#child {
position: absolute;
top : 0px;
right: 0px;
width: 40px;
height: 40px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<div id = "child"></div>
</div>
</body>
</html>
id ="child"의 부모의 position : relative 옵션을 준 경우
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: 100vh;
height: 100vh;
}
div {
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
#child {
position: absolute;
top : 0px;
right: 0px;
width: 40px;
height: 40px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<div id = "child"></div>
</div>
</body>
</html>