Struggling to create a drop-down box with CSS and not achieving the expected results

Currently, I am delving into the world of HTML and CSS to hone my skills in front-end web development. While attempting to code a drop-down box within my navigation menu, I have encountered an issue where the drop-down opens at the left corner while the navigation link remains elsewhere on the page. Your assistance in resolving this matter would be greatly appreciated. The HTML code is as follows:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
<div  id="wpback"></div>
<div id="wallpaper" ></div>

<div id="name">                                                 <!  
========================Name of Company================= >
<header>
            <h1 class="prince">PRINCE Institute</h1>
            <input type="button" value="Log In" style="float:right"></input>
            <input type="button" value="Sign Up" style="float:right">
</input>

</header>
            What do you want to know?<br><input type="text"></input>
            <input type="button" value="Search"></input>
</div>
<div id="menu">                                         <! 
========================Navigation Menu================= >
        <ul>
            <li><a href="">Home</a></li>
            <li><a href="">About</a>
                <ul>
                    <li><a href="">A1</a></li>
                    <li><a href="">A2</a></li>
                    <li><a href="">A3</a></li>
                </ul></li>
            <li><a href="">Courses</a>
                <ul>
                    <li><a href="">Java</a></li>
                    <li><a href="">Python</a></li>
                    <li><a href="">Data Base Management System</a></li>
                    <li><a href="">Machine Learning</a></li>
                    <li><a href="">Blockchain</a></li>
                </ul></li>
            <li><a href="">Settings</a>
                <ul>
                    <li><a href="">Your Profile</a></li>
                    <li><a href="">Your Cart</a></li>
                    <li><a href="">Mode</a></li>
                </ul></li>
        </ul>
</div>      

<div>                                           <! 
========================Content=========================== >
            <ul >Courses Offered:
            <li>Java</li>
            <li>Python</li>
            <li>Machine Learning</li>
            <li>Block Chain</li>
            <li>Data Base Management System</li></ul>
</div>


</body>
</html>

The CSS code is as follows:

body {
  font-family: lucida console;
  font-size: 14px;
  color: white;
  margin-top: 0;
  margin-left: 0;
  margin-right: 0;
}

#name {
  position: relative;
  top: 0;
  left: 0;
}

.prince {
  color: white;
  text-align: center;
  padding: 5px;
  background-color: rgba(65, 15, 0, 0.5);
}

#wpback {
  background-color: black;
  position: absolute;
  width: 100%;
  height: 100%;
}

#wallpaper {
  background-image: url('tech.jpg');
  opacity: 0.1;
  position: absolute;
  width: 100%;
  height: 100%;
}

#menu {
  margin-top: 10px;
  background-color: rgba(65, 15, 0, 1);
  height: 50px;
  position: relative;
}

#menu ul {
  padding: 0;
  margin: 0;
}

#menu ul li {
  list-style: none;
  display: inline;
}

#menu ul li a {
  margin: 10px;
  padding: 16px;
  text-decoration: none;
  color: white;
  line-height: 50px;
}

#menu ul li a:hover {
  background-color: gray;
  color: black;
  transition: ease-in-out 0.2s;
}

#menu ul li ul li {
  display: none;
}

#menu ul li:hover ul li {
  background-color: silver;
  display: block;
  width: 220px;
}

#menu ul li:hover ul li a {
  margin: 10px;
  padding: 15px;
  text-decoration: none;
  line-height: 50px;
  color: black;
}

#menu ul li ul li a:hover {
  background-color: gray;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="/style.css">
</head>

<body>
  <div id="wpback"></div>
  <div id="wallpaper"></div>

  <div id="name">
    <!  
    ========================Name of Company================= >
    <header>
      <h1 class="prince">PRINCE Institute</h1>
      <input type="button" value="Log In" style="float:right"></input>
      <input type="button" value="Sign Up" style="float:right">
      </input>

    </header>
    What do you want to know?<br><input type="text"></input>
    <input type="button" value="Search"></input>
  </div>
  <div id="menu">
    <! 
    ========================Navigation Menu================= >
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a>
        <ul>
          <li><a href="">A1</a></li>
          <li><a href="">A2</a></li>
          <li><a href="">A3</a></li>
        </ul>
      </li>
      <li><a href="">Courses</a>
        <ul>
          <li><a href="">Java</a></li>
          <li><a href="">Python</a></li>
          <li><a href="">Data Base Management System</a></li>
          <li><a href="">Machine Learning</a></li>
          <li><a href="">Blockchain</a></li>
        </ul>
      </li>
      <li><a href="">Settings</a>
        <ul>
          <li><a href="">Your Profile</a></li>
          <li><a href="">Your Cart</a></li>
          <li><a href="">Mode</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <div>
    <! 
    ========================Content=========================== >
    <ul>Courses Offered:
      <li>Java</li>
      <li>Python</li>
      <li>Machine Learning</li>
      <li>Block Chain</li>
      <li>Data Base Management System</li>
    </ul>
  </div>


</body>

</html>

Regarding the issue with the navigation menu layout, when no options are included, the Home, About, Courses, and Settings links align properly in the navigation bar. However, upon adding dropdown options, the individual links cascade downward instead of remaining in line.

