Seeking guidance on how to perfectly align my collapsible navigation bar for a university assignment

Currently, I am tackling a project for my college course, and the navigation bar is posing some challenges. Despite conducting various research attempts to resolve the issue, the site remains quite basic as it stands as just a template. Primarily, my objective for the navigation bar was to center it on the main background image while enabling the "Rooms" tab to have a dropdown functionality.

nav {
    
                background-color: transparent;
    
            }
    
            nav a {
                color: #F2E2C4;
                text-align: center;
                padding: 10px;
                text-decoration: none;
                font-size: 20px;
                font-weight: bold;
                display: inline-block;
                text-decoration: none;
                position: relative;
                font-family: 'Spartan';
            }
    
            nav ul {
                padding: 0px;
                margin: 0px;
                list-style-type: none;
            }
    
            nav a.beachview {
                float: left;
                color: #F2E2C4;
                text-align: center;
                padding: 0px 16px;
                text-decoration: none;
                font-size: 45px;
                padding-top: 2px;
                font-family: 'Spartan';
            }
    
            nav a:hover {
                background-color: none;
                color: #8C8474;
            }
    
            nav ul ul {
                display: none;
                position: absolute;
            }
    
            nav ul li:hover ul {
                display: inline-block;
            }
    
            nav ul ul li {
                float: none;
            }
    
            nav li {
                float: left;
            }
<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <title>Beachview - Home</title>
    <meta name="author" content="Your Name">
    <meta name="description" content="Example description">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link rel="icon" type="image/x-icon" href="" />
    <link href="https://fonts.googleapis.com/css?family=Dosis&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Spartan&display=swap" rel="stylesheet">
</head>

<body>
    <div class="bg-img">
        <div class="container">
            <nav>
                <ul>
                    <li><a href="#">ROOMS</a>
                        <ul>
                            <li><a href="#">GLASGOW</a></li>
                            <li><a href="#">EDINBURGH</a></li>
                            <li><a href="#">ABERDEEN</a></li>
                            <li><a href="#">DUNDEE</a></li>
                        </ul>
                    </li>
                    <li><a href="#">GALLERY</a></li>
                    <li><a class="beachview" href="#">BEACHVIEW</a></li>
                    <li><a href="#">LOCAL</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </nav>
        </div>
    </div>
    <main>Main stuff goes here</main>
</body>

</html>

I'm not entirely certain if the above information makes sense since the background image does not seem to be loading properly.

Answer №1

  1. To create a dropdown menu, add a .dropdown class to the li element containing the dropdown ul.
<li class="dropdown"><a href="#">ROOMS</a>
  <ul>...</ul>
</li>
  1. To align the dropdown below the "Rooms" link, apply the following styles:
.dropdown {
  /* Allow absolute positioning of child ul relative to parent */
  position:relative; 
}
.dropdown ul {
  position:absolute;
  left:0;
  top:2.5em;
}
  1. Add display: flex to your .bg-img container and include margin: auto; in the child-element nav for vertical and horizontal centering.
.bg-img {
  background-image: url('https://via.placeholder.com/900x200/000/333'); // Replace with your image
  height: 200px; // Sample height
  width: 100%;   // Sample width
  display: flex; // Easily center child-elements with Flex
}

nav {
  background-color: transparent;
  margin:auto; // Center alignment key
}

I have updated by moving your class="container" from the wrapper div to the nav element and removed the unnecessary div.

