×

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of lists below an unordered list. <ul> as it is popularly recognized in HTML international, sort of contrary to popular belief. Nested list (<li>) tags below the <ul> tag used to create drop-down shape.

To carry out the outcomes, we can use CSS for the additives particularly present inside the shape in a big way.

Example

Let’s consider a coding example.

Consider the below code:

<!DOCTYPE html>
<html>
    <head>
        <title>Dropdown property</title>
    </head>
    <body>
        <nav>
            <ul>
                <li class="Level-1">
                <a href="">Dropdown-list</a>
                <ul>
                    <li><a href="">Li 1</a></li>
                    <li><a href="">Li 2</a></li>
                    <li><a href="">Li 3</a></li>
                    <li><a href="">Li 4</a></li>
                </ul>
                </li>
            </ul>
        </nav>
    </body>
</html>

Output:

CSS Dropdowns

Types of CSS Dropdowns

The CSS Dropdowns are classified as:

  1. Basic Dropdown
  2. Dropdown-menu
  3. Right-aligned Dropdown
  4. Dropdown Navbar

Basic Dropdown

Example

Let’s consider a coding example.

<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
}


.dropdown-content {
display: none;
position: absolute;
background-color: white ;
min-width: 170px;
box-shadow: 0px 9px 16px 0px black;
padding: 13px 18px;
z-index: 1;
}


.dropdown:hover .dropdown-content {
display: block;
}


</style>
</head>
<body>
<h2>Hoverable Dropdown Here</h2>
<p>Circulate the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
<span>Hover Mouse Over Me</span>
<div class="dropdown-content">
<p>Hello All..!!</p>
</div>
</div>
</body>
</html>

Output:

CSS Dropdowns

If we hover the mouse (Hover Mouse Over Me) then the below output is shown:

CSS Dropdowns

Explanation:

HTML- Use any element to open the dropdown content material, e.g. A <span>, or a <button> element.

Use a container element (like <div>) to create the dropdown content material and upload something you want inner of it.

Wrap a <div> element across the elements to put the dropdown content efficaciously with CSS.

CSS- The .Dropdown class makes use of position: relative, which is wanted when we need the dropdown content material to be positioned right under the dropdown button (using position: absolute).

The .Dropdown-content material magnificence holds the real dropdown content. It's far hidden by default and might be displayed on hover (see underneath). Word the min-width is ready to 160px. Feel free to trade this.

Rather than using a border, we've got used the CSS field-shadow belongings to make the dropdown menu appear to be a "card".

The :hover selector is used to revealing the dropdown menu when the consumer moves the mouse over the dropdown button.

Dropdown-menu

Create a dropdown menu that allows the consumer to select an alternative from a listing. In this case, just like the preceding one, it depicts that we add links in the dropdown box and fashion them to in shape a styled dropdown button:

Example

Code:

<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
  background-color: purple;
  color: white;
  padding: 15px;
  font-size: 15px;
  border: none;
  cursor: pointer;
}


.dropdown {
  position: relative;
  display: inline-block;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: seashell;
  min-width: 150px;
  box-shadow: 0px 8px 16px 0px black;
  z-index: 1;
}


.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


.dropdown-content a:hover {background-color: white}


.dropdown:hover .dropdown-content {
  display: block;
}


.dropdown:hover .dropbtn {
  background-color: purple;
}
</style>
</head>
<body>


<h2>Dropdown Menu</h2>
<p>Hover the mouse over the button to open that dropdown menu.</p>


<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
  <a href="**">Link-1</a>
  <a href="**">Link-2</a>
  <a href="**">Link-3</a>
  </div>
</div>


<p><strong>Remember:</strong> We use href="**" for test links. In a web site this would be URLs.</p>


</body>
</html>

Output:

CSS Dropdowns

If we hover the mouse(Dropdown) then below output is shown:

CSS Dropdowns

Right-aligned Dropdown

If you need the dropdown menu to move from right to left, instead of left to right, upload right: 0;

Example

Code:

<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
  background-color: lightcoral;
  color: white;
  padding: 18px;
  font-size: 18px;
  border: none;
  cursor: pointer;
}


.dropdown {
  position: relative;
  display: inline-block;
}


.dropdown-content {
  display: none;
  position: absolute;
  right: 0;
  background-color: white;
  min-width: 150px;
  box-shadow: 0px 8px 16px 0px black;
  z-index: 1;
}


.dropdown-content a {
  color: black;
  padding: 11px 15px;
  text-decoration: none;
  display: block;
}


.dropdown-content a:hover {background-color: white;}


.dropdown:hover .dropdown-content {
  display: block;
}


.dropdown:hover .dropbtn {
  background-color: lightcoral;
}
</style>
</head>
<body>


<h2>Right Left Dropdown</h2>
<p>Determine whether the dropdown content material should go from left to right or right to left with the left and proper properties.</p>


<div class="dropdown" style="float:left;">
  <button class="dropbtn">Left</button>
  <div class="dropdown-content" style="left:0;">
  <a href="**">Product1</a>
  <a href="**">Product2</a>
  <a href="**">Product3</a>
  </div>
</div>


<div class="dropdown" style="float:right;">
  <button class="dropbtn">Right</button>
  <div class="dropdown-content">
  <a href="**">Product1</a>
  <a href="**">Product2</a>
  <a href="**">Product3</a>
  </div>
</div>


</body>
</html>

Output:

CSS Dropdowns CSS Dropdowns

Dropdown Navbar

Dropdown menu is added in Navbar.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<style>
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: black;
}


li {
  float: left;
}


li a, .dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 15px 17px;
  text-decoration: none;
}


li a:hover, .dropdown:hover .dropbtn {
  background-color: purple;
}


li.dropdown {
  display: inline-block;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: white;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px black;
  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: white;}


.dropdown:hover .dropdown-content {
  display: block;
}
</style>
</head>
<body>


<ul>
  <li><a href="#Home">Home</a></li>
  <li><a href="#Login">Login</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="**">Product 1</a>
      <a href="**">Product 2</a>
      <a href="**">Product 3</a>
    </div>
  </li>
</ul>


<h3>Dropdown Menu is in a NavBar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>

Output:

CSS Dropdowns CSS Dropdowns

Related Topics

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

Css Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

5 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Hover

CSS Hover: CSS :hover is used to select the various elements whenever we point the mouse on these elements. We can also use this selector on all the HTML elements,...

3 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.