Flexbox causing spacing problems in HTML/CSS navigation bar

I have encountered a unique issue that I cannot seem to find a solution for, so here goes:

I am in the process of designing a navigation bar with the heading positioned on the left-hand side and the navigation items on the right. My goal is to center the nav bar on the page with a width of 960px. In addition to this, I would like to achieve two specific things:

1.) The navigation bar should be fixed at the top of the screen to remain visible while scrolling.

2.) When minimizing the screen, the spacing between the heading and the navigation items should decrease first. Currently, the items do not adjust responsively.

I can only accomplish one of these goals at a time, not both simultaneously. Any assistance or suggestions would be greatly appreciated. Thank you.

html {
  font-family: helvetica;
  font-size: 18px;
}

.container {
  max-width: 960px;
  margin: 0 auto;
}

.banner {
  background-color: aqua;
  opacity: .5;
  border-radius: 10px;
  position: fixed;
}

.header {
  display: flex;
  justify-content: space-between;
  height: 4rem;
  align-items: center;
  width: 960px;
}

.container .banner .header h1 {
  margin-left: 2.5rem;
}

.container .banner .header .nav ul {
  display: flex;
  width: 200px;
  justify-content: space-between;
  text-decoration: none;
  margin-right: 2.5rem;
}

.container .banner .header .nav ul a {
  color: black;
  list-style-type: none;
  text-decoration: none;
}
<div class="container">
  <div class="banner">
    <div class="header">
      <h1>My Sweet Sweet Georgia</h1>
      <div class="nav">
        <ul>
          <li><a href="#">doggy</a></li>
          <li><a href="#">puppy</a></li>
          <li><a href="#">toys</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>

Answer №1

A helpful tip is to utilize align-items: left for your heading and align-items: right in place of relying on margin

* {
  padding: 0;
  margin: 0;
}

html {
  font-family: helvetica;
  font-size: 18px;
}

.container .header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  flex-direction: row;
  height: 4rem;
  align-items: center;
  width: 100%;
  position: fixed;
  background: aqua;
  opacity: .7;
  border-radius: 10px;
}

.container .header .nav ul li {
  display: inline-flex;
}
<!DOCTYPE html>
<html>

<head>
  <title>My Puppy</title>
  <link href="./resources/reset.css" type="text/css" rel="stylesheet">
  <link href="./resources/style.css" type="text/css" rel="stylesheet">
</head>

<body>
  <div class="container">
    <div class="header">
      <h1>My Sweet Sweet Georgia</h1>
      <div class="nav">
        <ul>
          <li><a href="#">doggy</a></li>
          <li><a href="#">puppy</a></li>
          <li><a href="#">toys</a></li>
        </ul>
      </div>
    </div>
  </div>
</body>

Answer №2

To update your css stylesheet:

First, remove the following code from the .header selector:

.header {
    width: 960px;
}

Next, add the below code to the .banner selector:

.banner {
    max-width: 960px;
    width: 100%;
}

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 could be causing the sudden appearance of a white space in my modal window?

enter image description here I have a CSS issue where I set the width, unlike the source from which I created this code. Here is the example code: <div class="info-box visible-md visible-lg"> <a class="waves-effect waves- ...

Turn off Chrome's new tab preview thumbnails

Is there a method to prevent Google Chrome from displaying certain pages as thumbnails? I am inquiring because I am developing a website that contains confidential information. As it stands now, the sensitive data can be viewed in the thumbnail preview. ...

Focus the text on a specific letter

I am attempting to align a phrase in the center of my navigation bar, which has been built using Bootstrap. Currently, the navbar appears as follows: Centered Navbar title My goal is to center the "|" symbol with the text adjacent to it on both sides, ...

Retrieve the value of a dynamically generated input element within a form object

I'm trying to figure out how to reference a dynamic 'name' of an input element in a form. Can anyone help? Here's an example using HTML: <form> <input type="text" name="qty1" value="input1" /> <input type="text ...

Interacting with an iframe element using Selenium in Python

I have a webpage with an iframe embedded, and I'm using Selenium for test automation: <iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0" style="dis ...

