What is the best way to incorporate a dropdown menu into existing code without causing any disruption?

I've come across this question multiple times before, but I still haven't found a suitable answer or solution that matches my specific situation. (If you know of one, please share the link with me!)

My goal is to create a basic dropdown menu within a horizontal navigation bar where a logo image is centered with links on either side. While I have Bootstrap installed, I couldn't quite figure out how to implement it using their framework, so I decided to build it from scratch.

The implementation doesn't necessarily have to be limited to HTML/CSS; however, I'm not well-versed in JavaScript yet (still learning).

Below is my current code snippet:

#header {
    height: 40px;
    position: relative;
    margin: 80px auto 0;
}

#header ul {
    margin: 0 auto;
    width: 800px;
    padding: 0;
    list-style: none;
}

#header ul li {
    float: left;
    width: 97px;
}

#header ul li:nth-of-type(4) {
    margin-left: 217px;
}

#header ul li a {
    text-transform: lowercase;
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 12px 0 0 0;
    height: 28px;
    color: #000;
    -o-transition:.5s;
    -ms-transition:.5s;
    -moz-transition:.5s;
    -webkit-transition:.5s;
    transition:.5s;
}

#header ul li a:hover {
    color: #c4c4c4;
}

.logo {
    position: absolute;
    left: 50%;
    margin: -60px 0 0 -124px;
}

@media screen and (max-width: 800px) {
  .logo {
    bottom: 100%;
  }

  #header ul li:nth-of-type(4) {
    margin-left: 0;
  }
  
  #header ul {
    width: 600px;
    position: relative;
  }
}
<div id="header">
        <a class="logo"><img src="http://www.susanhudsonphotography.com/blog/wp-content/uploads/p4/images/logo_1368744984.jpg" alt="Whatever Photography" height="140" width="230" /></a>
        <ul>
            <li><a href="">About</a></li>
            <li><a href="">Galleries <span class="caret"></span></a></li>
                <ul class="sub-menu"> <!--if you comment out the sub-menu, you'll see the navigation as I want it to look-->
                    <li><a href="">Portraits</a></li>
                    <li><a href="">Landscapes</a></li>
                    <li><a href="">Personal</a></li>
                </ul>
            <li><a href="">Blog</a></li>
            <li><a href="">Info/Rates</a></li>
            <li><a href="">Prints</a></li>
            <li><a href="">Contact</a></li>
        </ul>
    </div>

Another issue I've run into is that when I resize my viewport to a mobile size, the logo image shifts to the top of the navigation links, partially hiding it from view. This is a separate challenge that needs addressing...

Answer №1

Here is a slight adjustment to your code that you can try out.

#header {
    height: 40px;
    position: relative;
    margin: 80px auto 0;
}

#header ul {
    margin: 0 auto;
    width: 800px;
    padding: 0;
    list-style: none;
}

#header ul li {
    float: left;
    width: 97px;
}

#header ul li:nth-of-type(4) {
    margin-left: 217px;
}

#header ul li a {
    text-transform: lowercase;
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 12px 0 0 0;
    height: 28px;
    color: #000;
    -o-transition:.5s;
    -ms-transition:.5s;
    -moz-transition:.5s;
    -webkit-transition:.5s;
    transition:.5s;
}

#header ul li a:hover {
    color: #c4c4c4;
}

.logo {
    position: absolute;
    left: 50%;
    margin: -60px 0 0 -124px;
}

@media screen and (max-width: 800px) {
  .logo {
    bottom: 100%;
  }

  #header ul li:nth-of-type(4) {
    margin-left: 0;
  }
  
  #header ul {
    width: 600px;
    position: relative;
  }
  
    ul.sub-menu { 
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        width: auto!important;
        overflow: hidden;
        padding: 0 1em;
        z-index: 2;
        background: white;
        
    }
    
    .sub-menu li { display: block; float: none!important; }
    
    .more { position: relative; }
    .more:hover .sub-menu { display: inline-block; }
<div id="header">

        <ul>
            <li><a href="">About</a></li>
            <li class="more"><a href="">Galleries <span class="caret"></span></a>
                <ul class="sub-menu">
                    <li><a href="">Portraits</a></li>
                    <li><a href="">Landscapes</a></li>
                    <li><a href="">Personal</a></li>
                </ul>
            </li>
            <li><a href="">Blog</a></li>
            <li>        <a class="logo"><img src="http://www.susanhudsonphotography.com/blog/wp-content/uploads/p4/images/logo_1368744984.jpg" alt="Whatever Photography" height="140" width="230" /></a></li>
            <li><a href="">Info/Rates</a></li>
            <li><a href="">Prints</a></li>
            <li><a href="">Contact</a></li>
        </ul>
    </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

Create an HTML document with a bottom section that shows up on every page when printed

I am currently working on creating a footer for an HTML file where the requirement is to have the footer displayed on each printed page. I came across a solution that fixes specific text to every page that I print. Here is the code snippet: <div class ...

