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

updating the row of an html table with elements from a javascript object

I am faced with the task of dynamically adding rows to a table based on the number of elements in my JavaScript object. The object consists of keys and arrays of values. userobject={ ID: [1,2,3] IP_Address: ["12.21.12 ...

Is it possible for JQuery to interact with an Access database for both reading and writing

Is it possible to utilize Jquery to establish a link with a Microsoft Access database? I have been exploring this possibility but haven't found any relevant information. This database won't be used for a production site, but rather for a small gr ...

Overflow and contain a flex item within another flex item

I am facing a challenge where I need to prevent a flex-child of another flex-child from overflowing the parent, ensuring it does not exceed the screen height or the height of the parent (which is also a flex-child). Here's the CodePen link for refere ...

Osclass Website Alert: Multiple H1 tag Issue Detected

I operate a website called MyMobPrice and utilize OsClass for its functionality. However, I am encountering an error on each product page stating "More than One H1 Tag found." The issue lies within the two sets of H1 codes designed for desktop and mobile ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...

Position footer at the bottom of the webpage and adjust the height to be filled with main content using Bootstrap 4

I am currently in the process of designing a bootstrap layout that includes a header navbar along with a footer. My goal is to ensure that the footer is always at the bottom of the page, even if there is limited content. Here's what I have implemente ...

Tips for positioning a label within the span element wpcf7-form-control-wrap

Is it possible to place the label for input elements inside the span element generated by the cf7 shortcode? I want to achieve this in order to make the label only visible when the input field is in focus. To accomplish this, both the label and the input ...

How to effectively manage multiple stylesheet links in React Native Expo development

Hello, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS/JS along with the basics of React. As a beginner developer, my current project involves creating a Study planner app for myself using React Native ...

What is the best way to showcase React Components in a inline format?

I'm attempting to place multiple React Components in the same line so that I can scroll through them later. Unfortunately, React and Material UI are not cooperating. The Card Components consist of a standard div element with text and an image inside. ...

Tips for switching images in a grid using jQuery

I have implemented an image grid using flexbox on a website I am developing. The grid consists of 8 boxes, and my goal is to randomly select one box every 2 seconds and assign it one of 12 random images. My strategy involves creating an array containing UR ...

What are some strategies to stop text from causing the container height of the <li> element to expand?

Link to my code on Plunker In my attempt to create a fluid layout, the sidebar of my site consists of a list of links. I am trying to make each <li> element a perfect square, but when I add text inside, it disrupts the dimensions and turns the squar ...

How can I ensure a DIV is shown on every sub-page?

Is there a way to dynamically display a <div id="chatbox"> in all subpages without needing to manually edit each one? For example, adding this code to the main index.html file and having it appear on /products/index.html. ...

Tracking the User Interface efficiency of a Java Applet running on a website

I have developed a Java Swing based messenger applet that runs smoothly on browsers. My next step is to host this applet on a webpage and allow users to test it out. I want to track the time they spend on each task while using the messenger, as well as any ...

Implementing a method to pass total value to the <td> tag in angular JS using controller

I am having trouble calculating the total IR for each table cell. Despite my efforts, the function is not working as expected and I can't figure out why. $scope.getTotalb = function () { var totalb = 0; for (var i = 0; i < $scope ...

Numerous websites with scroll-based navigation

I am currently building a unique website that includes scrolling in various directions without the use of a horizontal scrollbar. The design is similar to this image: https://i.stack.imgur.com/Ex2Bf.jpg. It is a one-page website where vertical scrolling ...

Reducing div size when clicked - Using JQuery version 1.9

I am looking to make a specific div shrink in size, while keeping all the data visible, each time a user clicks on a certain icon with the class legend-icon. For example, I want the div with the ID #Chart to shrink when clicked. This is the HTML code: &l ...

Is it possible for a website administrator to view the modifications I have made to the CSS and HTML code?

Is it possible for a website to detect any temporary changes made to their CSS or Html through developer tools, even though these changes are not permanent? ...

I am experiencing issues with a few of my design choices not functioning correctly

I am facing issues with some of my styles. I have put in a lot of effort, but there seems to be a problem with my menu. The last part of my CSS is not functioning correctly. When hovering over the menu content, I want it to turn black and... #site_menu { ...

How to Customize Password Input Fields in HTML

My input field is set to password type, allowing only a six-digit number: <fieldset> <label for="password-input">Enter New Pin</label> <input type="password" name="password" id="password-input" inputmode="numeric" minlength="6" ...

The script file (.js) isn't showing up on the PHP or HTML webpage

Experiencing a peculiar issue and seeking advice on alternative solutions due to my limited experience in this matter. The Issue: I currently have the following script running smoothly: <script type="text/javascript" id="myscript" src="http://piclau ...