본문 바로가기
웹 퍼블리싱 - WEB PUBLISHING

[웹퍼블리싱] html/css 맨 위로가기 버튼 만들기

by 설쁘 2023. 3. 26.

 

 

웹 사이트나 홈페이지에서 스크롤이 길어질 때, 상단또는 원하는 구간으로 갈 수 있는 버튼이 있습니다.

hmtl 안에서 작성할 수 있는 코드를 정리해볼게요.

 

<!DCOTYPE html>
<html>
	<head>
    	<title>top_button</title>
        
        <style>
            #btn a {
                text-decoration: none;
                color: black;
            }

            #btn {
                background-color: #00AE68;
                display: block;
                position: relative;
                float: left;
                width: 80px;
                padding: 0;
                margin: 10px 20px 10px 0;
                font-weight: 600;
                text-align: center;
                line-height: 35px;
                color: #FFF;
                border-radius: 5px;
                transition: all 0.2s ;
                box-shadow: 0px 0px 0px 0px #21825B;
            }

            #btn:hover {
                background: #21825B;
            }

        </style>
    </head>

	<body>
    
      <div id="1">첫 번째 섹션</div>
        <ul>
            <li>aaa</li>
            <li>bbb</li>
            <li>ccc</li>
        </ul>
      <div id="2">두 번째 섹션</div>
        <ul>
            <li>aaa</li>
            <li>bbb</li>
            <li>ccc</li>
        </ul>
      <div id="3">세 번째 섹션</div>
        <ul>
            <li>aaa</li>
            <li>bbb</li>
            <li>ccc</li>
        </ul>
      
      <button id="btn"><a href="#1"> 이동하기 </a></button>
      
    </body>
</html>

 

1. 원하는 섹션에 id (또는 class)를 지정합니다.

2. 버튼에 a href 링크를 이용하여, id (또는 class)를 호출합니다.

2-1. 원하는 섹션의 id를 호출하면, 버튼을 클릭시 지정한 섹션으로 이동합니다.

 

>>> 결과물