Front-End/CSS

[CSS] Ex17_fixed.html

이뮨01 2023. 6. 12. 13:40

보통 웹 사이트의 상단바로 이동하는 버튼을 만들 때 사용하는 position의 속성값 fixed를 배우다.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        body{
            height: 2000px;
        }
        #parent{
            width: 200px;
            height: 600px;
            background-color: gray;
         }
        .box{
            width: 200px;
            height: 200px;
        }
        .child3{
            width: 70px;
            height: 70px;
            border-radius: 50%;
        }
        .child1{
            background-color: #ff4949;
            
        }
        .child2{
            background-color: #f9ca24;
        }
        .child3{
            background-color: #0984e3;
            text-align: center;
            /* 한 문장일 경우, (line-height: 해당 요소의 높이) 
               하면 수직 방향 가운데정렬 */
            line-height: 70px;
            font-size: 20px;
            font-weight: bold;
            position: fixed;
            right: 0px;
            bottom: 0px;

        }
        .child3>a{
            color: white;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div id="parent">
        <div class="box child1"></div>
        <div class="box child2"></div>
        <div class="box child3">
            <a href="#parent">Top</a>
        </div>
    </div>
</body>
</html>
결과

파란색 영역의 Top 글씨를 누르면 웹 사이트의 상단 위치로 이동한다. 
웹 사이트에서 위아래로 이동해도 파란색 영역은 화면의 우측하단에 고정(fixed)된다.
텍스트를 영역의 중앙으로 이동하기 위해서 가로의 중앙, 세로의 중앙으로 이동시키기 위해(.child3 참고)