Text overlaps when li element is nested inside another element

Seeking assistance in creating a CSS/HTML menu bar that triggers list elements on hover. Facing difficulty arranging the list in two columns.

Code Sample:http://jsfiddle.net/Km922/1/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Experiment</title>
    <style media="all">
        // CSS code here
    </style>
</head>
<body background="Images/global-splash-map.jpg">
    // HTML code here
</body>
</html>

When hovering over "menu1," it displays correctly, but when hovering over "menu2," the list elements overlap instead of appearing in a single line. Looking for help to resolve this issue.

Answer №1

You are facing a few issues. Firstly, floats and absolute positioning do not work well together:

.navigation ul li ul .first {
    float: left;
    position: absolute;
}

Additionally, stacking list items like you have done and trying to split them into two columns using floats is not the correct approach.

<ul>
  <li class="second">Argentina</li>
  <li class="second">Brazil</li>
  <li class="second">Bolivia</li>
  <li class="second">Chile</li>
  <li class="second">Colombia</li>
  <li class="second">Ecuador</li>
  <li class="first">Panama</li>
  <li class="first">Paraguay</li>
  <li class="first">Peru</li>
  <li class="first">Uruguay</li>
  <li class="first">Venezuela</li>
</ul>

Creating multi-column lists without specific HTML can be tricky. You may find this article helpful to start with:

Answer №2

It is important to note that using float and absolute positioning together on the same element is not recommended. Instead of relying on these methods, you can achieve perfect columns by implementing the CSS columns module.

I have made significant improvements to your CSS code:

http://jsfiddle.net/uPzxb/4/ (prefixes not included)

.navigation ul {
    margin: 0;
    padding: 0;
    list-style: none;
    left: 30px;
    position: relative;
    top: 20px;
}
.navigation > ul > li {
    float: left;
    height: 30px;
    margin-left: 15px;
    margin-right: 15px;
    position: relative;
}
.navigation li a {
   text-decoration: none;
}
.navigation li a:hover {
   text-decoration: underline;
}
.navigation li ul {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 30px;
    display: none;
}
.navigation li:hover ul {
    left: 0;
    width: 160px;
    display: block;
    columns: 2; /* utilize this for column layout */
}
.header-container {
    background: url(Images/nav-bg4.png) repeat-x scroll 0 0 transparent;
    height: 136px;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 901;

}
#apDiv1 {
    position: absolute;
    width: 200px;
    height: 115px;
    z-index: 902;
    top: 29px;
}

http://caniuse.com/#feat=multicolumn

Answer №3

When dealing with elements that may shift position, such as dropdown list items, it is advisable to switch their positioning from absolute to relative.

position: relative;

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

CSS code for a fixed fullscreen menu with scrolling

I have implemented a full-screen menu that covers the entire screen, excluding the header and menu tab bar. You can view it here: https://i.stack.imgur.com/h5cQa.png On smaller screens, the entire menu cannot be displayed at once, as shown here: https://i. ...

Looking for assistance in streamlining the code

On my Drupal page, I have multiple divs and a JavaScript function that checks for content inside each one: if ($('.accred').length) { $('#accred').show(); } else{ $('#accred').hide(); } // More code follows... T ...

I'm encountering difficulties utilizing ternary operators in TypeScript

