What might be causing the overflow of my page width by my navbar?

I am currently working on creating a product landing page for freecodecamp, and I'm facing some issues with my navbar - the first element I need to get right. Could you help me identify what's wrong with my code?

Additionally, I would like to add an image behind the navbar. You can view the image here.

Coding can be frustrating, especially when dealing with all these small but crucial details that seem to go off track. I often find myself wishing for someone to explain the code to me, but unfortunately, that luxury isn't always available.

<div class="nav-bar-wrapper">
       <nav id="nav-bar">
          <ul>
            <li><a class="nav-link" href="#">link1</a></li>
            <li><a class="nav-link" href="#">link2</a></li>
            <li><a class="nav-link" href="#">link3</a></li>
            <li><a class="nav-link" href="#">link4</a></li>
          </ul>
        </nav>
</div>


    html{
         width: 100%; 
         height: auto;
    }

    .nav-bar{
       width: 100%;
       height: 7vh;
       background-color: #555; 
    }
    #nav-bar a.active {
       background-color: #04AA6D; 
    }
    .nav-link{
       text-decoration: none;
       padding: 3px;
       display: inline-block;
       color: coral;
       width: 25%;
     }
    .ul, li{
       display:inline-block;
     } 
   ul{
       display: inline;
       text-align: center;
       padding:10px;
       border: 3px solid;
       font-size: 20px;
       text-align: justify;
    }
    li{
        margin: auto;
        display: inline;
    }
    .nav-link:hover{
         color: green;
    }
  

Answer №1

If you set all item widths to 25%, they will appear the same size on the full width of the screen. However, factors like margin, padding, border, etc., may reduce the actual space to around 98%. Adjusting the width to 24% or 23% can help resolve this issue.

.nav-link{
       width: 23%;
}

Using flex is a better practice. To implement flex, simply set:

html {
    width: 100%;
    height: auto;
}

.nav-bar {
    width: 100%;
    height: 7vh;
    background-color: #555;
}

#nav-bar a.active {
    background-color: #04AA6D;
}

.nav-link {
    text-decoration: none;
    padding: 3px;
    display: inline-block;
    color: coral;
    width: 100%;
}

ul,
li {
    display: inline-block;
}

ul {
    display: flex;
    text-align: center;
    padding: 10px;
    border: 3px solid;
    font-size: 20px;
    text-align: justify;
}

li {
    margin: auto;
    display: inline;
}

.nav-link:hover {
    color: green;
}
<div class="nav-bar-wrapper">
       <nav id="nav-bar">
          <ul>
            <li><a class="nav-link" href="#">link1</a></li>
            <li><a class="nav-link" href="#">link2</a></li>
            <li><a class="nav-link" href="#">link3</a></li>
            <li><a class="nav-link" href="#">link4</a></li>
          </ul>
        </nav>
</div>

Answer №2

If you're looking to update your css, consider using the following code:

ul {
    display: flex;
    align-items: stretch;
    justify-content: space-between;
    width: 100%;
    margin: 0;
    padding: 0;
}

li {
    border: 3px solid;
    display: block;
    flex: 1 1 auto;
    list-style-type: none;
    text-align: center;
}

https://i.stack.imgur.com/jnowL.png

fiddle: https://jsfiddle.net/L039vgsf/1/

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

Start a server without the need to access it through a web browser

On my back-end server (PHP 7.2), I am calling a .php file from index.html every few seconds. <!DOCTYPE html> <html> <head> <title>Title of the document</title> <script src="https://ajax.googleapis.com/ajax/libs/jqu ...

Tips on how to showcase list items in a horizontal manner while maintaining consistent spacing and ensuring they are

I have a list of items denoted by the "li" tag, and I am looking for a way to display them horizontally or in a neat format. Below is the HTML code snippet: <!doctype html> <html lang="en" ng-app="app"> <head> ... </head> <bo ...

Modify the drop-down value based on the selection of a checkbox in HTML

Here is the updated version that I am stuck with. <base href="http://skins4device.com"/> <link rel="stylesheet" href="/gasss/css/ghd.css" type="text/css" media="all"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.m ...

