CSS Right-aligned Dropdown
Adding right: 0; makes the dropdown menu open from right to left, rather than from left to right.
Example
Prompt
Create a page demonstrating right-aligned dropdown content. Show two dropdown buttons side by side: one labeled "Left" floated left with dropdown-content using left:0, and one labeled "Right" floated right with dropdown-content using right:0. The dropdown buttons have green background (#4CAF50), white text, 16px padding, 16px font-size, no border, pointer cursor. On hover the button background changes to #3e8e41. The dropdown container is position relative, display inline-block. The dropdown-content is hidden by default, position absolute, right:0, background #f9f9f9, min-width 160px, box-shadow 0px 8px 16px 0px rgba(0,0,0,0.2), z-index 1. Links inside are black, 12px 16px padding, no underline, display block. Link hover background is #f1f1f1. On dropdown hover, show the content. Include a heading "Aligned Dropdown Content" and a paragraph explaining the left/right properties.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Right-Aligned Dropdown</title>
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
right: 0;
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;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h2>Aligned Dropdown Content</h2>
<p>Determine whether the dropdown content should go from left to right or right to left with the left and right properties.</p>
<div class="dropdown" style="float:left;">
<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>
</div>
</body>
</html>
CSS Dropdown Image
A dropdown box can hold an image and other content as well.
Example
Prompt
Create a page demonstrating a dropdown with an image. A div with class "dropdown" contains a small thumbnail image (100x50) and a hidden dropdown-content div. The dropdown-content shows a larger version of the same image (300x200) and a description div with text "Beautiful Cinque Terre" with 15px padding and centered text. The dropdown container is position relative. The dropdown-content is hidden by default, position absolute, background #f9f9f9, min-width 160px. On hover, the dropdown-content is displayed. Use "img_5terre.jpg" as the image source with alt text "Cinque Terre". Include a heading "Dropdown Image" and a paragraph "Move the mouse over the image below to open the dropdown content."
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown Image</title>
<style>
.dropdown {
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
}
.dropdown:hover .dropdown-content {
display: block;
}
.desc {
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<h2>Dropdown Image</h2>
<p>Move the mouse over the image below to open the dropdown content.</p>
<div class="dropdown">
<img src="img_5terre.jpg" alt="Cinque Terre" width="100" height="50">
<div class="dropdown-content">
<img src="img_5terre.jpg" alt="Cinque Terre" width="300" height="200">
<div class="desc">Beautiful Cinque Terre</div>
</div>
</div>
</body>
</html>
CSS Dropdown Navbar
A navigation bar can also contain a dropdown menu.
Example
Prompt
Create a page with a dropdown menu inside a navigation bar. Use an unordered list with list items for Home, News, and a Dropdown item. The body has margin and padding 0. The ul has no list-style, no margin/padding, overflow hidden, background-color #38444d. List items float left. Links and the dropbtn have display inline-block, white color, centered text, 14px 16px padding, no underline. On hover, links and the dropbtn get a red background. The dropdown list item has display inline-block. The dropdown-content is hidden by default, position absolute, background #f9f9f9, min-width 160px, box-shadow 0px 8px 16px 0px rgba(0,0,0,0.2), z-index 1. Links inside dropdown-content are black, 12px 16px padding, no underline, display block, text-align left. On hover they get background #f1f1f1. On dropdown hover the content is shown. Include a heading "Dropdown Menu in Navbar" and paragraph "Hover over the Dropdown link to see the dropdown menu."
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown Navbar</title>
<style>
body {
margin: 0;
padding: 0;
}
h2, p {
margin: 10px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #38444d;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
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: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<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>
<h2>Dropdown Menu in Navbar</h2>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>