CSS: dropdown with multiple columns

Check out my jsFiddle project here: http://jsfiddle.net/4z2Vx/

My goal is to design a dropdown/menu widget with multiple columns.

The picture on the left is from my website, while the one on the right is inside of jsFiddle. Besides the obvious differences in color and font, I'm puzzled by how jsFiddle seems to be adjusting the alignment. However, that's not the real issue - what's troubling me is that the first column keeps getting hidden under the second one... I've spent hours trying to fix this but can't seem to make them display next to each other.

Answer №1

The issue at hand is that when using float on elements that are positioned absolutely, it may not work as expected. One solution is to either restructure the HTML so that there is an absolutely positioned parent containing both .first and .second (as suggested in griswoldo's answer), or set a specific width for the dropdowns and then position them accordingly:

.dropdown li ul { 
    display: none; 
    border:black 1px solid;  
    position:absolute;
    background-color:white; 
    width: 150px;  /*set the width*/  
} 

.second {
    left: 150px; /*set the position*/
}​

http://jsfiddle.net/tYeDk/

Answer №2

The issue appears to be related to the

position: absolute;

declaration in the .dropdown li ul rule. By removing that and relocating the "collections" link outside of a ul element, both menus can be displayed side-by-side (with "collections" underneath). Additionally, setting float to left for both .first and .second will align them directly next to each other.

Take a look at this quick modification I made to your fiddle (may not be perfect, but it should help guide you): http://jsfiddle.net/4z2Vx/29/

Answer №3

After reviewing your code, it appears that you are in need of a megamenu. The issue lies in your sub menu ul being positioned absolutely, which is causing it to ignore floating.

To resolve this, you should create a child for your absolute positioned element.

Here is an example of how your HTML should look:

<div class="dropdown">
<ul id="menus"><li>Collections
        <ul class="sub">
            <ul>        
            <li><a href="#">ABC/BBC</a></li>
            <li><a href="#">Action</a></li>
            <li><a href="#">Animated</a></li>
            <li><a href="#">Children</a></li>
            <li><a href="#">Documentary</a></li>
            <li><a href="#">Drama</a></li>
            <li><a href="#">Cult</a></li>
    </ul>
    <ul>
            <li><a href="#">Family</a></li>
            <li><a href="#">Fantasy</a></li>
            <li><a href="#">Horror</a></li>
            <li><a href="#">Lifestyle</a></li>
            <li><a href="#">Mystery</a></li>
            <li><a href="#">Reality</a></li>
            <li><a href="#">Sciene fiction</a></li>
       </ul>
      </ul>


</li>
</ul>
</div>​

You will also need to update the CSS as follows:

.dropdown
{
    font-size:16px;
    font-weight:400;  
    line-height:40px;
    text-indent:15px;
    padding-right:15px;
    vertical-align:middle;
    display:inline-block;
}

.dropdown:hover
{
    background-color:green;
}

.dropdown li ul.sub { 
    display: none; 
    border:black 1px solid;  
    position:absolute;
    background-color:white;    
} 

.dropdown ul
{
    list-style-type:none;
    margin:0;
    padding:0;
}

.dropdown a
{
    display:block; 
    padding-right:15px;  
    line-height:30px; 
}

.dropdown a:hover {
    color: rgb(0,0,0);
    text-decoration: underline;
}

.dropdown li:hover
{
    background-color:green;
}

#menus ul.sub ul{
    float:left
}​

By implementing these changes, you can easily add more ul elements as needed.

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

Encountering an issue while trying to integrate controllers in AngularJS, facing an error while moving controller code from app.js to a separate controller file

I decided to delve into basic Angular code in order to gain a better understanding of how to create and declare controllers for the project I am currently working on. I found some sample code from an AngularJS tutorial on controllers at angularjs.org, but ...

leveraging embedded jetty for developing a web-based interface

As a newcomer to web development and using embedded Jetty, I have created the source code below in Eclipse IDE. I need to programmatically start the Jetty server and cannot use the command line to do so. The web interface must be lightweight due to the li ...

What is the best way to animate an element to move to another element and then return to its original position when clicked using JQuery?

