.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
border: 1px solid red;
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="dropdown">
<span> ์ฌ๊ธฐ์ ๋ง์ฐ์ค ์ค๋ฒ ํด๋ด! </span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
<div class="dropdown">
<img src="cat.jpg" alt="logo">
<div class="dropdown-content">
<img src="cat.jpg" alt="cat" width="200" height="200">
<div class="desc">๊ณ ์์ด ์
๋๋น ๊ท์ฝ์ฅฌ</div>
</div>
</div>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
<button class="dropbtn">Left</button>
<div class="dropdown-content" style="left:0;">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div class="dropdown" style="float:right;">
<button class="dropbtn">Right</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
๐ฌcursor: pointer; ๋ง์ฐ์ค ์ปค์์ ๋ชจ์์ ํฌ์ธํฐ(pointer) ํํ๋ก ๋ณ๊ฒฝํ๋ ๊ฒ
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Dropdown</a>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</li>
</ul>
li a, .dropbtn { /**/
display: inline-block; /**/
padding: 14px 16px;
text-align: center; /**/
text-decoration: none;
color: #fff;
}
li a:hover , .dorpdown:hover .dropbtn{ /* */
background-color: rgb(61, 96, 96);
}
li.dropbtn { /**/
display: inline-block; /**/
}
.dropdown-content { /**/
display: none; /**/
position: absolute; /**/
background-color: f9f9f9;
min-width: 160px; /**/
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); /**/
z-index: 1; /**/
}
.dropdown-content a { /**/
color: black;
padding: 12px 16px;
text-decoration: none;
display: block; /**/
text-align: left; /**/
}
.dropdown-content a:hover {background-color: #8b4a4a;} /**/
.dropdown:hover .dropdown-content { /**/
display: block; /**/
}
๋๋กญ ๋ค์ด
๐ฌ li a, .dropbtn { display: inline-block;} => ๋๋น(width)์ ๋์ด(height)๋ฅผ ์ง์ ํ ์ ์๋ ์ ์ฐํ ์์ (์ธ๋ผ์ธ ์์์ ์ ์ฌํ ๋ฐฐ์น/ ๋ธ๋ก ๋ ๋ฒจ ์์์ ์ ์ฌํ ํฌ๊ธฐ์ ์์น ์กฐ์ /์ฌ๋ฌ ์์๋ฅผ ๋๋ํ ๋ฐฐ์น ( ์ํ ๋ฉ๋ด ํญ๋ชฉ์ด๋ ๊ทธ๋ฆฌ๋ ์์ ) )
๐ฌ li.dropbtn { display: inline-block; } =>
๐ฌ .dropdown-content { display: none; => ์์๋ฅผ ์จ๊น ์ํ
position: absolute; => ์์์ ์์น๋ฅผ ๋ถ๋ชจ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋์ ์ผ๋ก ์ง์
min-width: 160px; => ์ต์ ๋๋น๋ณด๋ค ์์์ง์ง ์๋๋ก ์ ํ
z-index: 1; => ์์์ ์์ ์์(z-index)๋ฅผ ์ง์ , z-index ๊ฐ์ด ํฐ ์์์ผ์๋ก ๋ค๋ฅธ ์์ ์์
}
๐ฌ .dropdown-content a { display: block; => ๋ธ๋ก ๋ ๋ฒจ ์์๋ ํ ์ค์ ์ฐจ์งํ๋ฉฐ, ์์ง์ผ๋ก ์์
๋๋ค.
text-align: left;
๐ฌ .dropdown:hover .dropdown-content { display: block; } => ๋ธ๋ก ๋ ๋ฒจ ์์๋ ํ ์ค์ ์ฐจ์งํ๋ฉฐ, ์์ง์ผ๋ก ์์
๋๋ค.