CSS에서 요소의 위치를 고정으로 정의하여 사용자의 뷰포트를 기준으로 요소를 렌더링할 수 있습니다. 위치 지정 방식이 고정된 요소는 스크롤해도 이동하지 않으며 CSS 위치 지정 속성(왼쪽, 오른쪽, 위, 아래)에 의해 위치가 지정됩니다.
예시
CSS 고정 위치 지정 방법의 예를 살펴보겠습니다. -
<!DOCTYPE html>
<html>
<head>
<style>
p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
div:first-child {
background-color: orange;
text-align: center;
}
div:last-child {
width: 250px;
height: 100px;
margin: auto;
background-color: turquoise;
position: absolute;
z-index: -1;
top:0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>What is ASP.NET?</div>
<p>ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites............</p>
<div>
</div>
</body>
</html> 출력
다음은 위의 코드에 대한 출력입니다 -

예시
위치 지정 방법의 또 다른 예를 살펴보겠습니다. -
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 2px double #a43356;
margin: 5px;
padding: 5px;
}
#d1 {
position: relative;
height: 10em;
}
#d2 {
position: absolute;
width: 20%;
bottom: 10px; /*relative to parent d1*/
}
#d3 {
position: fixed;
width: 30%;
top:10em; /*relative to viewport*/
}
</style>
</head>
<body>
<div id="d1">Android is an open source and Linux-based operating system for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies. <mark>relative</mark>
<div id="d2"><mark>absolute</mark></div>
<div id="d3"><mark>fixed</mark></div>
</div>
</body>
</html> 출력
다음은 위의 코드에 대한 출력입니다 -