What is the best way to ensure consistency in a value across various browsers using Javascript?

I am currently developing a feature on a webpage that displays the last update date of the page. The functionality I am aiming for is to select a date in the first input box, click the update button, and have the second box populate the Last Updated field ...

Troubleshooting issues with drawing images on HTML5 canvas: experiencing unexpected outcomes

As I am diving into learning how to use the HTML5 canvas element, I have had success in drawing rectangles, utilizing getImageData, manipulating pixels, and then using putImageData to witness the color changes of the rectangles, among other capabilities. ...

What is the best way to create a scrollable horizontal element using bootstrap?

Here is the specific issue I'm facing: https://i.sstatic.net/oi94N.png The problem is that the icons are wrapping onto 2 lines, which is not the desired outcome. This is what I currently have: <li class="list-group-item participants-grouping-li" ...

Peek into the Outlook Calendar Event's Source Code

We are interested in analyzing the source code of calendar events in Outlook across all versions from 2010 to 2016 due to its integration with multiple platforms like Google, Exchange, and Exchange Online. Is there a method available to obtain the HTML so ...

I have managed to update the Immutable and Primitive Data-Types in JS. Now, the question arises - are these truly Primitives or are the concepts in

In JavaScript, there are 4 primitive data types that store values directly: String, Number, Boolean, and Symbol. I am excluding undefined and null from this list as they are special data types with unique characteristics. One key feature of primitives is ...

Assigning a new classification to the primary object in the evolving array of objects

I'm working with an element that loops through all the objects using v-for and has a CSS class named top-class{}... I need to dynamically add the top-class to the first object (object[0]) and update it based on changes, removing the old top-class in t ...

Ways to navigate by percentage in react

I'm working on a flexbox that scrolls horizontally with boxes set to 25% width. Currently, I have it scrolling by 420px which is causing responsive issues. Is there a way to implement the scroll using percentages instead of fixed pixel values in React ...

position the input and span elements side by side

Need help aligning an input and a span element next to each other. The span is currently positioned below the input, but I want it to be on the right side of the input. I am trying to create a search bar using this input and span tag, so they need to be al ...

Creating a personalized stepper component (using custom icons) using HTML

Seeking guidance on how to achieve a design similar to the image below using HTML and CSS. After conducting extensive research, I have not been able to find a suitable solution. I attempted to modify this answer to fit my requirements, but it did not prod ...

Tips for avoiding a 500 GET error due to an empty request during page loading

I recently encountered an issue with a form containing a dependent drop-down menu that reloads after form submission to preselect the main choice that was made before submission. The problem arises when the page initially loads without any parameters being ...

Which is more effective: coding with just plain JavaScript and CSS, or utilizing frameworks?

As a student, is it more beneficial to focus on utilizing pure JavaScript & CSS or frameworks? And which approach is best suited for the professional field? ...

Concealing and Revealing a Div Element in HTML

Every time the page is refreshed, I am encountering a strange issue where I need to double click a button in order to hide a block. Below is the code snippet for reference: <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

Is there a way to prevent a span from appearing at the end of a line?

Imagine a scenario where I have a paragraph with various span elements that are styled using classes. What if there are numbers interspersed within the text for easy reference, and I want to ensure they do not end up at the end of a line? For example: 1 ...

PHPMailer issue with rendering HTML in email body

Our email system utilizes PHPMailer to format emails, but when the emails are sent and opened in email clients like Gmail and Yahoo, they display as text instead of HTML. We have tested different email clients, such as Outlook, which allows us to attach a ...

A variable in Javascript storing data about jQuery DOM elements

Currently, I am using the slick slider for my development (http://kenwheeler.github.io/slick/) In JavaScript, there is an event that returns a variable called 'slick' When I print the variable using console.log, this is what I see: From what I ...

Creating JavaScript functions that accept three inputs and perform an operation on them

Apologies in advance if there are any silly mistakes as I am new to Javascript. I want to add "salary", "pension", and "other" together, then display the result in an input field when a button is clicked. Not sure if I have used the correct tags in my cod ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...