본문 바로가기
뒷북 정리 (국비 교육)/css

[css] step07. position

by 규글 2021. 12. 13.

step07.  - 01~02. position

	<style>
		body{
			border: 2px solid yellow;
			margin: 0;
		}
		.spacer{
			/* default position 속성의 값은 static 이다. */
			position: static;
			height: 300px;
			background-color: #999;
			border-bottom: 1px solid red;
		}
	</style>
</head>
<body>
<div class="spacer">div1</div>
<div class="spacer">div2</div>
<div class="spacer">div3</div>
<div class="spacer">div4</div>
<div class="spacer">div5</div>
</body>

 

 

 

 

 

 

 

 

 

  1. <div>는 문서 기준으로는 항상 고정된 위치에 있다. (static) 때문에 scroll 한다고 <div>의 위치가 바뀌는 것이 아니다. 하지만 static이 아닌 relative, absolute, fixed 속성으로 변화를 줄 수 있다.

    	<style>
    		.box{
    			width: 100px;
    			height: 100px;
    			border: 1px solid red;
    		}
    		.spacer{
    			height: 500px;
    			background-color: #999;
    		}
    		#one{
    			/* 원래 배치되어야 하는 위치를 기준으로 배치 */
    			position: relative;
    			/* 위에서 떨어진 거리 (y좌표) */
    			top: 150px;
    			/* 왼쪽에서 떨어진 거리 (x좌표)*/
    			left: 150px;
    		}
    		/* absolute 는 특정 요소를 기준으로 절대 좌표에 배치 하고자 할때 사용한다. (특정 요소는 부모 요소인데 부모요소의 position 속성이 명시적으로 지정 되어 있어야 부모 요소를 기준으로 절대 좌표에 배치할 수 있다. */
    		#two{
    			/* 부모 요소가 position 속성이 없으면 body 기준으로 절대 좌표에 배치 */
    			position: absolute;
    			top: 100px;
    			left: 100px;
    		}
    		/* fixed 는 window 기준으로 절대 좌표에 배치 할때 사용한다.
    		따라서 스크롤바를 움직여도 window 기준으로 항상 같은 위치에 존재한다. */
    		#three{
    			position: fixed;
    			top: 100px;
    			left: 200px;
    		}
    		#four{
    			/* 부모 요소가 position 속성이 없으면 body 기준으로 절대 좌표에 배치 */
    			position: absolute;
    			top: 100px;
    			left: 100px;
    		}
    	</style>
    </head>
    <body>
    <div class="box">static</div>
    <div class="box" id="one">relative
    	<div class="box" id="four">test</div>
    </div>
    <div class="spacer"></div>	
    <div class="box" id="two">absolute</div>
    <div class="box">너는 어디?</div>
    <div class="box" id="three">fixed</div>
    </body>​









  2. relative : 글자 그대로 상대적인 위치를 말한다. 원래 들어가야 할 위치를 기준으로 한 상대적인 위치이다.
  3. absolute : 글자 그대로 절대적인 위치를 말한다. 특정 요소를 기준으로 한 절대 좌표에 배치하고자 할 때 사용한다. 만약 부모 요소에 position 정보가 없다면 body를 기준으로 한 절대 좌표에 배치된다.
  4. fixed : 보고 있는 화면을 기준으로 절대 좌표에 배치한다. 때문에 화면 스크롤을 해도, 해당 객체는 화면 상에서 같은 위치에 계속 배치되어 있다.

 

 

step07. example.1~3

	<style>
		.wrapper{
			position: relative;
			width: 500px;
			height: 500px;
			margin: 0 auto;
			border: 1px solid red
		}
		.box{
			position: absolute;
			width: 50px;
			height: 50px;
			background-color: yellow;
		}
		#one{
			top: 100px;
			left: 100px;
		}
		#two{
			top: 100px;
			right: 100px;
		}
		#three{
			bottom: 100px;
			left: 100px;
		}
		#four{
			bottom: 100px;
			right: 100px;
		}
		#center{
			top: 225px;
			left: 225px;
		}
		/* .wrapper div 전체 덮기 */
		#test{
			background-color: #000;
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			color: #fff;
			opacity: 0.5; /* 투명도 ( 0 ~ 1.0) */
		}
	</style>