I am struggling with ternary operators in TypeScript and need help understanding the issue. Please review the code below: const QuizQuestionContainer = ({ qa }: QuizQuestionContainerPropsType) => { const { question, option1, option2, option ...

Exploring methods to iterate through an extracted div using Selenium in Python

In my previous step, I encountered a challenge while trying to extract text from a div. After several hours of troubleshooting, I was finally able to achieve the desired result. However, the next step proved to be equally difficult as I needed to implement ...

swapping out an external CSS file in React for a new CSS file

My React app is quite large and includes four main CSS files (darkLTR, lightLTR, darkRTL, lightRTL), which may not be the most efficient setup. The templates were provided by my boss, and I was instructed to use them instead of Material UI, which I initial ...

SliderToggle and Div implementation in Internet Explorer (IE) using jQuery

I'm encountering an issue with jQuery's slideToggle function and a div specifically in IE. Here is the code snippet causing the problem: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.d ...

Centering items in Bootstrap 4, while excluding the first element

.c-footer { background: #000; padding: 20px 0; color: #fff; } .c-footer__logo { display: inline; } .c-footer__link { color: inherit; } .c-footer__link a { color: inherit; } .c-footer__link a:hover { color: #fff; } ...

Balancing heights with jQuery

Is there a way to set the height of an h3 element based on the tallest version of its siblings, but only for certain elements? I have a list where CSS organizes items into rows of 3. The last li element in each row has the class "endRow". I want to find t ...

Troubleshooting overflow problems when centering a UL menu with an unspecified width

Trying to create a demo where the scrollbars are removed when resizing smaller, but adding overflow: hidden causes issues with sub-menus. Demo Link HTML <nav> <ul> <li><a href="#">Menu Item</a></li> ...

unusual problem with referencing jQuery mobile pages and implementing click functionality

I am facing a strange issue with my index.html page, which is a website built using jQuery Mobile. The problem arises when I click on a button multiple times within a form on this page - it generates an alert to confirm that the button is working and then ...

Experiencing an inexplicable blurring effect on the modal window

Introduction - I've implemented a feature where multiple modal windows can be opened on top of each other and closed sequentially. Recently, I added a blur effect that makes the background go blurry when a modal window is open. Subsequently opening an ...

CSS: Implementing opacity on background images within an unordered list menu items

My menu is made up of ul/li items with background images, but I'm having trouble adjusting the opacity and alignment of the images. While there are resources online for changing background image opacity, such as this article: https://scotch.io/tutoria ...

What is the best way to create grid designs using less in bootstrap?

When using Bootstrap without a preprocessor, we include classes directly into our opening tags like this: <div id="El" class="col-md-1"></div> Personally, I usually work with Bourbon Neat and Sass. With Sass, I can import mixins in the rules ...

Retrieving the value of a formControl within a formArray is made possible through a reactive form in

How can I retrieve the value of ItemName in my HTML code? When I attempt to use {{invoiceForm.controls[i].items.controls.itemName.value | json}}, it returns as undefined. <form [formGroup]="invoiceForm"> <div formArrayName="items" *ngFor="let ...

Locate a specific element by utilizing xpath

Recently, I encountered a coding issue that has been puzzling me. Here's the html snippet in question: <div class="Main"> <div> <div class="display">First:</div> <div class="price">$0.01</div> ...

What are the steps for integrating a date and time picker bootstrap plugin?

Referencing a tutorial from http://www.w3schools.com/bootstrap/bootstrap_modal.asp: <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <div id="myModal" class="modal fade" role= ...

When JSF renders bootstrap buttons, they appear without any horizontal padding

There is a strange behavior that I cannot explain... I am working with a series of buttons: <h:commandButton value="foo" actionListener="#{foo.foo}" styleClass="btn btn-danger"> <f:ajax render=":form"></f:ajax> </h:co ...

Make sure that the iframe loads the next page with enough force to break out

My dilemma involves an iframe that loads the new tab page. When a user clicks on the thumbnail, it opens within the iframe. My goal is to have any subsequent load in the iframe redirected to window.top. Is there a way to achieve this without manually setti ...

Previewing a PDF file with multiple headers displayed above the text content

Creating multiple headers for each page of a PDF document using dompdf can be tricky. The headers you generated are fixed at the top with a width of 100%. However, you are facing an issue where on the second and subsequent pages, the header overlaps with t ...

Incorporating external files into Javascript code may cause issues with its functionality

Currently, I have a fully developed PHP theme that I am in the process of designing. Within this theme, I have integrated an image slideshow plugin to enhance its functionality. The following code represents the implementation of the image slideshow: &l ...