Answer №1

Dropdown Alignment Options

Decide whether you want the dropdown content to be aligned from left to right or right to left using the left and right properties.

CSS styling

.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;
}

Here is the HTML code

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Imitate a keypress within a hidden web browser component using C#

Currently, I am engaged in a project that requires me to navigate a webpage and gather information about products, prices, etc. using the webbrowser object in .NET 3.5. The challenge arises because the page contains fields with an ajax suggest script, mak ...

The error code 405 (Method Not Allowed) occurs in Ajax when the action field is empty or identical to the current page

Special thanks to @abc123 for sharing the code below in one of their posts: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <form id="formoid" a ...

The customization of a class within an array loop is not functioning properly

I'm facing an issue where I've added a class (question) to each element in an array, and the loop is running smoothly. However, I'm having trouble getting jQuery to recognize the class - it's almost as if it doesn't exist... I sus ...

Angular 6 CSS spacing dilemmas

We recently made the switch from Angular 5 to Angular 6 and noticed that there is no spacing between the buttons/icons, etc. We are looking to bring back the spaces between the buttons and icons. I have recreated the issue below. As you can see in the Ang ...

Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Table heading</th> </tr> < ...

How to customize the arrow color of an expanded parent ExpansionPanel in material-ui

Currently facing a challenge in customizing material-ui themes to achieve the desired functionality. The goal is to have the expansion panels display a different arrow color when expanded for improved visibility. However, this customization should be appl ...

Managing class values with Selenium - Manipulating and updating classes

In the program I'm working on, there is a need to toggle which element receives the class value of "selected". <div class="countryValues"> <div data-val="" >USA and Canada</div> <div data-val="US" >USA - All< ...

Modifying css background in real-time based on the current weather conditions

Is there a way to dynamically change the background image in CSS based on the weather condition? I'm utilizing the wunderground API to retrieve the weather data, which is stored in the weather variable. I am struggling with how to update the backgrou ...

Updating HTML input fields via AJAXIncorporating AJAX

Let's take a look at our database: ID Name Price 1 product1 200 2 product2 300 3 product3 400 Now, onto my form: <form action="test.php" method="post" /> <input type="text" name="search" id="search" placeholder="enter ...

Switch between displaying the HTML login and register forms by clicking a button

Currently, I am working on a website that requires users to either log in or register if they do not have an account. Below is the code I have developed for the login page: <span href="#" class="button" id="toggle-login">Log in</span> <di ...

Retrieving data from relational databases based on specific conditions in Django

I've been encountering some challenges with rendering a particular view ('myprojects') that displays the projects associated with a user from the 'uProjects' model. To address this, I created an object_list to filter 'uProject ...

Modify the state of each individual button

I recently came across a cool implementation using clicks and jQuery fade effects to show a div when a button is clicked and then change the state of the button. Check it out here: http://jsfiddle.net/ttj9J/5/ HTML <a class="link" href="#" data-rel="c ...

Make toggleClass show up smoothly without disappearing

I'm currently working with jQuery's toggleClass() method and I want to achieve a fade-in effect without the corresponding fade-out animation. Using the "duration" attribute applies the same duration for both actions, which is not what I want. I w ...

What is the best way to show input choices once an option has been chosen from the 'select class' dropdown menu?

When it comes to displaying different options based on user selection, the following HTML code is what I've been using: <select class="form-control input-lg" style="text-align:center"> <option value="type">-- Select a Type --</opti ...

Puppeteer challenge with breaking images

When generating a PDF from HTML, I am facing an issue where the signature image breaks on another page. Is there a way to ensure that if content or images break, they will move to another PDF page? Puppeteer version - 3.3.0 Node version - 12.16.1 Check t ...

If the image is not found, add a count instead of showing a new one each time

I have implemented a function to retrieve the two-letter country code: $ipaddress = $_SERVER['REMOTE_ADDR']; function ip_details($ip) { $json = file_get_contents("http://ipinfo.io/{$ip}"); $details = json_decode($json); return $detai ...

Trouble with implementing conditional styles in Next JS

<label className={`${darkModeActive ? 'text-brand-five' : 'text-brand-seven'} ${errors.invoiceDate ? 'text-[#EC5756]' : ''}`} htmlFor="invoiceDate"> Invoice Date </label> I am encountering a ...

Troubleshooting AJAX hover functionality issue

Here is the content that loads through AJAX: <div class="grid"> <div class="thumb"> <img alt="AlbumIcon" src="some-image.jpg"> <div style="bottom:-75px;" class="meta"> <p class="title">Title< ...

The unexpected syntax error, indicated by an ampersand followed by a quotation mark, was encountered in [location] on line 7

Every time I try to run my code, I encounter the following error message: Parse error: syntax error, unexpected '"' in D:\server\WT-NMP\WWW\myproj1\insert.php on line 7 This is the code that is triggering the error: &l ...

The background-size CSS property may experience malfunctioning on iOS devices when using Safari browser

My website has a body background size set, which works well on Android and Windows devices. The background size is intended to be 85% width and 5% height of the body. However, when loading the site on an iPhone, the height appears much larger than 5%. It s ...