The navigation elements in the Flexbox are behaving strangely as they overflow in an unexpected way

During my responsiveness testing, I came across a very strange issue. I have code that aligns the header as a flexbox, but when I resize the window to over 900px in width, the navbar moves out of the header and appears on top of the next element.

Here's an example:

https://i.sstatic.net/2Xl20.png

This strange behavior occurs until the width reaches 1205px, after which it displays as expected.

Like this:

https://i.sstatic.net/erutj.png

The issue depicted in the first image shouldn't be happening because I added a media query specifically for desktop screens.

Below is the key code snippet for that section:

HTML:

<header>
    <a href="#"><img class="logo" src="images/img-tea-cozy-logo.webp" alt="The Tea Cozy Logo"></a>
    <nav>
        <ul class="bar">
            <li><a href="#mission">Mission</a></li>
            <li><a href="#featured">Featured Tea</a></li>
            <li><a href="#locations">Locations</a></li>
        </ul>
    </nav>
</header>

CSS:

/* Header */
header {
    max-width: 100%;
    height: 4.54em;
    border-bottom: 1px solid seashell;
    display: flex;
    align-items: center;
    flex-direction: column;
    background-color: black;
}

header a img {
    height: 2.18em;
    margin-top: 0.45em;
}

nav ul li {
    display: inline-flex;
    text-decoration: underline;
    padding: 0.13em 0.25rem 0 0.25rem;
}

nav ul li a {
    color: seashell;
}

And here is the media query for Desktop:

/* Header for Desktop Screens */
@media only screen and (min-width: 900px) {
    header {
        height: 3.13em;
        justify-content: space-between;
    }
    
    header a img {
        height: 2.27em;
        margin: 0.30em 0 0 0.45em;
    }
    
    nav ul li {
        padding: 0;
        margin: 0.45em 0.45em;
        font-size: 1em;
    }
}

The strangest thing happened when I mistakenly inserted "and" after the condition, like this:

@media only screen and (min-width: 900px) and {
...
}

To my surprise, the issue was resolved even though the syntax was incorrect:

https://i.sstatic.net/7LWjy.png

I'm seeking help to understand what's going on here and how to fix the problem shown in the first image.

Answer №1

Don't forget to include flex-direction: row in the media query:

/* Header */

header {
  max-width: 100%;
  height: 4.54em;
  border-bottom: 1px solid seashell;
  display: flex;
  align-items: center;
  flex-direction: column;
  background-color: black;
}

header a img {
  height: 2.18em;
  margin-top: 0.45em;
}

nav ul li {
  display: inline-flex;
  text-decoration: underline;
  padding: 0.13em 0.25rem 0 0.25rem;
}

nav ul li a {
  color: seashell;
}


/* Header for Desktop Screens */

@media only screen and (min-width: 900px) {
  header {
    height: 3.13em;
    justify-content: space-between;
    flex-direction: row;
  }
  header a img {
    height: 2.27em;
    margin: 0.30em 0 0 0.45em;
  }
  nav ul li {
    padding: 0;
    margin: 0.45em 0.45em;
    font-size: 1em;
  }
}
<header>
  <a href="#"><img class="logo" src="images/img-tea-cozy-logo.webp" alt="The Tea Cozy Logo"></a>
  <nav>
    <ul class="bar">
      <li><a href="#mission">Mission</a></li>
      <li><a href="#featured">Featured Tea</a></li>
      <li><a href="#locations">Locations</a></li>
    </ul>
  </nav>
</header>

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

The looping functionality in Swiperjs is not working properly for sliders

Encountering some issues with the loop setting for swiper. The problem is that it stops sliding before entering a second loop, always halting at a certain point. I have never worked with swiper before, so I am unsure if this is the intended behavior or not ...

What is causing the image to become distorted on the canvas?

