whatisthis?
practice - css ) ๋ชจ๋ฌ ํ์ ์ฐฝ ๋ง๋ค๊ธฐ (JSโ) ๋ณธ๋ฌธ
WEB STUDY/HTML,CSS
practice - css ) ๋ชจ๋ฌ ํ์ ์ฐฝ ๋ง๋ค๊ธฐ (JSโ)
thisisyjin 2022. 3. 12. 11:17Modal pop-up
- without JS
๐ ๊ธฐ๋ณธ ์๋ฆฌ
- <a href="#popup"> ์ ํด์
ํด๋น div#popup์ ๊ฐ์ํด๋์ค :target์ ์ค๋ค! - display block๊ณผ none ํ ๊ธ๋๊ฒ
- ๋ซ๊ธฐ๋ฒํผ์๋ <a href="#">๋ก ๋ค์ ๋๋๋ฆฐ๋ค.
HTML
<a href="#popup" class="opene">Open</a>
<div id="popup">
<h2>modal</h2>
<a href="#">๋ซ๊ธฐ</a>
</div>
CSS
#popup {
display: none;
}
#popup:target {
display: block;
}
๐ป ์์ฉ ์์
์ ์๋ฆฌ๋ฅผ ์ด์ฉํด์ ์์ฉ ์์ ๋ฅผ ๋ง๋ค์ด๋ณด์.
div#popup์ class๋ ์ถ๊ฐํด์ ์คํ์ผ ํ๋๋ฅผ ๊ฐ์ง๊ณ ์ฌ๋ฌ๊ฐ์ ๋ชจ๋ฌ์ ๋ง๋ค ์ ์๊ฒ.
๋ํ, display: block์ด์๋๊ณ display: flex๋ฅผ ํด์ ๊ฐ์ด๋ฐ์ ๋ ฌ๋ ๋ค์ผ๋ก ํ ์ ์๊ฒ.
index.html
<!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>Modal Popup</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<a href="#popupA" class="opener">Open Modal A</a>
<a href="#popupB" class="opener">Open Modal B</a>
<a href="#popupC" class="opener">Open Modal C</a>
<div id="popupA" class="layer">
<div class="box">
This is popup A
<a href="#" class="close">close</a>
</div>
</div>
<div id="popupB" class="layer">
<div class="box">
This is popup B
<a href="#" class="close">close</a>
</div>
</div>
<div id="popupC" class="layer">
<div class="box">
This is popup C
<a href="#" class="close">close</a>
</div>
</div>
</body>
</html>
style.css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100vh;
background-color: #eaa;
}
a {
text-decoration: none;
color: inherit;
}
.opener {
display: block;
text-align: center;
background-color: #fff;
width: 300px;
margin: 10px auto;
padding: 10px 30px;
border-radius: 30px;
transition: all 0.2s ease-in-out;
}
.opener:hover {
background-color: #666;
color: #fff;
}
.layer {
display: none;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
position: fixed;
left: 0;
right: 0;
bottom: 0;
top: 0;
}
.layer .box {
position: relative;
padding: 20px 20px 60px;
margin: 20px;
width: 500px;
background-color: #fff;
border-radius: 10px;
}
.layer .box .close {
position: absolute;
bottom: 20px;
right: 20px;
background-color: #09f;
color: #fff;
text-align: center;
padding: 5px 15px;
border-radius: 5px;
font-size: 13px;
transition: all 0.2s ease-in-out;
}
.layer .box .close:hover {
transform: scale(1.1);
}
.layer:target {
display: flex;
animation: open 0.5s;
}
@keyframes open {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
result
- โจ ์ค์ตํ๊ธฐ
์ฐ์ ๋ชจ๋ฌ์ฐฝ์ ๋น์ฐํ js๋ก ํด๋์ค ํ ๊ธํด๊ฐ๋ฉด์
display: none๊ณผ display: block์ ์๋ค๊ฐ๋ค ์คํ์ผ ์ฃผ๋๊ฑธ ์๊ฐํ์๋ค.
๋ฌผ๋ก ๊ทธ๋ ๊ฒ ํด๋ ๋๊ฒ ์ง๋ง, css์ ๊ฐ์ํด๋์ค์ธ :target์ผ๋ก ๋ง๋ค์ด๋ณด๋ ๋ฐฉ๋ฒ์ด ์๋ค!
์ค์ตํ๋ฉด์ ์๋ฌธ์ด์๋ ์ ์ด, ๊ณ์ํด์ ์ฃผ์ ๋์ #popup์ด ๋ถ๊ณ #๊ฐ ๋ถ๊ณ ํ๋๋ฐ ์ด๊ฒ ์ข ๋ณด๊ธฐ์ซ๋ค.
์ฆ, aํ๊ทธ href๋ก ํ๋ค๋ณด๋ ์ด๋ ๊ฒ ๋์ ๋ถ๋๊ฑด๋ฐ , ๋ด๊ฐ ์ฃผ๋ก ์ด์ฉํ๋ ์ฌ์ดํธ๋ค๋ ์ด๋ ๊ฒ ํ์๋? ๐ฅ
'WEB STUDY > HTML,CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CSS - Media Query (Landscape) (0) | 2022.03.05 |
---|---|
CSS - BootStrap (๋ถํธ์คํธ๋ฉ) (0) | 2022.03.01 |
CSS - Grid System (design) (0) | 2022.03.01 |
CSS - ์ ํ์ (Selector) ์ฐ์ ์์ (0) | 2022.03.01 |
CSS - Selector ์ด์ ๋ฆฌ (0) | 2022.03.01 |