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

In CSS, there are two dynamic fields with adjustable widths, each occupying half of the total length

I have a collection of email addresses and names displayed in rows. My goal is to maximize the amount of content shown. For short names, more of the email address should be visible, while for shorter email addresses, more of the name should be shown. If bo ...

Difficulty activating JavaScript function - unsure of proper implementation

I'm having trouble with calling a function instead of an alert in my code. I tried using FunctionsName(); and removing the alert(''); but it doesn't seem to be working for me :( Could someone please take a look at the following code an ...

Unable to transmit an object containing a property that includes an array of other objects

I am attempting to send an object that has the following structure: var postBody = { id: userID, objectID: [0, 1], objects: [ {id: 0, x: 0.33930041152263374, y: 0.08145246913580247, width: 0.0823045267489712, height: 0. ...

Issue tracking the window scroll in Internet Explorer when using Jquery animation

Is there a way to make a div follow the window's scroll position smoothly? It seems to work fine in Firefox, but in Internet Explorer 6 and 7, the animation causes a jumpy effect with the window scrolling. I've attempted to use easing without su ...

Refreshing browser data with JQuery ajax when the browser is refreshed

Is there a way in JavaScript or jQuery to stop the page from refreshing (F5) and only update certain parts of the page using Ajax? I attempted the following code, but it did not work: $(window).bind('beforeunload', function(event) { ...

Adjust the content of an iframe based on the size of the screen

Hi there, I'm currently facing an issue with an ad that can't be resized. The support team suggested using two different ads - one for mobile and one for desktop. The dimensions of the ads are 720 x 90 and 300 x 100. I would like the ad to automa ...

Having trouble loading the image source using JSON in Vue.js

var topArticle=new Vue({ el:'#toparticle', data:{topmostArticle:null}, created: function(){ fetch('topnews.json') .then(r=>r.json()) .then(res=>{this.topmostArticle=$.grep(res,functi ...

Troubleshoot: Spring MVC AJAX file upload error code 415 - Media Type Not Supported

I've been attempting to upload files using jQuery's Ajax method, but I keep encountering a 415 (Unsupported Media Type) error on the client side. Interestingly, when I make non-Ajax requests, everything works perfectly. This is my controller cod ...

I'm curious if there's a method to ensure that the content within a mat-card stays responsive

So I'm working with this mat-card: <mat-card> <mat-card-content> <div *ngFor="let siteSource of siteSources | paginate: { itemsPerPage: 5, currentPage: page};"> <site-details [site]='siteSource'></s ...

Slowly revealing sticky navigation with slideDown animation using JQuery

Here is the code for a .JS file: $(document).scroll(function (){ var menuheight= $('header').height(); var y = $(this).scrollTop(); if (y>(menuheight)) { $('.menu_nav_features').addClass ...

Cookies are failing to be saved upon reloading the page

I found this snippet of code $(document).ready(function () { var d = new Date(); var newMinutes = d.getTimezoneOffset(); var storedMinutes = getCookieValue("tzom"); if (newMinutes != storedMinutes) { setCookie("tzom", newMinutes) ...

Difficulty with collapsing icon in Bootstrap's navigation bar

I am in the process of building a bootstrap website and facing an issue with the collapsible navigation bar. The nav toggle button is always visible, but I want it to only appear when the nav bar goes to a medium size. How can I achieve this? The code sni ...

Use jQuery to compare the input values whenever they are modified

I am trying to synchronize the input values of two inputs as they are typed in. The code I have seems to work sometimes, but not consistently. Here is the code snippet: $('#google-querynav').keypress(function() { var text = $(this).val(); ...

What is the best way to create underlined text that is either left-aligned or centered?

I am looking to replicate this specific style of decoration in a full width container. My goal is to have it aligned to the left of the text and restricted to a maximum width. https://i.stack.imgur.com/H8DXT.png ...

Arranging inline-flex elements within a div container

I have been struggling to position two elements side by side within a single div, where the second element should be on the right side. <div className={classes.container}> <div className={classes.first}> First </div> <d ...

Leverage a single property from a CSS class within another CSS class

I am facing an issue where I need to utilize one property from a CSS class in another CSS class. .class_a {margin:10px; width: 360px; float: left; color:#ffffff; ...etc...} .class_b { use the margin property of .class_a } Only utilize the margin propert ...

Creating space between flex items in Slick Carousel with Vue

This is my current Slick Carousel. There are 4 items in a row, and I am looking to insert some margin between each item while maintaining the existing aspect-ratio: 1.3/1; I'm struggling with overriding the classes from vue-slick-carousel. Does any ...

What is the process for simulating form submission using jQuery?

I am working on an ASP.NET web page with a form that includes a submit button. I have integrated validation logic into the client-side click event of the button. Now, I want to be able to submit this form through my jQuery validation logic. Can someone p ...

Generate a custom website using React to display multiple copies of a single item dynamically

As a newcomer to React and web development, I've been pondering the possibility of creating dynamic webpages. Let's say I have a .json file containing information about various soccer leagues, structured like this: "api": { "results": 1376, ...

Inquiries about utilizing setTimeout with backbone.js and effectively managing timeouts

One of the questions I have is related to clearing timeouts using clearTimeout(content.idTimeout) for a specific idTiemout. But how can I clear all timeouts at once? Here is the model I am working with: var ContentModel = Backbone.Model.extend({ URL: "htt ...