Problem with mobile-first flexbox navigation

I attempted to replicate the fantastic flexbox tutorials by Wes Bos. I aimed to adapt a particular tutorial he presented on creating a responsive flexbox menu. However, my approach was to design the menu with a mobile-first perspective, utilizing media queries based on min-width.

Unfortunately, I encountered issues getting it to function correctly on the initial mobile layout. In Wes' menu, the list items stack on top of each other while the social icons at the bottom have flex: 1 1 25%. Yet in my version, even the social icons are stacked.

As for other breakpoints, my layout aligns with what Wes established.

I have shared my code through a codepen.

.flex-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.flex-nav .social {
  flex: 1 1 25%;
}

@media all and (min-width:500px) {

  .flex-nav li {
    flex: 1 1 50%;
  }

  .flex-nav ul {
    flex-wrap: wrap;
    flex-direction: row;
  }

  .flex-nav ul {
    border: 1px solid black;
  }
}

@media all and (min-width:800px) {

  .flex-nav li {
    flex: 3;
  }

  .flex-nav .social {
    flex: 1;
  }
}

Answer №1

Here is the default code without any media queries being applied:

.flex-nav ul {
  display: flex;
  flex-direction: column;
}

.flex-nav .social {
  flex: 1 1 25%;
}

You have set a flex-basis of 25% for each social media icon.

However, since your container has a flex-direction of column, the flex rule for the social media icons affects them vertically rather than horizontally.

Consider using this alternative approach instead:

.flex-nav ul {
   display: flex;
   flex-wrap: wrap;
}

li {
  flex: 0 0 100%;     /* adjusted to fit one item per row */
}

.flex-nav .social {
   flex: 0 0 25%;     /* resized to fit four items per row */
}

See revised demo here

Answer №2

I have organized the social links within a container to improve the menu structure and make it easier for me to navigate. VIEW ON CODEPEN Here is the HTML:

<div class="wrapper">
<nav class="flex-nav">
  <ul>
    <li><a href="#">item01</a></li>
    <li><a href="#">item02</a></li>
    <li><a href="#">item03</a></li>
    <li><a href="#">item04</a></li>
    <li><a href="#">item05</a></li>
    <li><a href="#">item06</a></li>
    <div class="social-container"> 
      <li class="social"><a href="#"><i class="fa fa-gift"></i></a></li>
      <li class="social"><a href="#"><i class=" fa fa-glass"></i></a></li>
      <li class="social"><a href="#"><i class=" fa fa-calendar"></i></a></li>
      <li class="social"><a href="#"><i class=" fa fa-cutlery"></i></a></li>
    </div>
  </ul>
  </nav>
</div>

CSS:

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  font-family: sans-serif;
  margin: 0;
}

a {
  color: white;
  font-weight: 100;
  letter-spacing: 2px;
  text-decoration: none;
  background: rgba(0, 0, 0, 0.2);
  padding: 20px 5px;
  display: inline-block;
  width: 100%;
  text-align: center;
  transition: all 0.5s;
}

a:hover {
  background: rgba(0, 0, 0, 0.3);
}

.wrapper {
  max-width: 1000px;
  margin: 0 auto;
  padding: 50px;
}

.flex-nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100%; /* ADDED */
}

.flex-nav .social {
    flex: 1 1 25%;
}

.social-container { //just make it flex container 
  display: flex;
  width: 100%;
}

@media all and (min-width:500px) {

    .flex-nav li {
      flex: 1 1 50%;
    }

    .flex-nav ul {
      flex-wrap: wrap;
      flex-direction: row;
    }

    .flex-nav ul {
      border: 1px solid black;
    }
}

@media all and (min-width:800px) {

    .flex-nav li {
      flex: 1;
    }

    .flex-nav .social {
      /*flex: 1;*/
    }

    .social-container {
      flex: 2; /* set the value as many as you want */
    }
  
    .flex-nav ul { //change the direction
      flex-direction: row;
      flex-wrap: no-wrap;
    }
}

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

When a custom icon is clicked in the vue-select dropdown, the custom method is not activated

In my current project, I am creating a vue-component based on vue-select. Within this component, I have added a custom info Icon. The issue I am facing is that when the user clicks on the Icon, instead of triggering my custom method getInfo, it opens the s ...

Turn off the following navigation link in the Bootstrap wizard

I am working on a bootstrap wizard and I need to find a way to disable the next navigation link under certain conditions. Does anyone have suggestions on how I can achieve this using jQuery, CSS, or any other method? <div id="rootwizard"> ...

Is it feasible to animate a JQuery slider?

