2. CSS

CSS position fixed 사용하기 2

도피디 2024. 11. 24. 15:10
반응형
<!DOCTYPE html>
<html lang="kr">
<head>
<title>The Nick Times</title>
<style>
body {
height: 1000vh;
margin: 20px;
}
div {
width: 300px;
height: 300px;
background-color: teal;
}
#different {
top: 5px;
left: 200px;
position: fixed;
background-color: wheat;
width: 350px;
}
</style>
</head>
<body>
<div></div>
<div id="different"></div>
</body>
</html>

 

top, left, right, bottom 이라는 프로퍼티 중에서

하나만 수정해도 

노란색 div는 초록색 div 위로 가게 되어 있다.

(더 이상 block이든 margin이든 뭐든 신경쓰지 않는다.)

 

 

 

 

 

결과

(스크롤 시 노란색 div는 그 자리에 고정되어 있음 / 완전히 다른 레이어가 된다)

반응형