Bootstrap 3 with a dual sidebar layout positioned on the left side

I am currently working on a project where I need to create a fluid layout with a left sidebar. However, now I would like to add an expandable sidebar next to the existing one. Below is an image to illustrate my goal:

Initially, the left sidebar contains a logo and some navigation elements. When a user clicks on the second navigation element, I want to display a subnavigation. But, I'm not sure how to achieve this. Has anyone encountered a similar example before? I could really use some guidance on how to get started.

Here's an example for a single sidebar:

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-md-2 sidebar">
      <ul class="nav nav-sidebar">
        <li class="active"><a href="#">Navigation 1</a></li>
        <li><a href="#">Navigation 2</a></li>
      </ul>
    </div>
  </div>
</div>

Answer №1

A clever jQuery script can be utilized to determine which sub navigation should be displayed based on the link's href attribute, which includes the ID. Take a look at this DEMO:

$(function () {
    $(".main").on("click", "a", function () {
        $(".sub").removeClass("active");
        $($(this).attr("href")).addClass("active");
        console.log($(".sub ul"));
    });
});

If you structure your HTML like this:

<div class="container-fluid">
    <div class="row">
        <div class="col-xs-6 col-sm-3 col-md-2 sidebar">
            <div class="logo">
                <img src="http://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg" />
            </div>
            <ul class="nav nav-sidebar main">
                <li class="active"><a href="#sub1">Navigation 1</a></li>
                <li><a href="#sub2">Navigation 2</a></li>
                <li><a href="#sub3">Navigation 3</a></li>
            </ul>
        </div>
        <div class="col-xs-6 col-sm-3 col-md-2 sidebar">
            <ul id="sub1" class="nav nav-sidebar sub active">
                <li><a href="#">Subnavigation 1.1</a></li>
                <li><a href="#">Subnavigation 1.2</a></li>
                <li><a href="#">Subnavigation 1.3</a></li>
                <li><a href="#">Subnavigation 1.4</a></li>
            </ul>
            <ul id="sub2" class="nav nav-sidebar sub">
                <li><a href="#">Subnavigation 2.1</a></li>
                <li><a href="#">Subnavigation 2.2</a></li>
                <li><a href="#">Subnavigation 2.3</a></li>
            </ul>
            <ul id="sub3" class="nav nav-sidebar sub">
                <li><a href="#">Subnavigation 3.1</a></li>
                <li><a href="#">Subnavigation 3.2</a></li>
                <li><a href="#">Subnavigation 3.3</a></li>
                <li><a href="#">Subnavigation 3.4</a></li>
                <li><a href="#">Subnavigation 3.5</a></li>
            </ul>
        </div>
    </div>
</div>

Add some CSS for styling:

.logo {
    text-align: center;
}
.logo img {
    width: 50%;
    margin: 20px auto;
}
.sub {
    max-height: 0;
    height: auto;
    overflow: hidden;
    transition: max-height 0s;
    background: #fafafa;
}
ul.active {
    max-height: 400px;
    transition: max-height 1s 0.1s;
}

Answer №2

If you're looking to create vertical tabs, JQuery UI is the way to go! Check out their documentation for more information: http://jqueryui.com/tabs/#vertical. Feel free to customize the styling to suit your needs.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8>
  <title>jQuery UI Tabs - Vertical Tabs functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
    $( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );
  });
  </script>
  <style>
   //Feel free to override the default JQuery styling
  .ui-tabs-vertical { width: 55em; }
  .ui-tabs-vertical .ui-tabs-nav { padding: .2em .1em .2em .2em; float: left; width: 12em; }
  .ui-tabs-vertical .ui-tabs-nav li { clear: left; width: 100%; border-bottom-width: 1px !important; border-right-width: 0 !important; margin: 0 -1px .2em 0; }
  .ui-tabs-vertical .ui-tabs-nav li a { display:block; }
  .ui-tabs-vertical .ui-tabs-nav li.ui-tabs-active { padding-bottom: 0; padding-right: .1em; border-right-width: 1px; border-right-width: 1px; }
  .ui-tabs-vertical .ui-tabs-panel { padding: 1em; float: right; width: 40em;}
  </style>
