×

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

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 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.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

CSS Transition

CSS Transition The transitions in CSS are influenced, included to modify the component from one design to another, without the use of the JavaScript or Flash. We should describe two of the...

2 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 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 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...

5 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.