Open In App

How to put two columns one below other in sidebar in Bootstrap?

Last Updated : 18 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

To place two columns one below the other in a sidebar using Bootstrap, you can use Bootstrap's grid system. By setting up a vertical stacking of columns within a sidebar container, you ensure that the columns appear one below the other. This is achieved by using a single column width and placing them within a vertical layout.

Approach

  • Integrate Bootstrap via CDN link inside the head tag of the HTML.
  • A row <div class="row"> is created, organizing content horizontally. Inside this row, two columns are defined: one <div class="col-3"> occupying 3 out of 12 grid columns, and the other <div class="col-9"> taking up the remaining 9 columns.
  • The first column, designated with a class of "col-3," represents the sidebar.
  • Within the sidebar column, a list of menu items is displayed. Each item is contained within its column and centered using inline styles for text alignment and padding.
  • The second column, denoted as "col-9," represents the main content area. It includes a row with a heading for "Page Content" and two additional rows, each containing a column with distinct content. The content columns are styled with background colors to differentiate
  • In the second example, the sidebar isn't constantly visible but rather collapsible, allowing the page content to shift when it's opened. Initially hidden, the sidebar emerges when the user clicks the hamburger button, activating the side_open() JavaScript function. It then displays a close button for toggling closure, which invokes the side_close() function. When open, the sidebar occupies 25% of the page width, while closed, it takes up 0%, dynamically adjusting the layout of the two columns. 

Example 1: In the first approach, we will demonstrate a sidebar that is always open and fixed to the left side of the page. The code contains nested rows and columns to get the desired output. 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
        integrity=
"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" 
        crossorigin="anonymous">
</head>
  
<body>
    <!-- Main container holds the
        entire content -->
    <div class=container>
        <!-- Row -->
        <div class="row">
            <!-- This div takes 3 out of
                12 columns in the grid -->
            <div class="col-3" style="background-color: #DEFFFB">
                <!--This row contains the col which
                shows the column heading-->
                <div class="row">
                    <!-- this div takes 12 out of
                    12 columns in the grid -->
                    <div class="col" style="text-align: center;
                            font-weight: bold;
                            font-size: 25px;
                            color: green">Menu
                    </div>
                </div>
                <!-- This row contains the menu items-->
                <div class="row">
                    <div class="col" style="text-align: center;
                            padding: 15px;">
                        <a href="#">Link 1</a><br> <br>
                        <a href="#">Link 2</a><br><br>
                        <a href="#">Link 3</a>
                    </div>
                </div>
            </div>
            <!--This column takes 9 out of 12
            columns available-->
            <div class="col-9">
                <div class="row" style="height: 50px">
                    <div class="col" style="text-align: center;
                            background-color: green;
                            font-weight: bold;
                            font-size: 25px;
                            color: white">Page Content
                    </div>
                </div>
                <!--This row contains the first column-->
                <div class="row">
                    <div class="col" style="background-color: lightgreen;">
                        Bootstrap is a free and open-source
                        tool collection for creating responsive
                        websites and web applications.
                        It is the most popular HTML, CSS,
                        and JavaScript framework for developing
                        responsive, mobile-first web sites.
                    </div>
                </div>
                <!--This row contains the second column-->
                <div class="row">
                    <div class="col" style="background-color: #E6FFE3 ;">
                        Bootstrap is a free and open-source tool
                        collection for creating responsive websites
                        and web applications. It is the most popular
                        HTML, CSS, and JavaScript framework for
                        developing responsive, mobile-first web sites.
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
  
</html>

Output:  Example: The example shows the above-explained approach.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
        integrity=
"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" 
        crossorigin="anonymous">
    <!-- JS, Popper.js, jquery and
        jQuery autocomplete -->
    <script src=
"https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity=
"sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 
        crossorigin="anonymous">
    </script>
    <script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
        integrity=
"sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" 
        crossorigin="anonymous">
    </script>
    <script src=
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
        integrity=
"sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" 
        crossorigin="anonymous">
    </script>
</head>
  
<body>
    <!--Main container that contains the
            main content-->
    <div class="container">
        <!--Main row -->
        <div class="row">
            <!--Display is set to none so that it
                    remains hidden when page loads-->
            <div class="col-3" id="mySidebar" style="display: none;
                    background-color: #fbdeff;">
                <!--Button is used to close the sidebar-->
                <button onclick="side_close()">
                    Close ×
                </button>
                <!--Menu items-->
                <h1>Menu</h1>
                <a href="#">Link 1</a>
                <br/>
                <br/>
                <a href="#">Link 2</a>
                <br/>
                <br/>
                <a href="#">Link 3</a>
            </div>
            <!-- Main page content -->
            <div class="col-9" id="main">
                <!-- Button used to open the sidebar -->
                <button id="openNav" onclick="side_open()">☰</button>
                <div class="row" style="background-color: #c2ff95;">
                    <div class="col">
                        <h1>Page Content</h1>
                    </div>
                </div>
                <div class="row" style="background-color: #deffe2;">
                    <div class="col">
                        Bootstrap is a free and open-source
                        tool collection for creating
                        responsive websites and web
                        applications. It is the most
                        popular HTML, CSS, and JavaScript
                        framework for developing responsive,
                        mobile-first web
                        sites.
                    </div>
                </div>
                <div class="row" style="background-color: #b8b7f9;">
                    <div class="col">
                        Bootstrap is a free and open-source
                        tool collection for creating
                        responsive websites and web
                        applications. It is the most
                        popular HTML, CSS, and JavaScript
                        framework for developing responsive,
                        mobile-first web
                        sites.
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- JavaScript functions to control the sidebar-->
    <script>
        // JavaScript functions to open the sidebar
        function side_open() {
            /* Sidebar takes 25% of the total width
            of main container in open state */
            document.getElementById(
                "mySidebar").style.width = "25%";
            document.getElementById(
                "mySidebar").style.display = "block";
            document.getElementById(
                "openNav").style.display = "none";
        }
        // JavaScript functions to close the sidebar
        function side_close() {
            // Sidebar takes 0% of the total width
            // of main container in open state
            document.getElementById(
                "main").style.marginLeft = "0%";
            // Visibility is hidden
            document.getElementById(
                "mySidebar").style.display = "none";
            document.getElementById(
                "openNav").style.display = "inline-block";
        }
    </script>
</body>
  
</html>

Output:

onea
Output

Next Article

Similar Reads