Computer >> 컴퓨터 >  >> 프로그램 작성 >> CSS

CSS 위치 지정 방법 이해

<시간/>

위치 지정 요소의 경우 CSS에는 다음과 같은 위치 지정 방법이 있습니다. -

CSS의 상대 위치

상대 위치 지정을 사용하면 요소가 일반 위치를 기준으로 배치됩니다. 이를 위해 position:relative를 사용하십시오.

예시

<!DOCTYPE html>
<html>
<head>
<style>
div.demo {
   position: relative;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   left: 50px;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<p>This is demo text.</p>
<div class="demo">
position: relative;
</div>
<p>This is another demo text.</p>
</body>
</html>

출력

CSS 위치 지정 방법 이해

CSS의 절대 위치

절대 위치 지정을 사용하면 가장 가까운 위치에 있는 조상을 기준으로 요소가 배치됩니다.

예시

<!DOCTYPE html>
<html>
<head>
<style>
div.demo1 {
   position: relative;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   width: 600px;
   height: 200px;
}
div.demo2 {
   position: absolute;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   top: 50px;
   right: 0;
   width: 300px;
   height: 100px;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<div class="demo1">position: relative;
<div class="demo2">
position: absolute;
</div>
</div>
<p>This is another demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
</body>
</html>

출력

CSS 위치 지정 방법 이해

CSS의 고정 위치

position:fixed; 페이지를 스크롤해도 같은 위치에 유지됩니다.

예시

<!DOCTYPE html>
<html>
<head>
<style>
div.demo1 {
   position: relative;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   width: 600px;
   height: 200px;
}
div.demo2 {
   position: absolute;
   color: white;
   background-color: orange;
   border: 2px dashed blue;
   top: 50px;
   right: 0;
   width: 300px;
   height: 100px;
}
div.demo3 {
   position: fixed;
   bottom: 0;
   right: 20px;
   width: 500px;
   border: 3px solid orange;
   color: blue;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<div class="demo1">position: relative;
<div class="demo2">
position: absolute;
</div>
<div class="demo3">
position: fixed;
</div>
</div>
<p>This is another demo text.</p>
</body>
</html>

출력

CSS 위치 지정 방법 이해