</head>
<body>
<h3>특정 요소 안에서 원하는 위치에 배치</h3>
<div class="wrapper">
	<div class="box" id="one">one</div>
	<div class="box" id="two">two</div>
	<div class="box" id="three">three</div>
	<div class="box" id="four">four</div>
	<div class="box" id="center">center</div>
	<div id="test">test</div>
</div>	
</body>
  1. 부모 요소의 position이 relative이고 자식 요소의 position이 absolute인 경우에, 부모 요소의 위치를 기준으로 한 좌표를 새롭게 absolute 좌표계로 하여 position이 정해진다.
  2. 이전 예제에서는 left와 top만 사용했으나 right와 bottom도 사용할 수 있다.
  3. margin이 0에 auto로 하게 되면 top과 bottom은 0, left와 right가 auto라서 가운데 정렬이 된다.
  4. id가 test인 객체의 css를 보면 상하좌우에 0을 기입했는데, 이 경우는 자식요소이지만 부모 요소만큼 size가 커진다.
    뒤 객체들을 덮어버리는 것을 'back drop'이라고 했다.

    	<style>
    		.wrapper{
    			width: 200px;
    			border: 1px solid red;
    			position: relative;
    		}
    		#myInput{
    			width: 200px;
    		}
    		.wrapper button{
    			position: absolute;
    			top: 0;
    			right: 0;
    		}
    	</style>
    </head>
    <body>
    <div class="wrapper">
    	<input type="text" id="myInput"/>
    	<button>검색</button>
    </div>	
    </body>​
     
    	<style>
    		/* window 전체를 cover 하기 */
    		.backdrop{
    			background-color: #000;
    			position: fixed;
    			top: 0;
    			left: 0;
    			right: 0;
    			bottom: 0;
    			opacity: 0.3;
    			/* 
    				none 은 안보이게 하기 
    				display: none | block | inline | inline-block ;
    			*/
    			display: none; 
    		}
    		p{
    			background-color: #0f0;
    		}
    	</style>
    </head>
    <body>
    <h1>position : fixed 응용</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est fuga incidunt aperiam exercitationem autem. Veniam odio pariatur doloremque. Eligendi eaque accusamus repellendus officiis placeat voluptates. Ab autem, ex enim expedita!</p>	
    <button id="showBtn">눌러보셈</button>
    <div class="backdrop"></div>
    <script>
    	document.querySelector("#showBtn")
    	.addEventListener("click", function(){
    		// .backdrop 의 인라인 css 조작
    		document.querySelector(".backdrop")
    		.style.display="block";
    	});
    
    	document.querySelector(".backdrop")
    	.addEventListener("click", function(){
    		this.style.display="none";
    	});
    </script>
    </body>​
     
  5.  javascript를 이용해서 css를 변경할 수 있다.
    (ex - 객체의 참조값.style.display="block";)
  6. display 자리에 <style> 영역에 작성하던 대로 'background-color' 라고 작성하면 연산으로 인식하여 에러가 난다. 때문에 backgroundColor 라고 camel case로 작성한다.
  7. css도 가중치가 있어서 우선 순위라는 것이 존재하는데, inline css가 우선한다는 것을 알고 넘어간다. 곧 배울 예정이다.

 

'뒷북 정리 (국비 교육) > css' 카테고리의 다른 글

[css] step09. float  (0) 2021.12.13
[css] step08. pseudo element  (0) 2021.12.13
[css] step06. pseudo class  (0) 2021.12.13
[css] step03~05. margin / padding / border  (0) 2021.12.13
[css] step02. selector (선택자)  (0) 2021.12.13

댓글