The navigation menu collapses upon itself when resizing the browser window

Every time I try to adjust the browser size, the navigation menu seems to collapse within itself, creating a strange effect. I can't figure out what's causing this issue.

I've experimented with max-width and sometimes including the "wrapper" in a div only adds to the confusion.

@font-face {
  src: url(fonts/Modric.ttf);
  font-family: Modric;
}

@font-face {
  src: url(fonts/Orkney-Regular.ttf);
  font-family: Orkney;
}

@font-face {
  src: url(fonts/Made-Bon-Voyage.otf);
  font-family: Made-Bon-Voyage;
}

* {
  box-sizing: border-box;
}

body {
  background-color: #262c2c;
}

.navbar {
  max-width: 75%;
  height: 100px;
  margin-right: auto;
  margin-left: auto;
}

.navbar a {
  float: left;
  padding: 40px;
  background-color: #262c2c;
  text-decoration: none;
  font-size: 25px;
  width: 25%;
  /* Four links of equal widths */
  text-align: center;
  color: #dae1e7;
  font-family: Orkney;
}

h1 {
  text-align: center;
  font-family: Orkney;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
  opacity: 50%;
  margin-left: auto;
  margin-right: auto;
}

#wrapper {
  max-width: 1000px;
  margin-left: auto;
  margin-right: auto;
  padding-right: 50px;
  padding-left: 50px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="navigation.css">
  <title>Pasetta Studios</title>
</head>

<body>

  <div id="wrapper">
    <div class="navbar">
      <a href="#index.html">Home</a>
      <a href="#about.html">About</a>
      <a href="#projects.html">Projects</a>
      <a href="#contact.html">Contact</a>
    </div>


    <img src="images/top-image.jpg" alt="plants">
    <img src="images/second-image.jpg" alt="benches">
    <img src="images/third-image.jpg" alt="cactus">
    <img src="images/last-image.jpg" alt="more cactus">
    <img src="images/pasetta-studios" alt="pasetta studios">

    <code>Designed by Pasetta Studios. Built by Abraham.</code>


  </div>
</body>

</html>

Answer №1

After removing the padding on the links, the issue was fixed as mentioned in a comment. The element stopped getting smaller once it became smaller than 100px (2x50px padding) and wrapped to the next line. This wrapping behavior is known as wrapping in CSS. Since you are centering the text, the padding is unnecessary.

To address this, I included an overflow: hidden property in .navbar to allow it to wrap around the floated links.

An outline has also been added to all elements inside the body to enhance visibility for demonstration and development purposes. You can achieve this by using the F12 developer tools in your browser.

body * {
outline: 1px solid red;
}

body {
  background-color: #262c2c;
}

.navbar {
  max-width: 75%;
  height: 100px;
  margin: auto;
  overflow: hidden;
}

.navbar a {
  float: left;
  padding: 40px 0;
  background-color: #262c2c;
  text-decoration: none;
  font-size: 25px;
  width: 25%;
  /* Four links of equal widths */
  text-align: center;
  color: #dae1e7;
}
#wrapper {
  max-width: 1000px;
  margin: auto;
  padding: 0 50px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="navigation.css">
  <title>Pasetta Studios</title>
</head>

<body>

  <div id="wrapper"><
    <div class="navbar"><
      <a href="#index.html"><Home</a><
      <a href="#about.html"><About</a><
      <a href="#projects.html"><Projects</a><
      <a href="#contact.html"><Contact</a><
    </div><
  </div><
</body>

If you're new to this, it's a good starting point but slightly outdated. Nowadays, most people use flexbox, which looks like this:

body * {
outline: 1px solid red;
}

body {
  background-color:#262c2c;
}

.navbar {
margin: auto;
max-width: 75%;

/* Added: */
display: flex;
}

.navbar a {
background-color:#262c2c;
text-decoration:none;
font-size:25px;
/* Four links of equal widths */
text-align:center;
color:#dae1e7;

/* Added: */
line-height:100px; /* Center the text vertically. */
flex:1; /* Allow the links to expand horizontally. */
}

#wrapper {
max-width:1000px;
margin:auto;
padding:0 50px;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="navigation.css">
<title>Pasetta Studios</title>
</head>

<body>

<div id="wrapper"><
<div class="navbar"><
<a href="#index.html"><Home</a><
<a href="#about.html"><About</a><
<a href="#projects.html"><Projects</a><
<a href="#contact.html"><Contact</a><
</div><
</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

What is the best way to iterate over each character in a string and trigger a function in JavaScript?

I am currently working on a project to create a random password generator. The code responsible for generating the password is functioning correctly. However, I am facing an issue with converting the characters of the password into phonetic equivalents. I ...

Using Node.js and Less to dynamically select a stylesheet source depending on the subdomain

