MapleStory Finger Point

๐Ÿ”ด WEB/Web ๊ฐœ๋…์ •๋ฆฌ

CSS Dropdowns

HYEJU01 2024. 4. 19. 03:19

    .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; } => ๋ธ”๋ก ๋ ˆ๋ฒจ ์š”์†Œ๋Š” ํ•œ ์ค„์„ ์ฐจ์ง€ํ•˜๋ฉฐ, ์ˆ˜์ง์œผ๋กœ ์Œ“์ž…๋‹ˆ๋‹ค.