๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
studies/Front-end

[CSS] position : relative, absolute +์ค‘์•™์ •๋ ฌํ•˜๊ธฐ

by Vada Kim 2020. 3. 31.
728x90
๋ฐ˜์‘ํ˜•
<!Doctype html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  right: 50px;
  top : 50px;
  position: absolute;
  background: red;
}
</style>
<body>

<h2>My First JavaScript Animation</h2>

<div id="container">
<div id="animate"></div>
</div>

</body>
</html>

 

 

<!Doctype html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  /*position: relative;*/
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  right: 50px;
  top : 50px;
  position: absolute;
  background: red;
}
</style>
<body>

<h2>My First JavaScript Animation</h2>

<div id="container">
<div id="animate"></div>
</div>

</body>
</html>
 

์ถœ์ฒ˜: https://www.w3schools.com/

์ถ”๊ฐ€๋กœ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๊ธ€: https://rgy0409.tistory.com/2951

 

โ€ป ์ฃผ์˜ํ•  ์ . absolute ์†์„ฑ์€ inline์ด ์•„๋‹Œ div์™€ ๊ฐ™์€ ๋ธ”๋Ÿญ element์— ์ง€์ •ํ•ด์•ผ ์ž‘๋™ํ•จ. ๋ฒ„ํŠผ์— absolute ์ง€์ •ํ•ด๋„ ์•ˆ๋จนํž˜.

 


 

absolute์†์„ฑ์—์„œ ์ค‘์•™์ •๋ ฌํ•˜๊ธฐ(CSS)

 

1. ์ˆ˜์ง๊ณผ ์ˆ˜ํ‰์— ๋Œ€ํ•ด ์ค‘์•™์ •๋ ฌ

position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);

 

2. ์ˆ˜์ง์— ๋Œ€ํ•ด ์ค‘์•™์ •๋ ฌ

top: 50%; 
transform: translate(-50%);

 

3. ์ˆ˜ํ‰์— ๋Œ€ํ•ด ์ค‘์•™์ •๋ ฌ

left: 50%; 
transform: translate(-50%);

 

 

728x90
๋ฐ˜์‘ํ˜•