Currently, my tech stack consists of nodejs, express, jade, and less. I have set up routing to different subdomains (for example: college1.domain.com, college2.domain.com). Each college has its own unique stylesheet. I am looking for a way to selectively ...

Tips for utilizing flexbox to position a button to the right of an InputSelect

Struggling to position a button next to a dropdown? No matter what I attempt, the button keeps ending up above the dropdown. Ideally, I'd like it to appear alongside 'Select Organisation' as shown in the image below: https://i.sstatic.net/w ...

I am experiencing issues with my HTML select list not functioning properly when utilizing a POST service

When using Angularjs to automatically populate a list with *ngFor and then implementing a POST service, the list stops functioning properly and only displays the default option. <select id="descripSel" (change)="selectDescrip()" > <option >S ...

Display excerpts of code within an Angular application within the HTML segment

I'm currently developing a tutorial page using Angular where I intend to display code snippets in various programming languages such as 'Java', 'TypeScript', 'C#', and 'Html'. Although there is a snippet called ...

Automatic closure of Info Window on Google Maps is not functioning as expected

I have recently developed a map which you can view here. The issue I am facing is that when I click on the layers to see the InfoWindow, they do not automatically close when I click on another layer. The JavaScript code I am using is causing this problem. ...

Implementing AJAX notifications in a contact form integrated with PHP, Google reCAPTCHA, and SweetAlert

I've implemented an AJAX script to handle the submission of a basic contact form with Google reCAPTCHA at the end of my HTML. Here's the relevant HTML code: <div class="col-md-6"> <form role="form" id="form" action="form.php" method ...

The comparison between createElement() and AJAX response

Currently, my website has a login form that updates the page with user data through AJAX after successful login. To maintain semantic integrity, I use JavaScript to remove the login form from the page once the user is logged in. However, when the user lo ...

Splitting your screen in a 10/90 column ratio using React

I am looking to split my screen into a 10/90 ratio of the total screen space. // Here is what I have attempted: return ( <> <div class="row"> <div className="col-md-4" > ; <div class="le ...

Is there a way for me to create a sidebar that is scrollable, like the one on www.youtube

Hey there! I'm looking to incorporate a feature on my website that resembles the layout of https://www.youtube.com/. More precisely, I want to have a sidebar on the left side displaying playlists, subscriptions, and other options. However, I don' ...

The method provided by the FullScreen API is not effective in cancelling the fullscreen mode

After testing my code in Google Chrome, I encountered the following error message: Uncaught TypeError: Object #<HTMLDivElement> has no method 'webkitCancelFullScreen' Interestingly, I also received a similar error while running the code i ...

What is the best approach to effectively manage right-to-left text-input fields?

I have been working on a project involving a multilingual layout. One concern that has arisen is: What is the proper way to handle text-input in this scenario? To better explain my issue, I created a JSFiddle. If I simply add the attribute dir="rtl", ...

Creating an unordered list (ul) with list items (li) that extend the full width

Having trouble making my list items (li) from the unordered list (ul) stretch across the page like the navigation bars on websites such as Verragio and James Allen. Can someone assist me with this? Here is a basic example of a nav hover bar. .dropdown-b ...

Combining href into click events - a step-by-step guide

I have an href link available. '<a href="index.php?imei=' + value['imei'] + '&nama=' + value['nama'] + '" class="summarykapal">Summary</a>' This is the function in question: function retr ...

Updating the src of an iframe and adding an onclick event to a link using JavaScript

Looking to design a page that accommodates both login and sign up functionalities by dynamically updating the src of an iframe. In the navigation bar, there is: <li id="change"><a onclick="sign()">Sign up</a></li> And within the ...

Is it possible to utilize a JavaScript framework within a Chrome extension?

Currently, I am developing a chrome extension that adds a toolbar to the DOM dynamically. In order to find, attach, and manipulate elements, I have been using CSS selectors in JavaScript. However, this approach has proven to be fragile as any changes made ...

The division of columns in the Bootstrap grid is malfunctioning

Recently, I embarked on my journey with Bootstrap and encountered an issue with the Grid Col System. I am aiming to center the logo and navigation bar on the page by dividing the Grid layout into 4 col-lg-4 (md-4). Subsequently, I inserted the image and na ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

Form fields in Bootstrap 4 will consistently expand downward in the select dropdown menu

When using bootstrap 4, I want my field to always expand downwards and never upwards, even when half the screen is taken up by the form's select element. How can I achieve this effect? I have tried using data-dropup-auto="false" but it didn't wo ...

What causes the size of an image to decrease when placed within an anchor tag?

Trying to add clickable social media icons, but when wrapped in an anchor tag the images shrink significantly. <span class="iconspan"> <a href="https://www.instagram.com/lil_ijoez/" target="_blank" ...