
w3.css
오버레이
CSS로 오버레이 효과를 만드는 방법을 알아보자.
오버레이 효과를 만드는 방법
1단계) HTML 추가
원하는 요소를 사용하여 문서 내부 어디든 배치하자.
<div id="overlay"></div>
2단계) CSS 추가
오버레이 요소 스타일 지정:
#overlay { position: fixed; /* Sit on top of the page content */ display: none; /* Hidden by default */ width: 100%; /* Full width (cover the whole page) */ height: 100%; /* Full height (cover the whole page) */ top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0,0,0,0.5); /* Black background with opacity */ z-index: 2; /* Specify a stack order in case you're using a different order for other elements */ cursor: pointer; /* Add a pointer on hover */ }
3단계) JavaScript 추가
오버레이 효과를 켜고 끄려면 JavaScript를 사용하자.
function on() { document.getElementById("overlay").style.display = "block"; } function off() { document.getElementById("overlay").style.display = "none"; }
기본 예시
예제 보기텍스트가 있는 오버레이 효과
오버레이 안에 원하는 것을 추가하고 원하는 곳에 배치한다.
이 예에서는 페이지 중앙에 텍스트를 추가한다.
예제
<style> #text{ position: absolute; top: 50%; left: 50%; font-size: 50px; color: white; transform: translate(-50%,-50%); -ms-transform: translate(-50%,-50%); } </style> <div id="overlay"> <div id="text">Overlay Text</div> </div>
기본 예시
예제 보기참고
W3C School - How TO - Overlay