방법 - 드롭업
CSS로 드롭업 메뉴를 만드는 방법을 알아보세요.
드롭업
드롭업 메뉴는 사용자가 미리 정의된 목록에서 하나의 값을 선택할 수 있는 토글 가능한 메뉴입니다.
호버블 드롭업 만들기
사용자가 요소 위로 마우스를 이동할 때 나타나는 드롭업 메뉴를 만듭니다.
1단계) HTML 추가:
예시
<div class="dropup">
<button class="dropbtn">Dropup</button>
<div class="dropup-content">
<a href="#">Link
1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
예시 설명
<button>, <a> 또는 <p> 요소와 같은 요소를 사용하여 드롭업 메뉴를 엽니다.
컨테이너 요소(예: <div>)를 사용하여 드롭업 메뉴를 만들고 그 안에 드롭업 링크를 추가합니다.
<div> 요소를 버튼과 <div> 주위에 둘러싸서 CSS를 사용하여 드롭다운 메뉴를 올바르게 배치합니다.
2단계) CSS 추가:
예시
/* Dropup Button */
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The
container <div> - needed to position the dropup content */
.dropup {
position: relative;
display:
inline-block;
}
/* Dropup content (Hidden by Default) */
.dropup-content {
display: none;
position:
absolute;
bottom: 50px;
background-color: #f1f1f1;
min-width: 160px;
box-shadow:
0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the
dropup */
.dropup-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropup links on hover */
.dropup-content a:hover {background-color: #ddd}
/* Show the
dropup menu on hover */
.dropup:hover .dropup-content {
display: block;
}
/* Change the background color of the dropup button when the
dropup content is shown */
.dropup:hover .dropbtn {
background-color: #2980B9;
}
예시 설명
배경색, 패딩 등으로 드롭업 버튼의 스타일을 지정했습니다.
.dropup
클래스는 드롭 position:relative
업 콘텐츠를 드롭업 버튼 위에 배치할 때 필요한 를 사용합니다( position:absolute
).
클래스 는 .dropup-content
실제 드롭업 메뉴를 보유합니다. 기본적으로 숨겨져 있으며 마우스를 가져가면 표시됩니다(아래 참조). 160px min-width
로 설정되어 있습니다. 자유롭게 변경하세요. 팁: 드롭업 콘텐츠의 너비를 드롭업 버튼만큼 넓히려면 width
100% 로 설정 overflow:auto
하고 작은 화면에서 스크롤을 활성화합니다.
테두리를 사용하는 대신 box-shadow
속성을 사용하여 드롭다운 메뉴가 "카드"처럼 보이도록 했습니다. 또한 z-index를 사용하여 드롭업을 다른 요소 앞에 배치합니다.
:hover
선택기는 사용자가 드롭업 버튼 위로 마우스를 이동할 때 드롭업 메뉴를 표시하는 데 사용됩니다 .