본문 바로가기
: ) Web/웹 스터디

[JS] Alert 박스 만들기

by miiinn 2024. 1. 4.

▶ 버튼 클릭 시 alert박스가 뜨고 닫기 버튼 클릭시 다시 alert박스가 사라진다.

 

[핵심 코드]


        <div id="box" class="alert-box">Alert!
            <button onclick="document.getElementById('box').style.display = 'none';">닫기</button>
        </div>
        <button onclick="document.getElementById('box').style.display = 'block';">버튼</button>

 

[HTML]

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <link rel="stylesheet" href="main.css">
    </head>
    <body>

        <div id="box" class="alert-box">Alert!
            <button onclick="document.getElementById('box').style.display = 'none';">닫기</button>
        </div>
        <button onclick="document.getElementById('box').style.display = 'block';">버튼</button>

    </body>
</html>

 

[CSS]

.alert-box {
    background-color: skyblue;
    padding: 20px;
    color: white;
    border-radius: 5px;
    display: none;
}