Is there a way to animate the slider using jQuery? I want to be able to press a button and have the inner part of the slider move slower (e.g. 300ms) without reacting to mouse clicks and slides. Here is the code: http://jsfiddle.net/gm4tG/5/ HTML: <d ...

What is the best way to generate a fresh JSON object within a react hook function?

I am currently facing two issues. Firstly, I am having trouble figuring out how to add/update the JSON items within a hook. Secondly, React seems to be restricting me from using the name that is stored in a previous JSON file. I am open to exploring alter ...

Responsive Login Box with Bootstrap 3

Currently, I am in the process of figuring out how to create a login module with the following features: A responsive Login Dialog Box with a Logo A background that darkens I have searched for bootstrap templates that include these elements, but unfortu ...

Is it possible to trigger a click event exclusively on a parent element?

One feature I'm trying to implement in my app is the ability to click on an item, such as changing the background color or text, and have a modal pop up with a color picker to customize the color of that specific item. However, I've run into an i ...

Retrieve all choices from dropdown menu using a POST request

I'm working on a page that includes two select boxes. I need to transfer items from Select #1 to Select #2. After clicking the submit button, I want to retrieve all options from Select #2, regardless of whether they are selected or not. <?php req ...

Creating an artistic blend by layering p5.js drawings over images in an HTML canvas

I'm having a tough time solving this issue. My goal is to overlay circles on top of an image, ideally using HTML for the image. However, I just can't seem to make it work. let img; function setup() { createCanvas(800,800); img = loadImage(&qu ...

Is it possible to modify a single entry in a series of records?

I have a list of data entries, such as addresses, and I am currently displaying them using the following combination of html5 and knockout code. <section id="lists" data-bind="foreach: addresses, visible: addresses().length > 0"> <article& ...

What is the best way to target the final child element of a specific CSS class?

I am working with an HTML table and my goal is to remove the border-bottom from the last row, excluding the row with class .table-bottom: <table cellpadding="0" cellspacing="0" style="width:752px;" class="table"> <tr class="table-top">< ...

show a division once it's clicked

I'm currently facing an issue with a script that I can't seem to solve on my own. Essentially, I am attempting to make a div appear after clicking on the "Products" button. Here is what I have so far: <div id="header"> <div id=" ...

Content remains connected to the left side of the webpage in a single column, adapting to different screen sizes

Can someone help me with this issue: RWD I have been struggling to center these elements in a single column when the screen size is less than 960px. I have tried using margin: 0 auto; but it doesn't seem to be working. Any suggestions? ...

Prevent the content within a Bootstrap "row" from moving to a new line

My website appears differently in desktop and mobile view. https://i.sstatic.net/MGJSk.png While the tags align perfectly on desktop, they overflow onto a new line in the mobile view. Specifically, #pytorch and #tensorflow are pushed to a new line which ...

Jquery does not display the variable in the Firefox console using console.log

I am trying to retrieve the data value from the text input, but it's not getting logged in the console. I even tried using a breakpoint, but it still doesn't console anything. What could be the issue? var x = '' $(() => { $("#en ...

AngularJS encountered an unhandled syntax error

My current approach involves utilizing the code below to display data fetched from Parse API into a table using AngularJS and Bootstrap. However, the JavaScript section where I have defined the controller doesn't seem to be running as expected. Below ...

Encountering redundant links during the scraping process

I'm currently working on extracting "a" tags with the class name "featured" from a specific website . Although the code I've written seems to be error-free, it unfortunately duplicates the links. How can I resolve this issue and avoid the duplica ...

Can we expect every bullet point to be consistently indented at precisely 1.8em?

Recently, I created a dropdown menu using only CSS. The challenge was to have a horizontal bar of items that can each expand into a vertical menu. However, I wanted the expanded menus to display bulleted lists instead of tertiary menus. By nesting three ul ...

Inject customized CSS following third-party plugin stylesheets in WordPress

I am currently exploring methods to incorporate a custom.css file after the loading of a third-party plugin. I am familiar with registering and enqueueing CSS files in functions.php. Is it possible to establish a dependency for the stylesheets of the thi ...

Unable to properly configure Bootstrap carousel to fit the screen - struggling to make it work

Apologies, this is now the 1754th question on this particular topic. Despite my extensive research, I still can't seem to get it to work as intended. After reviewing various answers, I created the following test case. The challenge lies in maintainin ...

Is it possible to use Chrome developer tools to find out the current file's name?

I am having difficulty locating a specific code within my online shop on Prestashop. The meta-data of the current page contains the content="Shop on PrestaShop" string, but when I search for it in Webstorm to identify which page file it is located in, I ca ...