Styling Dropdown Menus Using CSS

I'm facing some challenges with the following design requirements:

  • Need to add 8px padding on top and bottom of the navigation bar.
  • Title should be aligned to the left.
  • Links need to be right-aligned.
  • Navigation bar background should be 100% wide but only 1080px in width (centered).
  • Dropdown menu should have a horizontal border separating each link.
  • Padding for the dropdown menu should be 4px at the top and bottom, with an additional 4px indent.

For reference, here is the link to my code on jsfiddle: http://jsfiddle.net/6L8jJ/

Here's the HTML snippet:

<header>

  <div class="nav_top_bar">
    <nav class="nav_top_menu">
      <ul>
        <li class="nav_top_title"><b>title</b></li>
        <li><a href="" class="blue">option</a>
          <ul>
            <li><a href="" class="blue">suboption</a></li>
          </ul>
        </li>
        <li><a href="" class="green">option</a>
          <ul>
            <li><a href="" class="green">suboption 1</a></li>
            <li><a href="" class="green">suboption 2</a></li>
          </ul>
        </li>  
        <li><a href="" class="orange">option</a>
          <ul>
            <li><a href="">suboption 1</a></li>
            <li><a href="">suboption 2</a></li>
          </ul>
        </li>
        <li><a href="" class="purple">option</a></li>
        <li><a href="" class="yellow">option</a></li>
      </ul>
    </nav>
  </div>

</header>

And here's the CSS style applied:

.nav_top_bar {
 background-color: #333333;
 padding: 8px 0;
 width: 100%;
clear: both;
}

.nav_top_menu {
color: #c3c3c3;
font-size: 1em;
margin: 0 auto;
text-align: left;
width: 1080px;
}

.nav_top_title {
padding-right: 50px;
}

.nav_top_menu ul {
list-style: none;
text-align: left;
margin: 0;
padding: 0;
}

.nav_top_menu ul li {
display: inline;
float: left;
position: relative;
}

.nav_top_menu ul li a {
color: #c3c3c3;
display: block;
margin-left: 1px;
/* padding: 8px 16px; */
padding: 8px 20px 8px 0px;
white-space: nowrap;
}

.nav_top_menu ul li a:hover {
color: #ffffff;
}

.nav_top_menu li ul {
background-color: #333;
display: none;
}

.nav_top_menu li:hover ul {
border-top: 1px #ccc solid;
border-left: 1px #ccc solid;
border-right: 1px #ccc solid;
display: block;
position: absolute;
}

.nav_top_menu li:hover li {
border-bottom: 1px #ccc solid;
float: none;
font-size: 0.8em;
font-weight: bold;
padding-left: 4px;
text-align: left;
}

Answer №1

If you want to achieve the desired style, you'll need to experiment a bit more with CSS. Here is an example that may point you in the right direction:

Check out this JSFiddle for reference!

<header>

  <div class="nav_top_bar">
    <nav class="nav_top_menu">
      <ul>
        <li><a href="" class="blue">option</a>
          <ul>
            <li><a href="" class="blue">suboption</a></li>
          </ul>
        </li>
         // More list items go here
      </ul>
    </nav>
    <h1>Title</h1>
  </div>

</header>

Here is an overview of the CSS used:

.nav_top_bar {
  background-color: #333333;
  padding: 8px;
  width: 100%;
}
h1 {
    font-size: 14px;
    color: #fff;
}
// More CSS styling rules go here

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

Styling Angular with FullCalendar - Personalized CSS Design

I'm struggling with custom styling using FullCalendar for Angular. Specifically, I need to change the background color of the 'More Events' Popover, but no matter what I attempt, my styles aren't taking effect. I've been trying to ...

What is the best way to adjust an image's dimensions to fit a specific screen size without distorting it on a webpage?

Currently working on a website, and the top section has a small column on the right with an image taking up the majority of the space to the left. The problem arises when the image spills over the screen size, messing up the overall layout of the page. I d ...

Extracting data from a textbox in a PHP view and passing it to the controller