Here is a full working code snippet. (Click "Run Code Snippet, then hit "Full Page" on the right-hand side to see it working):

.bg-img {                            
  /* Replace this img url with your image */
  background-image: url('https://via.placeholder.com/900x200/000/333'); 
  height: 200px; /* Sample height */
  width: 100%;   /* Sample width */
  display: flex; /* Easily center child-elements using Flex with margin:auto */
}

nav {
  background-color: transparent;
  margin:auto;
}
.dropdown {
  /* Allow absolute positioning of child ul relative to parent */
  position:relative; 
}
.dropdown ul {
  position:absolute;
  left:0;
  top:2.5em;
}

/* None of the below code was modified */
nav a {
    color: #F2E2C4;
    text-align: center;
    padding: 10px;
    text-decoration: none;
    font-size: 20px;
    font-weight: bold;
    display: inline-block;
    text-decoration: none;
    position: relative;
    font-family: 'Spartan';
}

nav ul {
    padding: 0px;
    margin: 0px;
    list-style-type: none;
}

nav a.beachview {
    float: left;
    color: #F2E2C4;
    text-align: center;
    padding: 0px 16px;
    text-decoration: none;
    font-size: 45px;
    padding-top: 2px;
    font-family: 'Spartan';
}

nav a:hover {
    background-color: none;
    color: #8C8474;
}

nav ul ul {
    display: none;
    position: absolute;
}

nav ul li:hover ul {
    display: inline-block;
}

nav ul ul li {
    float: none;
}

nav li {
    float: left;
}
<body>
  <div class="bg-img">
    <!-- Old <div class="container"> moved to <nav> -->
    <nav class="container">
      <ul>
        <li class="dropdown"><a href="#">ROOMS</a>
          <ul>
            <li><a href="#">GLASGOW</a></li>
            <li><a href="#">EDINBURGH</a></li>
            <li><a href="#">ABERDEEN</a></li>
            <li><a href="#">DUNDEE</a></li>
          </ul>
        </li>
        <li><a href="#">GALLERY</a></li>
        <li><a class="beachview" href="#">BEACHVIEW</a></li>
        <li><a href="#">LOCAL</a></li>
        <li><a href="#">CONTACT</a></li>
      </ul>
    </nav>
  </div>
  <main>Main content goes here</main>
</body>

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

Ensure the position of a DIV is synchronized with the three-dimensional world while hovering over a ThreeJS Object

Is it possible to position a DIV in 2D space on top of a 3D Object in ThreeJs in a way that it moves along with the object when the world rotates? Keep in mind that the 3D Object is loaded from a model, containing nested groups and a mesh. https://i.ssta ...

Store HTML content in cache for optimal rendering in WebView

My dilemma revolves around the need to showcase HTML content using a WebView control. This content is retrieved from a REST webservice as a string and might include images, CSS, and other elements that must be fetched from the server. While I could rely o ...

Creating a straightforward image slideshow using jQuery featuring next and previous buttons

I am looking for assistance in adding next and previous buttons to this slider. I came across a code snippet on a blog that could be useful, which can be found at .net/dk5sy93d/ ...

Instructions for pulling data from a datatable row and showing it in a modal dialog for editing

I am facing an issue with displaying a dialog for editing datatable rows using Jquery. Despite trying various codes and researching online, I have not been successful in achieving the desired functionality. Any helpful guidance or suggestions on how to ach ...

Toggling jQuery to show or hide content depending on the selector

I am looking to implement a jQuery-based animation and here is the code I have so far: >items=document.querySelectorAll("#customers td span"); [<span alt=​" 02,Counter,11,2013-04-06 14:​59:​16">​ 02​</span>​, <span>​11 ...

Accessing Flask from various devices

Is there a way to access a Flask web-app from different devices instead of just locally on http://127.0.0.1:5000/? I want to be able to generate a specific IP address or make the site accessible from other devices. If anyone knows how to achieve this, pl ...

[SCSS] I am experiencing difficulties with loading fonts on my machine/browser, while they are functioning normally on other devices

When using relative paths, fonts may not load properly. However, absolute paths seem to work without any issues. //styles.scss @font-face { font-family: 'Roboto-Bold'; src: url('../fonts/Roboto-Bold.ttf'); } @font-face { font-fam ...

When the user hits the enter key, automatically submit a form without the need for

Is it possible to submit a form on enter using type="button"? Here are my input fields: <input type="text" id = "login-user" class="form-control log-input" placeholder="Username" required="required"> <input type="password" id="login-password" clas ...

If the sidebar is not found on WordPress, delete the CSS code

This situation seems a bit strange when it comes to applying a background color. Here is the main structure: <section class="main-row"> <div class="container"> <div class="row"> <div id="primary" class="col-sm- ...

Switch between showing the Font Awesome TitleText and its associated Class with a single click

Can the value of the title attribute for <i> be toggled? For example, if the title is set to <i title="Favorite This Post" class="fa fa-star-o" aria-hidden="true"> within <div class="postoption"> I would like to toggle both the title te ...

What is the reason behind getElementsByClassName not functioning while getElementById is working perfectly?

The initial code snippet is not functioning correctly DN.onkeyup = DN.onkeypress = function(){ var div = document.getElementById("DN").value document.document.getElementsByClassName("options-parameters-input").style.fontSize = div; } #one{ heigh ...

How to Alter the Color of a Hyperlink in HTML

I've been working on an angular2 application and I'm using a hyperlink to trigger a method that opens a small popup window. I've tried different methods to change the color of the link when it's clicked, but so far none have worked. Th ...

"Enhance Your Slide Up/Down Menu with a Unique Hover Effect in jQuery

HTML: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="navigation_desktop"> <div class="button_desktop_01">1.0 Main Menu <div class="SlideItem ...

Unlock maximum screen viewing on your custom video player with these steps to activate fullscreen

I'm having an issue with my basic video player - when toggling fullscreen, it doesn't fill the whole screen even though I tried using .fullscreen{width:100%} without success after searching for a solution. html <div class='player-contai ...

iOS is causing issues with the JavaScript function and not allowing it to

By creating an NSString that contains JavaScript, I am able to set the e-mail body. NSString * someString =@"<!DOCTYPE html>\n <html>\n <body> \n <h1>My Web Page</h1> \n <p ...

Tips for creating a vertical Angular Material slider with CSS

Attempting to modify the angular material directive to render vertically has been a challenge. I experimented with using transform:rotate in the CSS, however, it resulted in the slider behaving and rendering differently. md-slider { position: absolute ...

Setting custom domains on Heroku is essential for optimizing the performance and branding of my website

Essentially, when using Heroku, you are provided with a default domain: XXX.herokuapp.com. Personally, I have a collection of REST APIs that I want to configure on a domain called: api.myDomain.com. At the same time, I also have my HTML files (web view) ...

Tips for aligning the navigation bar, main content, and footer in the center

Exploring the realm of web coding for the first time, I find myself in need of some guidance. My current project involves creating a webpage for the local sports team to replace their outdated one on Wordpress. Finding the platform not very user-friendly, ...

Position the label and the select dropdown side by side within the sweetalert 2 component

Can anyone provide an example of aligning a label and dropdown in the same row using sweetalert2? I attempted to customize the label placement, but it appears on a separate line. I would like to log the selected dropdown item upon clicking the OK button. ...

Using Javascript math to create a backdrop effect by drawing rectangles around multiple other rectangles

Important Note: In this discussion, the term overlay will be used interchangeably with backdrop. Currently, I am developing a guide mode where I emphasize certain elements by making them stand out against a darker semi-transparent background. The approac ...