The left margin on the CSS menu appears to be a bit unusual

Looking for help with a CSS code for a horizontal menu:

.vertical-nav {
    height:auto;
    list-style:none;
    width: 100%; /******* MODIFIED ********/
    margin-top:0;
    margin-bottom:35px;
    margin-left:0;
}
.vertical-nav li {
    height: 25px;
    margin: 0;
    padding: 5px 0;
    background-color: #666;
    border: none;
    text-align: center;
    display: inline-block;
    float: left;
    width: 100px;  /******* MODIFIED ********/
}
.vertical-nav li:hover {
    background-color:#f36f25;
    color:#FFFFFF;
}
.vertical-nav li a {
    font-family:Calibri, Arial;
    font-size:18px;
    font-weight:bold;
    color:#ffffff;
    text-decoration:none;
}
.vertical-nav li.current {
    background-color:#F36F25;
}
.vertical-nav li.current a {
    color:#FFFFFF;
}
vertical-nav ul li ul {
    display:none;
    list-style-type:none;
    width:125px;
    padding:0px;
    margin-top:3px;
    margin-left:-5px;
}
vertical-nav ul li:hover ul {
    display:block;
}
vertical-nav ul li:hover ul li {
    background-color:#555555;
    width:125px;
    height:30px;
    display:inline-block;
}
vertical-nav ul li ul li:hover {
    background-color:#333333;
}
vertical-nav ul li ul li a {
    color:#FFF;
    text-decoration:underline;
}
vertical-nav ul li ul li a:hover {
    text-decoration:none;
}
.vertical-nav li ul {
    display: none;
    margin-top: 10px;
    padding: 0;
}
.vertical-nav li:hover ul {
    display: block;
}
.vertical-nav li:hover .sub-menu
{
    display: table;
}
.sub-menu li
{
    width: 100%;
    min-width: 180px;
    white-space: nowrap;  
    display:table-row;
    z-index:1;
    position:relative;
}
.sub-menu li a
{
    display:inline-block;
    padding: 0 10px;
}

Struggling to remove the left margin from the menu styling.

Tried adjusting margins without success. Here's the jsFiddle link: http://jsfiddle.net/QSEGR/

Answer №1

Actually, it's not about margin.

When it comes to HTML list elements like <ul>, web browsers actually apply a padding-left (for example, Google Chrome sets -webkit-padding-start: 40px;).

To override this default styling, you can set padding: 0; on the <ul> element:

.vertical-nav {
    padding: 0;
}

Check out the JSFiddle Demo

Note: Web browsers also add a default margin: 8px; to the <body> element, which you can reset with:

body {
    margin: 0;
}

So, what's the best approach?

Since different browsers may have different default behaviors and CSS declarations, most developers use a technique known as CSS Reset to start with a clean slate and override these defaults.

For a popular CSS Reset by Eric Meyer, check out Eric Meyer's CSS Reset.

Answer №2

Make sure to include padding: 0; for the list that is not ordered.

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

CodePen displaying CSS errorsI'm having trouble getting my CSS

I'm facing an issue with my CSS code and I need your help to figure out what's wrong. I believe it's a simple problem that someone can quickly identify just by looking at the code. Any assistance would be greatly appreciated! Here is the co ...

The correct alignment of images is crucial for a website's overall aesthetics and functionality

My column displays images aligned in the center when viewed locally, but the alignment changes when hosted remotely. Here is the image when locally hosted: https://i.sstatic.net/I56Dg.png And here is the remote host image: https://i.sstatic.net/ePoAn.p ...

Exclude a particular row from a table when there is no identifier

My PHP code generates a basic table. <table border="1" id="control> <tbody> <tr>...</tr> <tr>...</tr> <tr>...</tr> //Row #3 <tr>...</tr> <tr>... ...

Submitting form data via Ajax to be processed by Express.js with body parser