Tips for transferring the id from the url to a php function seamlessly without causing a page refresh

I have a div that includes a button (Book it). When the button is clicked, I want to append the id of the item I clicked on to the current URL. Then, use that id to display a popup box with the details of the clicked item without refreshing the page, as it ...

Fonts fail to load on Internet Explorer 10

Link to the website in question: In my fonts.css file, I am loading multiple fonts as shown below: @font-face { font-family: GothamBook; src: url('../fonts/Gotham-Book.otf'); src: url( ../fonts/Gotham-Book.otf ); /* IE */ } @font-face ...

What methods can be used to prevent the appearance of the ActiveX warning in IE7 and IE8 when dealing with drop

I have a dilemma with my website where I am using JavaScript to modify CSS on hover for specific items. Interestingly, in Firefox everything runs smoothly without any ActiveX message popping up. However, in IE8, I encounter a warning stating "To help prote ...

Adjusting the layout of columns in Bootstrap 3

I have been attempting to realign columns in bootstrap, but without success so far. In desktop view it looks fine as seen here. However, when the size is condensed, it appears like this tablet view. My goal is to swap the positions of the owner gateway col ...

Sending documents to a printer directly from a Rails application

Is there a library or gem in Rails that can be used to print the contents of a web page directly onto paper? I am curious if it's possible to specify only a specific part of the page, such as a div, for printing. Any guidance, advice, or tutorial link ...

Experiencing difficulty retrieving text using Selenium WebDriver in Python

I am using Selenium WebDriver to extract text from a specific box on this website. Check out my code below: from os import system from selenium import webdriver driver = webdriver.Chrome("./chromedriver") driver.get("http://www.lutanho.ne ...

A method for changing the background of a textbox based on a specific condition

I have an input text field below. <label>User Type</label> <input name="user_type" id="user_type" class="form-control" readonly/> I am trying to change the background color of this textbox based on the text ...

How can I apply styling to Angular 2 component selector tags?

As I explore various Angular 2 frameworks, particularly Angular Material 2 and Ionic 2, I've noticed a difference in their component stylings. Some components have CSS directly applied to the tags, while others use classes for styling. For instance, w ...

How can I modify the background color of the hamburger menu?

I attempted to modify the background color of the hamburger icon from black to yellow, but unfortunately, I couldn't achieve it. Here's what I tried: //hamburger $(".hamburger").toggleClass("is-active"); var $hamburger = $(".hamburger"); ...

Conceal the top section of the navigation bar as you smoothly scroll

Hello, I recently personalized a Bootstrap navbar with 2 rows - the upper section containing just the logo and social links, and the lower section with navigation links. I have been attempting to hide the upper section when scrolling, but so far, I have no ...

I am having issues with my CSS file not functioning correctly on my Django template. Does anyone have any suggestions on how to resolve this issue

I've encountered issues with CSS files not working properly on my Django template. Whenever I make changes to the CSS file and reload the browser page, nothing happens. However, if I restart my computer or power off and on again, the changes reflect o ...

How can the position of Bootgrid's pagination element be fixed in relation to its parent container, no matter how many table rows are present?

I've been struggling to keep the pagination section of a JQuery Bootgrid table I'm building in the right position. No matter what I do, it always seems to be aligned with the last row of the table. I attempted setting the container div as relativ ...

Creating custom styles in SCSS using the pseudo-element :after and targeting a specific class with the ampersand symbol

I am trying to add a transition effect to the element with an applied active class. My goal is to avoid duplicating my SCSS code (even though I understand it will be duplicated in the compiled CSS). The sole purpose of the active class is to modify the b ...

A guide to crafting a cross design using only CSS

Is it possible to create a cross design in pure CSS that overlaps an image inside a div? Or would I need to create a new image with the cross included? ...

jQuery's AJAX feature fails to identify the newly modified div classes

For my new website, I am developing a simple checklist feature. To handle the check and uncheck requests from users, I'm using jQuery's $.ajax() function. Whenever a user clicks on the check or uncheck buttons, jQuery retrieves the validation tok ...