Is there a way to retrieve the values entered in a text box on a webpage and pass them to a PHP controller for display? VIEW <input type="text" name="Quantity" id="Quantity" value="" /> CONTROLLER function cartoutput() {//echo "hi"; $category ...

The relationship between parent and child elements in CSS

Recently on Stack, I came across a question that was answered correctly. The query was about how to target the first two li elements after each occurrence of .class1. <ul> <li>Something</li> <li class="class1">Important</li ...

Update the iframe located on a different webpage

I am looking to create a dynamic button feature on my website where clicking a button on page1.html will open a new page (portal.html) with an iframe displaying the URL specified in the button. The setup involves two main pages: page1.html, and portal.htm ...

In Java, use an HttpRequest to retrieve the webpage content and ensure that the process waits until the JavaScript has finished

Is there a way in Java to wait for partial content of a webpage to be loaded by JavaScript before initiating the rest of the process? The webpage contains script that loads additional content once the page is fully loaded, and I need to ensure that the p ...

Trouble with Bootstrap scrollspy upon resizing the browser dimensions

Welcome to all you talented Stackoverflow bootstrappers! I have a burning question that I believe has not yet been asked, and I am eager to discover the solution. Let me walk you through how to replicate this bug: First, navigate to the following page: ...

What is the best way to retrieve the parent element quickly using Jquery?

html: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all"><input class="spinner1 ui-spinner-input" id="q1" name="value" value="1" min="1" aria-valuemin="1" aria-valuenow="2" autocomplete="off" role="spinbutton"><a class="ui ...

AngularJS: Number Retrieval and Output

Is it possible to utilize AngularJS to restrict a textarea to only accept numbers and return characters (/n/r)? I'm aiming for a format similar to the following: <textarea> 2344 5335 55555 2222 234 </textarea> The example above showcases ...

Navigating through intricate HTML structures with JavaScript can be challenging

Snippet of 01.js code: var childElements = document.getElementById('selected-plays').getElementsByTagName('li'); function getRequiredElements(childElements) { var listItems = []; var i = 0; for(i = 0 ; i < childElements. ...

The jQuery prop method seems to be malfunctioning

Here is the HTML code snippet: <ul class="expander-list" id="category"> <li> <div class="radio" style="padding-left:0px"> <label> <input type="checkbox" id="all" ...

Manipulating the DOM by linking to a div and using JSON information for opening and closing

I have retrieved data from a JSON file and displayed it as a link. I am looking to implement functionality that allows me to hide and show a div when the link is clicked. Below is the code I'm currently using: $(document).ready(function() { $ ...

Checking for validation in the datePicker input field

I have a textbox field labeled "Expires on" and I am currently utilizing the DatePicker feature. $("#sfExpiresOn").datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, ...

Tips for ensuring compatibility of CSS in IE7 and IE8

Despite using the following styles to dynamically display alternative colors in my HTML code, I am facing compatibility issues with IE7 and IE8. Surprisingly, everything works perfectly fine on IE9 and above. It seems these styles are not supported by IE7 ...

Having trouble with the JQuery scrollTop animation? Getting the error message "Cannot read property 'top' of undefined"?

I am having trouble with my jquery animation scrollTop function. Whenever I click on <a>, it takes me to the anchor without any animation. Can someone please provide a solution for this issue? Upon running the webpage, I encountered an error message ...

Unique option preservation on customized HTML select menus - Maintain original selection

Currently, I am attempting to replicate a custom HTML select based on the example provided by W3 Schools. You can view the demo through this link: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select The issue I am encountering is that ...

Is there a way to adjust the size of the canvas element in HTML without compromising quality or resorting to zooming?

I'm currently working on a basic modeling application for the web. The main component of my app is a canvas element that I'm trying to size correctly. However, when I set the height and width using CSS, it scales the entire canvas and compromises ...

Move a div element as you scroll down the page

I currently have a div that appears when a user scrolls down a page. When clicked, it sends you back to the top of the page. Right now, it simply fades in and out, but I would like it to slide in from the right side of the page. Below is my existing code: ...

Resize Pictures in Carousel for Smartphones

Currently, I am in the process of optimizing my website for mobile devices. The NivoSlider, which is used throughout the site, does not seem to scale well on smaller screens. Despite trying various methods in the mobile section of my stylesheet such as " ...

Streamlined method for handling child elements within the DOM

I am striving to achieve a randomized shine effect on specific text using CSS animation. I currently have a functioning code, but I am interested in learning if there is a more efficient or straightforward solution. HTML: <span class="foo"> < ...