Arranging Vue numbers

Vue data contains: data: { offices: requestData, selectedFloors: [ "3", "4", "5", "10", "11", "12", ...

Within a div element, there are links that I would like to hover over to emphasize the text

Hey there, I'm looking to make the text change color when you hover over the entire div. Currently, only the background changes color, but I want the text to as well. Thank you for your assistance! .shadow-box .service-box {text-align:center;borde ...

Update the value after verifying the element

I am trying to retrieve data from my database and update the values when an element is clicked (accepting user posts). The code I have written seems to work, but I encounter an error stating that props.actions is not a function when clicking on an element. ...

Learn how to manipulate data within a MongoDB database schema using Node.js and Mongoose: inserting, saving, and updating records

When inserting data into the MongoDB schema presented below, make sure that Employee name, Project name, and client name can be the same, but the employee ID must be unique. Duplicate entries are not allowed. var StatusSchema = new mongoose.Schema({ ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

What is the most efficient method in React for displaying an array as a table and wrapping every set of five elements with a <tr> tag?

Currently, I am working on rendering a table in React from an array of objects. My approach involves mapping the array to create table elements as shown below: var objects = response.data; var arrayOfTableElements = [] ...

validating if Object may be either 'null' or 'undefined'

In the code snippet below, I am attempting to verify whether hostel.country.address is null: return hostel.country.address && hostel.country.address.internalEmployeeIdentifier !== null || hostel.country.address.exter ...

Updating open graph meta tags using jQuery

I need assistance tweaking my code to dynamically modify the <meta property="og:title" content="CHANGE_ME"> tag when sharing a page containing an embedded video. While swapping the <h1> and <title> elements is functioning as intended, I& ...

Leveraging Leaflet or any JavaScript library alongside Typescript and webpack for enhanced functionality

Important: Despite extensive searching, I have been unable to find a resolution to my issue. My current endeavor involves developing a map library through the extension of leaflet. However, it appears that I am encountering difficulties with utilizing js ...

What is the best way to conceal an HTML element on a particular page that is already styled as (visibility: visible), but only if its child has a specific class assigned to it?

I have a grid of content and posts displayed on multiple webpages. Users can select specific items from the grid to navigate to more detailed information on another page. Each webpage has its own grid which I want to filter based on a specific class. The ...

Unable to remove spaces in string using Jquery, except when they exist between words

My goal is to eliminate all white spaces from a string while keeping the spaces between words intact. I attempted the following method, but it did not yield the desired result. Input String = IF ( @F_28º@FC_89º = " @Very strongº " , 100 , IF ( @F_28 ...

I am looking to enhance my JSON output using JavaScript

My JSON output appears as follows: {"intent":"P&P_Purchase","value1":{"date1":"30-Dec-19","prd_desc":"NEEM UREA OMIFCO (45 KG)","qty":"18MT","inv_no":"NRKT07003160"},"value2":{"date1":"25-Dec-19","prd_desc":"NEEM UREA IMP (45 KG)","qty":"18MT","inv_no ...

Embracing Micro Front End Development

Currently, I have a legacy AngularJS (Angular 1) app that cannot undergo any major changes at the moment. A new standalone app has been created using a newer stack. The goal is to integrate this new app into the old legacy app without making significant m ...

Exploring the Power of React's Ref API

Currently, I am following tutorials on Udemy by Max where he discusses how to work with Ref Api's in React 16.3. In one of the lectures, he demonstrated creating a ref inside a container class, not App.js, using this.lastref = React.createRef();. He ...

Create a draggable and droppable file upload container that functions similarly to an input with the type "file"

My goal is to create a native HTML5 multiple file upload feature using drag and drop functionality. Despite following tutorials like and http://www.html5rocks.com/en/tutorials/file/dndfiles/, I have not yet found the exact solution I am looking for. Ess ...

What is the most efficient way to gather all form inputs and then transmit them to an email address?

Could you please help me with creating a form page that collects user input data and sends it to my email? I am looking for solutions using only HTML, CSS, PHP, or JavaScript. Here is a preview of the page: Below is the PHP code snippet I've been wo ...

What steps should be taken to properly assess an AngularJS provider setup?

My provider definition looks like this: (function(angular) { angular.module('myModule', []) .provider('myService', function () { var service = {}; service.configureSomething = function () { }; service.$get = function () { ...

Obtain the child's identification number and include it in the parent element as an ARIA

I need assistance with a JavaScript (or jQuery) function to dynamically add an 'aria-labelledby' attribute to parent <figure> elements based on the ID of the <figcaption>. I have multiple images and captions set up in <figure>&l ...

React fails to display the content

Starting my React journey with Webpack configuration. Followed all steps meticulously. No error messages in console or browser, but the desired h1 element is not showing up. Aware that React version is 18, yet working with version 17. App.jsx import Reac ...