Despite specifying the image dimensions to be 50px by 50px, it appears stretched on the y-axis like this. View Stretched Image I suspect that the issue lies with the CSS styling applied. Here is my current CSS code: canvas { background-color: green; ...

React's Material-UI AppBar is failing to register click events

I'm experimenting with React, incorporating the AppBar and Drawer components from v0 Material-UI as functional components. I've defined the handleDrawerClick function within the class and passed it as a prop to be used as a click event in the fun ...

Requiring addresses for Google Maps in order to display directions

To display the directions between two addresses based on the chosen transportation mode, I need to pass the addresses to the code in page 2. After the user selects two cities from a Dropdown box on page 1, they will be sent to the code to show their locati ...

Unable to modify text color after implementing mat-sort-header in Angular Datatable

My current setup involves using an Angular data table to display data. I recently added the mat-sort-header functionality, which allowed me to change the font color of the header text. Below is a breakdown of my code: <section id="main-content" ...

Having difficulty altering the font in the WordPress tinymce editor

I've been attempting to update the font in the WordPress tinymce editor, but I'm running into issues. First, I created a tinymce-custom-editor.css file with the following code: @import url(https://fonts.googleapis.com/css?family=Open+Sans:400,7 ...

Displaying a particular form depending on user selection in PHP

Being new to the world of HTML and PHP, I have a question. In my HTML code, there is a form with two options to select from: lesson or topic. I want to display a different form depending on whether the user selects "lesson" or "topic." How can I achieve ...

Tips on targeting specific HTML content

Looking for assistance. I'm trying to extract HTML content from test.php <p> This is the content of a paragraph or article on a web page</p> <p>[albumname=Mount Climbing]</p> I want to process those [albumname] tags into thum ...

The input field within the mat-form has been styled to match the background of the page using CSS

I am currently facing an issue with the code below: Html: <form fxLayout="column" fxLayoutAlign="center center" [formGroup]="deleteForm" (ngSubmit)="onSubmitForm()"> <mat-form-field color="accent" appearance="outline"> <input ...

Implementing a gradient effect on a specific image element within an HTML canvas with the help of jQuery

Currently, I'm working on an HTML canvas project where users can drop images onto the canvas. I am looking to implement a linear gradient effect specifically on the selected portion of the image. My goal is to allow users to use their mouse to select ...

Issue with modal not being able to close the embedded video iframe

I have created a demo of a video in my footer that uses external CSS files and works perfectly. However, when I added it to the project, everything works except for closing the video. After clicking the play button in the footer, a modal appears on the sc ...

Set a CSS rule to style every other row in a table, starting from the second row and resetting after

Is there a way to refresh the even and odd count in CSS whenever a specific row is shown? Here's an example setup: header header header even even even odd odd odd Subhead Subhead Subhead even even even How can I ensu ...

"Enhance the styling with jQuery's css() function when clicked

I am currently working on the following code: $(document).ready(function(){ $("#FlatCheckbox").click(function(){ $("#style1").css("margin-top",10); }); }); <h2> <input type='checkbox' id="FlatCheckbox" class=" ...

Resolving AJAX requests within a loop

I am working with a JSON file that contains widgets {"widgetname": "widget1", "widgetID": "FJRH585fKFJN234NC"} As I iterate through the JSON, I generate HTML for each widget object. $.ajax({ url: "get_json.php", data: {action: "get_widgets", ...

Retrieve text from an element using jQuery when triggering "mouseenter" on a different element

Is there a way to retrieve the text from an element while simultaneously triggering a 'hover' action on another element? Here is an illustration: I am trying to gather all available colors, but the color names are only visible upon hovering ove ...

Modify a singular aspect of shorthand background attribute

Imagine using the shorthand background property to define multiple properties. For example: .img { background: url('/Content/images/a.png') no-repeat scroll 100% 3px; } If you need to override just the background-position of the image, sh ...

Is there a way to extract the "validade" value from the array and retrieve it exclusively?

The following array contains data: {"status":true,"data":[{"id":1,"pessoa_id":75505,"created_at":"2022-02- 01T17:42:46.000000Z","holder":"LEONARDO LIMA","validade&quo ...

Inspect Element - base tag href URL for inline CSS allocation

When utilizing the <base> tag in HTML along with the <style> tag on Microsoft's Edge Browser, I encounter a peculiar issue. Here is a very basic example: <!DOCTYPE html> <html lang="en"> <head> <title>Edge de ...

Incorporate a new JavaScript animation as the background for an established website, replacing the static background image

Seeking assistance with my portfolio as I delve into the world of Webdesign and learn the basics from templates. How can I integrate this javascript code into the background of my site, replacing the current minecraft image? Every attempt to implement it ...

Tips for creating a horizontal bar with CSS using the float property

Looking to create a horizontal scroll layout using CSS and floats? Here's my attempt, but I'm not getting the desired result. Flexbox isn't an option as it needs to be supported on IE. Take a look at my code and point out where I might have ...