Unable to set background for menu container

I'm having trouble with setting a background color on a menu container that is inside a container with a width of 100%. I tried applying the background color directly to the menu container, but it didn't work. I managed to get it working by changing the display property to inline-block, but I don't understand why I need to do that when it's just a container with 100% width.

menu {
    width: 100%;
    background: #333;
}

.menu > ul {
    width: 100%;
    list-style: none;
    padding: 0;
    margin: 0;
}

.menu > ul > li {
    float: left;
    background: #e9e9e9;
    display: inline-block;
    padding: 0;
    margin: 0;
}

.menu > ul > li > a {
    text-decoration: none;
    padding: 1.5em 3em;
    display: inline-block;
    outline: 0 none;
}

.menu > ul > li > ul {
    display: none;
    background: #333;
    padding: 20px 30px;
    position: absolute;
    width: 100%;
    z-index: 99;
    left: 0;
    color: #fff;
    margin: 0;
}

.menu > ul > li:hover > ul {
    display: block;
}

This is how my HTML looks like:

<div class="menu">
  <ul>
    <li><a href="#">Home</a>
      <ul>
        This is also mega menu
      </ul>
    </li>
    <li><a href="#">Who are we?</a>
      <ul>
        This is mega menu
      </ul>
    </li>
    <li><a href="#">Services</li></a>
      <li><a href="#">Contact</li></a>
  </ul>
</div>

Additionally, when I try to float my UL to the right nothing happens. Have I messed up the display property in any element?

Answer №1

Due to the LI elements being floated, the container ends up with a height of 0 pixels. To resolve this issue, make sure to clear the container (.menu) first. Additionally, there seems to be a minor mistake in your MENU element - you need to include the DOT .menu to specify it as a class in your HTML code.

To properly clear floats, refer to this resource: https://css-tricks.com/the-how-and-why-of-clearing-floats/

.menu:before,
.menu:after {
    content: "";
    display: table;
}
.menu:after {
    clear: both;
}

For a functioning example on jsfiddle, visit: jsfiddle.net/4pgvzxs0/

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

Despite the fact that the toggle button in React is designed to function properly, it is currently not working as

Check out step #2 of this challenge where a toggle button is used to switch between displaying monthly and yearly values. Here's the accompanying code: import { NavLink } from "react-router-dom"; import { useContext } from "react"; ...

What sets apart the <script> tag with a type attribute from the standard <script> tag in HTML?

Similar Question: Is it necessary to include type=“text/javascript” in SCRIPT tags? While working on my HTML project, I noticed that the JavaScript code within script tags is evaluated even if the type attribute is not explicitly set to "j ...

Adjust the text input width appropriately for the cloze exercise

My JavaScript-generated fill-in-the-blank text is causing me trouble when it comes to adjusting the size of the input boxes. Unlike others, I know exactly what the user will or should be entering. If there is a blank space like this _______________, I wa ...

What causes the content of my container to disappear following a setInterval div swap in Internet Explorer?

While developing a website on Chrome and Firefox, I was informed that it also needs to function properly in Internet Explorer. I managed to resolve the double padding issue in IE, but now I'm facing a new problem. The content of my "grid" disappears a ...

Why isn't the Angular directive resolving the dynamic button text variable?

Trying to implement an iPhone-style Toggle Button using CSS in Angular. The issue I am facing is that the dynamic button text variable "{{v1['btn2']}}" is not being evaluated properly the first time, although it works fine with static text. URL ...

Getting the CSS for an element's "Active" state using jQuery

Is there a way to retrieve the CSS for the :active state using jQuery? I am looking to create dynamic code so that I don't need to manually adjust the jQuery every time I want to style an element. Edit To clarify, I am trying to avoid using .addClas ...

Maximizing Bootstrap's Potential: Achieving Right Alignment for Divs

I am facing a challenge with aligning my div in the layout_cshtml. Currently, it is aligned to the left and the search bar and dropdownlist are not displaying properly. This negatively impacts the user experience. Additionally, my navigation menu is not ...

Send the completed form to a designated web address

Here's the form I'm working with: @using (Html.BeginForm("Search", "Home", FormMethod.Get, new { enctype = "multipart/form-data", @class = "navbar-form navbar-left form-inline", @role = "search" })) { var numberOfVi ...

Incorporate a text input feature into the Pikaday modal dialogue box

I'm currently in the process of revamping Pikaday and want to swap out the year select for a text input. However, every time I add an input field, it ends up being unresponsive. Despite not being disabled, z-indexed to the background, or having any op ...

Starting up with card and a loop counter

Can anyone help me with getting my card bootstrapping implemented in my .html to be centered? Here is the code I'm working with: <h1> 10 Games Recommended Based on {{ selected }} </h1> <div class="container"> ...

What is the best way to design an HTML table that features alternating colors for each row and column?

I recently came across this image showcasing a beautifully designed table: https://i.stack.imgur.com/6K63q.png The current HTML code I have applies colors to every even line, including the year columns and subcolumns. Is there a way to assign unique mixe ...

Center an image vertically in an AdminLte content-wrapper division

I am currently utilizing an AdminLte template as the framework for my design. In the designated area where I need to insert the main content, it is structured in the following manner: <div class="content-wrapper"> <se ...

Element width shrinks inside container query

Can you explain why using container-type: inline-size renders the span normally but collapses the button? <span style="container-type: inline-size; outline: 1px solid blue;"> This is a span </span> <button style="container-type: inli ...

Using jQuery to submit a form and update values

I am currently working with a datatable that includes a detail column with an edit button. Upon clicking the edit button, I pass the ID as a parameter and fetch all the values associated with that ID to display in a form. However, when I edit the values an ...

Discovering methods to store browser credentials securely in jQuery

I need to prevent the login button from being enabled when either the username or password fields are empty. Check out the code snippet below: $(document).ready(function(){ $('input').on('keyup blur mouseenter', function(e) { ...

HTML various button designs - such as a cogwheel

I need a button on my Angular/Electron project that resembles a gear icon. I came across these resources: here and here. However, when I tried to implement them, they didn't work as expected. Currently, the button looks like this: <button class= ...

Is there a way to create a hover effect on an input number field within a certain class, causing the -webkit-inner-spin-button opacity to change to 1?

Is it possible to achieve this? I attempted the following, but it's not working: .myClass input[type=range]:hover > .myClass input[type=range]::-webkit-inner-spin-button{ opacity: 0; } .myClass input[type=range]:hover > input[type=range]:: ...

What is the best way to showcase the outcome using the <table> tag in HTML with columns containing 50 records each, except for the final column?

I have developed a program that is currently creating 540 different numbers. My goal is to present these 540 distinct numbers in columns with 50 records each (except for the last column where there may be fewer than 50 records) using the HTML <table> ...

What's preventing the buttons from shifting positions?

Having some trouble with my Minesweeper Web Game setup. My buttons are stuck in place and won't budge. Can someone help me figure out what's wrong? Here's the code snippet I've been working on (FYI, using NetBeans 8.1 IDE) HTML: <! ...

React Images: Carousel display issue with images not centered

Is there a way to center the selected image instead of it appearing on the left-hand side? Here is the current behavior: https://i.stack.imgur.com/SUWOK.jpg I am using the following packages in a sandbox environment with Next.js 11 and TailwindCSS 2.2.4: ...