</head>
<body>

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nav element 1</a></li>
    <li><a href="#tabs-2">Nav element 2</a></li>
    <li><a href="#tabs-3"> Nav element 3</a></li>
  </ul>
  <div id="tabs-1">
    <h2>Sub nav 1 element </h2>
  <div id="tabs-2">
    <h2>Sub nav 2 element</h2>       
  </div>
  <div id="tabs-3">
    <h2>Sub nav 3 element </h2>
   <h2>Another sub nav 3 element </h2>
  </div>
</div>


</body>
</html>

Answer №3

Since you're already utilizing Twitter Bootstrap, why not consider implementing Stacked Nav-tabs to display the submenus?

You could structure it like this: (make sure to style the content flow after the navigation and wrap them in columns for responsiveness)

 <div class="sidebar"> 
   <div class=logo"></div>
   <ul class="nav nav-tabs nav-stacked">
     <li class="active"><a href="#pane1" data-toggle="tab">Tab 1</a></li>
     <li><a href="#pane2" data-toggle="tab">Tab 2</a></li>
     <li><a href="#pane3" data-toggle="tab">Tab 3</a></li>
     <li><a href="#pane4" data-toggle="tab">Tab 4</a></li>
   </ul>
 </div>
 <div class="tab-content">
   <div id="pane1" class="tab-pane active">
     <ul class="nav nav-tabs nav-stacked">
      <li ><a>Sub Tab 1</a></li>
      <li ><a>Sub Tab 2</a></li>
      <li ><a>Sub Tab 3</a></li>
     </ul>
   </div>
   <div id="pane2" class="tab-pane">
     <ul class="nav nav-tabs nav-stacked">
      <li ><a>Sub Tab 1</a></li>
      <li ><a>Sub Tab 2</a></li>
      <li ><a>Sub Tab 3</a></li>
     </ul>
   <div id="pane3" class="tab-pane">
     <ul class="nav nav-tabs nav-stacked">
      <li ><a>Sub Tab 1</a></li>
      <li ><a>Sub Tab 2</a></li>
      <li ><a>Sub Tab 3</a></li>
     </ul>
   </div>
   <div id="pane4" class="tab-pane">
     <ul class="nav nav-tabs nav-stacked">
      <li ><a>Sub Tab 1</a></li>
      <li ><a>Sub Tab 2</a></li>
      <li ><a>Sub Tab 3</a></li>
     </ul>
   </div>
 </div>

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

There was an error while parsing JSON on line 1, where it was expecting either a '{' or '[' to start the input

