Problems revolving around the fundamental CSS drop-down menu concept

I'm struggling to center a CSS drop-down menu on my webpage without causing the drop-down effect to break. No matter what I try, it seems to mess up. What am I missing?

Snippet of code:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap{max-width:1280px;width:100%;margin:auto;}

nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}  
<body>
    <div id="wrap">
        <nav>
            <ul>
                <li><a href="#">Menu 1</a></li>
                <li><a href="#">Menu 2</a></li>
                <li>
                    <a href="#">Menu 3</a>
                    <ul>
                        <li><a href="#">Drop 1</a></li>
                        <li><a href="#">Drop 2</a></li>
                        <li><a href="#">Drop 3</a></li>
                    </ul>
                </li>
                <li><a href="#">Menu 4</a></li>
            </ul>
        </nav>
    </div>
</body>

Answer №1

To adjust the width and margin of the ul element, follow these simple steps:

#wrap nav ul {
    width: 430px;
    margin: auto;
}

Snippet to implement the CSS rules:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap {max-width:1280px;width:100%;margin:auto;}

nav ul {width: 430px; margin: auto;}
nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}
<body>
    <div id="wrap">
        <nav>
            <ul>
                <li><a href="#">Menu 1</a></li>
                <li><a href="#">Menu 2</a></li>
                <li>
                    <a href="#">Menu 3</a>
                    <ul>
                        <li><a href="#">Drop 1</a></li>
                        <li><a href="#">Drop 2</a></li>
                        <li><a href="#">Drop 3</a></li>
                    </ul>
                </li>
                <li><a href="#">Menu 4</a></li>
            </ul>
        </nav>
    </div>
</body>

If you prefer not to specify the ul width, you can make it inline-block and align its parent element's text to the center.

Alternate snippet with revised alignment:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap {max-width:1280px;width:100%;margin:auto;}

nav {text-align: center}
nav ul {display: inline-block; margin: auto;}
nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}
<body>
    <div id="wrap">
        <nav>
            <ul>
                <li><a href="#">Menu 1</a></li>
                <li><a href="#">Menu 2</a></li>
                <li>
                    <a href="#">Menu 3</a>
                    <ul>
                        <li><a href="#">Drop 1</a></li>
                        <li><a href="#">Drop 2</a></li>
                        <li><a href="#">Drop 3</a></li>
                    </ul>
                </li>
                <li><a href="#">Menu 4</a></li>
            </ul>
        </nav>
    </div>
</body>

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

Adjust the table height to 25% and include a vertical scrollbar using Bootstrap styling

I have been working on designing a web page layout that includes two status tables positioned side by side at the top. While I have successfully set this up, I am facing an issue with implementing a vertical scroll feature specifically for the right-hand t ...

Customizing the text color of steps in a v-stepper component using Vuetify.js

When working with the v-stepper component of VuetifyJS, it is easy to change the color of the steps themselves by using the `color` prop. But how can I alter the text of each step? Specifically, I am looking to modify the color of the text that says Name ...

Why is my AngularJS application triggering two events with a single click?

<button id="voterListSearchButton" class="serachButton" ng-click="onSearchButton1Click()">find</button> This button allows users to search for specific information: $scope.onSearchButton1Click = function() { var partId = $("#partIdSearch" ...

Annoying AngularJS Scrolling Problem

I am facing an issue with a Container that loads multiple views <md-content flex id="content"> <div ng-view></div> </md-content> One of the loaded views has the following structure: <div layout="column" id="selection-whit ...

Switching a conditional className to a styled component

Currently, I am in the process of converting my project from plain CSS to styled components using styled-components. So far, I have successfully converted all of my components except for one. I have looked at various examples on Stack Overflow but none of ...

Struggles with CSS hover effects on text and buttons

Beginner in the world of HTML & CSS Struggling with the following: Hover Effect: Looking to implement a hover effect where hovering over either the staff name text or the circle button above it triggers the appearance of a square border (refer to 'A ...

Placing a div over a JavaScript element

Is it feasible to overlay a div(1) with a background image on top of another div(2) that contains JavaScript (like Google maps API v3)? I have experimented with z-index without success, and I am unable to utilize absolute positioning because I depend on t ...

CSS Stylelint rule to detect the 'missing selector' issue

My current process involves using stylelint to identify any CSS errors before building and deploying our site. However, a recent commit includes code that fails the CSS parser/minifier but passes stylelint validation. .example-class , /*.example-class-two ...

How can we use jQuery to extract an HTML element's external stylesheet and add it to its "style" attribute?

My goal is to extract all CSS references from an external stylesheet, such as <link rel="stylesheet" href="css/General.css">, and add them to the existing styling of each HTML element on my page (converting all CSS to inline). The reason for this re ...

What is the best way to maximize vertical space in every element on a page?

How can I achieve a layout similar to the image shown? I want three distinct sections: a navigation bar fixed at the bottom, and left and right sides each taking up 50% of the screen. Additionally, all boxes on the left side should have equal height. I am ...

The scrolling function halts as soon as the element that was middle-clicked is deleted from the

I am knee-deep in developing my own React virtualization feature and encountering a minor annoyance. When I middle click on an item in the list and start scrolling, the scrolling stops once that item is removed from the DOM. My initial thought was that the ...

Text and Image Coordination

I'm encountering some challenges with a specific section. My goal is for it to resemble this. However, I am currently achieving something different, like this. .timeline { text-align: center; } #about-us { ul { &:before { ...

Create a stylish Bootstrap 3 modal featuring an image and convenient scroll bars

My dilemma is with a modal dialog containing an image of fixed width set at 1500px. I want the image to maintain this size and for scroll bars to appear on smaller screen sizes, allowing users to scroll through the entire image. Essentially, I need the mod ...

The sub-menu is being obscured by a PDF element in Internet Explorer, causing visibility issues

I have an object type tag on my page that displays a PDF file. Everything is functioning correctly, but in Internet Explorer, my sub-menu is being hidden behind the object tag. var objTag = $('<object></object>') .attr({ data: source ...

What impact does utilizing CSS 3D Transforms have on search engine optimization (SEO)?

Google emphasizes the importance of not hiding content, as it may not be counted by search engines. However, Google Cache has shown some discrepancies in understanding certain elements correctly. It's possible that what Google perceives as hidden migh ...

In search of a regular expression for identifying any of several classes

In the process of upgrading all my websites, I decided to redesign them so that all URL's default to lower case. This meant changing all image names and paths to lower case as well, which was accomplished using regexes. However, this action inadverten ...

Using single-line ellipsis with the Material-UI select and option components

I have a TableHead Component with multiple columns. Among them is a <Select> element that allows users to filter the table content based on one of four statuses. I am trying to implement an ellipsis at the end of the option string if it exceeds its c ...

Tips for merging identical media queries into a single query using SASS

Previously, I organized my media queries in LESS by combining them at a single place like this LESS: .media-mixin(@break) when (@break = 1200px) { .abc{ color: red; } } .media-mixin(@break) when (@break = 1200px) { .xyz{ color: yellow; ...

Unexpected Type Error: Unable to Assign 'Render' Property to Undefined

At the moment, I am encountering these specific issues: An Uncaught TypeError: Cannot set property 'render' of undefined The element #main cannot be found This is the code that I currently have. Despite looking for solutions from various sourc ...

Adjust the size of both the right and left price scales on TradingView Lightweight Charts

Is it possible to set a fixed width for both the right and left price scales on a chart? Let's say, 50 pixels for the right price scale and 70 pixels for the left price scale? For a working example, please visit https://jsfiddle.net/TradingView/cnbam ...