Having trouble sending form data to Node.js via AJAX. Using Express with body parser on Node.js However, receiving undefined in req.body. After searching extensively online and trying various solutions without success, I am seeking assistance on the c ...

add element into the center of the div

I am attempting to insert an element (such as a div) in the middle of another div and directly after two line breaks. Below is an example of my code. The length of the div may vary. <html> <head> <title></title> <script ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

Discrepancy between relative path resolving in IE7 and Firefox

Here's the code I have inside my Smarty tpl file (or any simple HTML): <img src="../images/blah.jpg" /> When I view it in Firefox, the path resolves to: localhost/app/index.php/images/blah.jpg (and the image doesn't load). However, in IE7 ...

A new URL must be refreshed for the link to properly bind the data received from the API call with the subscribe method

In my Angular application, I have a URL link that fetches data from the backend API using the subscribe method in a component. The expected behavior is that when this URL is accessed in a browser, the data should be displayed in the corresponding HTML comp ...

Creating a HTML5 Geolocation object using Python: A step-by-step guide

For my software analytics testing, I am looking to send GET requests with a geolocation object that includes variable timestamps and locations. The website utilizes the HTML5 navigator.getcurrent.location() function. While I can use the random module to r ...

What is the best way to iterate through two separate "div" elements that contain different data in each?

Currently, I am working on implementing a timeline feature in my web application. I have two distinct divs, each containing different data. Despite utilizing a foreach loop, I am encountering unexpected issues with its functionality. Could someone please a ...

What strategies and techniques should be considered when creating websites optimized for mobile devices?

With a wealth of experience in programming languages like Java, Python, and C, I actively engage in self-study to enhance my skills. While I have dabbled in creating mobile-friendly websites, upon reviewing my work, it is evident that my frontend developme ...

Attempting to incorporate a new CSS sub menu alongside the current one

Having trouble with menu styling, I'm new to this and trying to add a sub menu to an existing drop down menu. Check out my JSFiddle This is the CSS code: .menu_main { float: left; width: 60%; } #access .menu { list-style: none; font-weight: normal ...

Rotating an arrow image on an accordion using jQuery

I have been working on this page located at Unfortunately, my attempts to showcase the issue in a jsfiddle were unsuccessful. The webpage features an accordion-style FAQ section, and I am attempting to make the arrow rotate 90 degrees when a question is ...

What steps can I take to ensure that an image does not exceed the size of its parent tag?

Is there a way to ensure that items placed in a container do not exceed the boundaries of the container? I am trying to use my logo as the home button, but when I set the image height and width to 100%, it becomes larger than the nav bar. The CSS for the n ...

Is it possible to modify the border colors of separate elements in Bootstrap 5?

<div class="m-3 p-3 border-5 border-top border-danger border-bottom border-primary"> Can borders be styled with different colors? </div> Is there a method to achieve this, possibly using sass? ...

Is it possible to shift the "ul" block of the navigation bar to the right without compromising the responsiveness toggle button functionality?

I'm currently using bootstrap NavBar and encountering an issue where moving the ul block to the right is disabling the toggle button responsiveness. <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" c ...

bootstrap accordion animation

#accordion { .card { border: unset !important; .card-header { @extend .text-xx-large; background-color: $white; padding: ($spacer * 3.44) 0px ($spacer * 3.44) 0px; } } } .card-header { h5 { a{ color: $d ...

The utilization of `ngSwitch` in Angular for managing and displaying

I am brand new to Angular and I'm attempting to implement Form Validation within a SwitchCase scenario. In the SwitchCase 0, there is a form that I want to submit while simultaneously transitioning the view to SwitchCase 1. The Form Validation is fun ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Issue with Bootstrap nav-bar not collapsing after hamburger icon has been used to expand it

Having a Bootstrap-related issue that I hope you can help me with. Whenever I expand my menu using the burger icon, it fails to collapse when I try to close it. Can you please point out my mistake and provide some guidance on how to prevent this from hap ...