Goal How can we implement a method to move item 3 to another container? If clicked again, how can we return the item to its previous position? This functionality needs to be applicable to all items. Javascript $('#item3').click(function(){ ...

The functionality of an external Javascript library fails to operate properly once the website is deployed to a hosting

Encountering a peculiar bug with my website at . The website is supposed to load both the JQuery and EasyUI libraries. JQuery is loading correctly as a function to print out "ready" when the page loads. The issue arises when trying to drag products onto th ...

Mobile device tab animation

I am facing a challenge with a tab containing four items that are vertically stacked on mobile devices. My goal is to animate the clicked li element to move to the bottom of the stack. After experimenting with CSS animations, I was able to achieve this eff ...

The functionality of right-clicking or context clicking in Internet Explorer 11 using Selenium WebDriver is experiencing issues

When attempting to open/select a context menu by right-clicking on an element on a page, I am encountering an issue. Despite using the Action class to execute the click operation, the contextClick() command seems to be performed at a different location on ...

Adjust the width of the table data cells dynamically in HTML and CSS based on the screen resolution and content

My React JS app has a page that features a table with tbody, tr, and td elements. The table consists of only two columns represented by the td tags. I am trying to make the first column adjust automatically based on screen resolution and content. I have ex ...

Exploring the SetCustomValidity Function in Angular

I am trying to utilize the html setCustomValidity method for setting up business validation, however the form is not blocking the custom validity. How can I implement this function in Angular? Here is the code snippet: export class AutoComponent { au ...

Leveraging the Xpath Contains function to locate an element with specific text content

For my Selenium Xpath locators, I have always used the Contains function successfully. However, I recently encountered an issue where it does not work for a TD element in a table, despite sending the correct text to the function. To replicate the problem ...

Email header image alignment issue in Thunderbird

I recently created an HTML email using a template and it looks great on most platforms, but not in Thunderbird. The header image is not aligned properly above the content as shown in the screenshot. Does anyone know how to troubleshoot this issue or have a ...

Turn off smooth scrolling in Bootstrap 5 using HTML and CSS techniques

On my website, I have implemented anchor links that activate smooth scrolling when clicked, thanks to the Bootstrap feature. However, the unique layout of my website requires me to turn off smooth scrolling in either the HTML or CSS code. I appreciate an ...

Utilizing property validation in your codebase

How can I set up my code to display a warning in the console if the value of the Prop is not a string? The player-name prop should always be a string. If this: <greet :player-name="5"></greet> contains a number, I want my code below to generat ...

When I tried to address one issue, my CSS ended up breaking. Any suggestions on how I can resolve the header

Edit: https://i.sstatic.net/kVeHk.jpg Picture 1 code: main.css body { font-family: arial; font-size: 14px; color: #1D2130; background-image: linear-gradient(top, rgb(200,225,245) 15%, rgb(240,240,240) 60%); // more CSS properties h ...

Responsive cards using Bootstrap styling

Currently trying my hand at mastering flexbox for responsive design with Bootstrap. In the picture, there are 4 cards where 2 should be displayed at the bottom and the other 2 at the top, but on mobile phones, they should all stack at the bottom. I attempt ...

Guide to making a circular button using Bootstrap and ensuring the text is perfectly centered

I am attempting to create circular buttons for numbers, but I am having trouble centering the text within the button. Ideally, I would like to have 15 circular buttons numbered from 1 to 15. Below is the code snippet: body { padding: 1em; } <link ...

How may I utilize CSS to generate a dynamically resizing rotated triangle that adjusts based on the text content?

Looking to add a dynamic badge in the top left corner of a div that adjusts its size based on the text content. Check out this example: http://jsfiddle.net/sbick/npxaasht/ <article> <div class="triangle"> <span>NEW</span ...

What steps can be taken to identify the referrer of external links when the target attribute is set to "_blank"?

Can't seem to capture the Referrer with PHP: $_SERVER['HTTP_REFERER']; Whenever visitors land on mywebsite.com from external-web-site.com via a link with target="_blank", for example: ... <a href="http://mywebsite.com" target ...

What could be causing the issue with the 100% height and 100% width not functioning properly on my ASPX page?

The CSS and HTML code work perfectly in an isolated test environment: body, html { height: 100%; min-height:600px; min-width:800px; overflow:hidden; margin:0;padding:0; } html {overflow:auto;} .fill {height:100%; width:100%; backgro ...

Revealing non-div elements with the CSS hover attribute

I'm looking for a way to utilize the CSS hover property on individual elements within a div in order to affect specific span elements. For example, hovering over "something1" should reveal "text1" and so forth. Is there a way to achieve this without ...

Is it possible to create a dynamic drop-down menu that retrieves data from a database using PHP?

<?php $sql="select software from software_table"; $qry=mysql_query($sql); $count=mysql_num_rows($qry); $row=mysql_fetch_array($qry); $data=['software_name']; ?> <?php for($i=0; $i<=count($data); $i++){ ?> <select name="software_ ...