I've been struggling to extract data from my PHP array that is encoded using json_encode. I'm new to JQuery and have been attempting this for a few days without success. This code snippet is not producing the desired results: $.post('coord ...

Update the CSS classes exclusively for the specified ID

Attempting to modify and override certain classes within the complex control (dxList) of DevExtreme has been successful thus far. .dx-loadpanel-content { transform: translate(0px, 0px) !important; margin: auto !important; left: 0px !important; ...

Is it possible to combine margin auto with a transform on the left simultaneously?

While examining a webpage, I noticed a positioning issue and decided to remove some CSS code to determine the root of the problem. It appears that the navbar has a 1px gap on the left side from the edge of the screen. After removing the last two lines of c ...

Switch up the animation based on the state in AngularJS

In my AngularJS application, I have implemented CSS animations for transitioning between states. The animations involve sliding content from left to right with specific timings and transforms. .content.ng-enter, .content.ng-leave { -webkit-animation-t ...

Ways to patiently wait in a for loop until the ajax call is completed

I am a beginner in web development and currently working on a small website project that involves using ajax to display new comments. Below is the function I have created: function show_comments() { $('div#P_all_posts>div').each(function () { ...

Can JavaScript trigger an alert based on a CSS value?

Hello, I am facing an issue. I have created a blue box using HTML/CSS and now I want to use JavaScript to display an alert with the name of the color when the box is clicked. Below is my code: var clr = document.getElementById("box").style.background ...

Navigating through ajax requests on a Leaflet map

Currently, I have a leaflet map set up with the leaflet-panel-layers plugin to create a visually appealing layer control. To create my layers and overlays, I have two functions in place. The issue arises when trying to access external geoJSON files, as lea ...

What is the best way to adjust the height of the second column downwards?

I am trying to achieve a 2-column layout with both columns at the same height. This is how it currently appears: And this is how I want it to be: Here is my code snippet: html { background-color: lightblue; font-family: Tahoma, Geneva, sans-serif ...

How to automatically switch to the next tab in Bootstrap using JQuery when the video finishes

I recently developed a tabbing system featuring 4 tabs using Bootstrap. While it currently functions manually, I am seeking a way to have it loop automatically upon video completion. Below is my current code: <div id="tab"> <ul class="nav nav ...

Looking for a jQuery plugin that helps with form alignment?

In a blog post comment, I came across an interesting jQuery form alignment plugin: jQuery.fn.autoWidth = function(options) { var settings = { limitWidth : false } if(options) { jQuery.extend(settings, options); }; ...

Is it possible to add a border to both the tbody and td

I currently have a table that is organized with tbody elements to group rows together. In order to create a grid-like structure, I applied borders to each individual td element within the tbody. However, I also desire to show that the tbodies themselves ar ...

Align the CSS dropdown content to the right rather than the left for a cleaner layout

The information in this dropdown menu is currently aligned to the left of the icon. I would like it to be aligned to the right. Thank you! <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://use.fontawesome.com/releas ...

Using Laravel, I have managed to pass the start and end price range through an ajax get request, but I am facing difficulties in returning it correctly

Array Result after selecting price rangeWhen attempting to retrieve products based on the specified price range, I call the controller function with the input values: <script> function send(){ var start = $('#amount_start').val(); var ...

Is it possible to maintain the width of an element even when its height is set to 0px?

Let me provide a detailed explanation of my issue below, but the question remains the same: Is there a way to make an element hidden or invisible with a height of 0px or slightly more than 0px while maintaining its width for functionality in Safari? Here ...

Unable to render the Google Gauge chart using the provided JSON information

I'm currently facing an issue with implementing Google charts on my website using data from a MySQL database through JSON encoding. Despite ensuring that my JSON encoded data is in the correct format based on various forums, I'm encountering diff ...

Issue with Slick Slider when expanding collapsible section in Bootstrap 4

Whenever I expand a bootstrap collapse that contains a carousel, only one carousel item appears instead of all. Is this a bug in Bootstrap or Slick Slider? Slider $('.remember__carousel').slick({ slidesToShow: 3, slidesToScroll: 1, prevA ...

`Send data to a C# controller function by utilizing <button> and jquery`

My bootstrap modal contains multiple buttons for downloading files in different formats. I can successfully trigger the controller method by setting the onclick function like this: onclick="location.href='@Url.Action("DownloadAsJPG", "Home")'" ...

The Dilemma of jQuery Dragging and Resizing Clash

I have implemented the Gridster Plugin() in my project. I discovered an updated demo on Github which includes resizable functionality. To ensure compatibility with future items, I have also integrated the jQuery livequery plugin. However, I am facing an i ...

Implementing a method to pass total value to the <td> tag in angular JS using controller

I am having trouble calculating the total IR for each table cell. Despite my efforts, the function is not working as expected and I can't figure out why. $scope.getTotalb = function () { var totalb = 0; for (var i = 0; i < $scope ...

Click to Resize Window with the Same Dimensions

I have a link on my website that opens a floating window containing more links when clicked. <a href='javascript:void(0);' onclick='window.open("http://mylink.html","ZenPad","width=150, height=900");' target='ZenPad'>&l ...