Creating Flexnav Menu Buttons that automatically adjust their width based on the content within them

Is it possible to use flexnav without fixed width buttons?

I would like all navigation items to only use the necessary width to display their text and padding. Currently, I have to divide 100% by the number of navigation items we have. This value is set with .flexnav li{}

<div id="wrapper">
  <h1>Flexnav - Content Width Test</h1>
  <div class="menu-button">Show Menu</div>
  <div class="nav-container container">
    <div class="nav">
      <ul class="flexnav" data-breakpoint="800">
        <li><a href="">Startpage</a></li>
        <li><a href="#">About Us</a>
          <li><a href="#">Products</a>
            <ul>
              <li><a href="#">Product Category 1</a></li>
              <li><a href="#">Products</a></li>
              <li><a href="#">Large Product Category</a></li>
              <li><a href="#">Small one</a></li>
              <li><a href="#">PPPPProducts</a></li>
            </ul>
          </li>
          <li><a href="#">Media</a></li>
          <li><a href="#">News</a></li>
          <li><a href="#">Contact</a></li>
          </ul>
        </div>
    </div>
    <p>The red space is needed for a search and some Icons</p>
  </div>

I have created a codepen to showcase the working nav. Any tips on solving this problem would be greatly appreciated: http://codepen.io/anon/pen/aCjGq

Thank you!

Answer №1

'Currently, I need to distribute 100% among the number of navigation items we have.'- There is no necessity to do that. Simply assign a width to the container div of the menu and utilize the flexnav plugin as shown below :

$(".flexnav").flexNav({

             'calcItemWidths': true , // dynamically calcs top level nav item widths
       });

Answer №2

Although the question may be dated, this solution could still benefit someone in need. Consider integrating the following snippet into your CSS:

  .flexnav > li {
    width: auto;
  }
  .flexnav > li.has-subitems {
    padding-right: 30px;/*adjust according to dropdown menu icon*/
  }
  .flexnav li ul{
    padding-left: 0 !important;
    min-width: 210px;/*customize as needed*/
  }

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

Adjusting the height of a div by referencing the height of another div

My website features a two-column layout <div id="site-wrapper"> <div id="leftSidebar"> </div> <div id="rightSide"> </div> </div> Here is my JavaScript code: $('#leftSidebar').height($('#rightSide ...

Is there a method to obtain the compiled CSS for an Angular 2+ component?

Is it possible to retrieve compiled CSS (as a string) from an Angular2+ component? @Component({ selector: 'test-graph', templateUrl: './test-graph.component.html', styleUrls: ['./test-graph.component.scss'] }) export cla ...

Using apply quaternion is not a valid function

Can anyone shed some light on this issue I'm having while attempting to set vertices of a geometry using a quaternion? The code snippet I've been using is: geometry.attributes.position.array[x].applyQuaternion(quaternion) However, it doesn' ...

decoding json field containing forward slashes using javascript

Seems easy enough, but I'm having trouble figuring it out. let data="[{name:\"House\",id:\"1\"},{name:\"House and Land\",id:\"5\"},{name:\"Land\",id:\"6\"},{name:\"Terrace\",id:&bs ...

Guide to parsing a nested Buffer with JSON.parse

I need help with serializing and deserializing an object containing multiple Buffers. I've encountered an issue where the Buffers are not correctly recreated when using JSON.stringify() followed by JSON.parse(). var b64 = 'Jw8mm8h+agVwgI/yN1egch ...

jQuery Ajax error 403 (unlike XMLHttpRequest)

Recently, I encountered an issue with two Ajax calls in my code. One of the calls was implemented using XMLHttpRequest and the other one using jQuery. Surprisingly, the first call completed successfully without any errors. However, the second call, which s ...

Updating the DOM after making changes with Maquette involves calling the `renderMaquette

In a previous discussion, I expressed my desire to utilize Maquette as a foundational hyperscript language. Consequently, avoiding the use of maquette.projector is essential for me. However, despite successfully appending SVG objects created with Maquette ...

Troubleshooting: Unable to get jQuery serialScroll / scrollTo to function properly

I'm going crazy trying to figure out how to make the JQuery ScrollTo plugin work correctly. I want to be able to scroll it to the next and previous items programmatically. Although I've looked at some related queries (and their corresponding fid ...

Retrieve the index of the item that has been selected in a dropdown list

<select ng-click="getIndex($index)" size="14" ng-model="playlist.fileSelected" ng-options="saveFile for saveFile in playlist.playlist"></select> When I try to access $index, it shows up as undefined. Is there a way to retrieve the index of the ...

Generate a configuration file that allows for the reading and storage of modifications

Is there a way to create a configuration file (JSON) on the local file system using JavaScript where I can write and modify data without losing it when the application is restarted? Any suggestions or solutions for this problem? Thank you for your assista ...

Guide on implementing asyncWithLDProvider from Launch Darkly in your Next.js application

Launch Darkly provides an example (https://github.com/launchdarkly/react-client-sdk/blob/main/examples/async-provider/src/client/index.js) showcasing how to use asyncWithLDProvider in a React project (as shown below). However, I'm struggling to integr ...

Why does the jQuery "alert" function work, but the image "src" setting is being overlooked?

Building on the previous inquiry: Can't I use JQuery inside my FancyZoom popup? The solutions provided in response to that query resolved the issue of consistently triggering our Hello World alert within our FancyZoom popup. However, our ultimate g ...

Incorporating React into an already existing webpage and accessing URL parameters within a .js file

Following the official guidelines: To include React in a website, visit https://reactjs.org/docs/add-react-to-a-website.html I have created a file named test.html with the following content: <!DOCTYPE html> <html> <head> < ...

The CSS property for restricting white-space to nowrap is not functioning as

Having a div with multiple child divs floating left can be tricky. To prevent them from breaking, setting them to display:inline-block and white-space:nowrap seems like the solution. However, in some cases this may not work as expected. If your goal is to ...

Randomly choose one of 5 pages and insert an HTML element at different intervals using JavaScript, jQuery, MySQL, or PHP

Suppose we have the following HTML element: <img src="icon.png"> Our website consists of 5 pages: index.php products.php register.php about.php terms.php How can we randomly place this HTML element on one of the pages, ensuring it stays there for ...

What could be causing the "AJAX data not defined" error

Attempting to make an Ajax post request to the root directory on my Express server. By simply using the HTML form and submitting an artist name, I successfully receive a response back and can send the information to the client without any issues... As se ...

The second Ajax request recreates the HTML content, resulting in duplication

I am trying to implement an AJAX request that will prepend a specific html block when link 1 is clicked in my DOM: $("#link1").on("click", function(){ $.get("myBlock.html", function(data){ $(".wrapper").prepend(data); }); }); This html block, which co ...

Is it possible to leverage both functions and variables within the ng-options expression in Angularjs?

I am faced with a situation where I have 2 select boxes. The first one is used to choose the user type (such as groups or individual), and the second one displays the options based on the selection made in the first box. I was wondering if it is possible t ...

How to efficiently pipe and stream JavaScript objects in a Node.js environment

Trying to grasp the concept of Node.js streams can be a bit daunting, especially for someone who is relatively new to JavaScript and node. I've recently delved into languages like Perl and PHP, so this is definitely a new challenge for me! To better ...

Trigger a modal popup upon page load event generated from the server side using C#

I am trying to display a modal popup based on certain conditions during the page load event, but I am facing some issues. This is my modalPopup code: <script type="text/javascript"> function openModal(message, header, url) { $('#my ...