Creating flexible DIV columns that adjust to display changes using Bootstrap 3's CSS

I am currently facing a challenge with a simple layout problem:

My objective is to create a toggle side menu. When the side menu is visible, the main layout should be 80% width, and when it is not visible, the main layout should be 100% width, as shown below:

I want to incorporate Bootstrap 3 and utilize their CSS styles so that I can easily add rows and columns to the primary div (highlighted in light orange in the diagram).

Your assistance would be greatly appreciated :)

UPDATE:

This is my current progress:

<div class="container">
<div id="sidebar" style="border:solid 1px;width:20%;float:left">SIDEBAR</div>
<div style="border:solid 1px;width:100%">
    <div style="border:solid 1px red">
        <div class="row">
            <div class="col-lg-6">Some Content</div>
            <div class="col-lg-6">
                <div class="row">
                    <div class="col-lg-12">Some content</div>
                </div>
            </div>
        </div>
    </div>
</div>

<button onclick="document.getElementById('sidebar').style.display = document.getElementById('sidebar').style.display == 'none' ? 'block':'none'">TOGGLE</button>

Demo

Answer №1

In reality, you cannot simply use 20% in Bootstrap grid sizing because it operates on a 12-column system. This means that 100% equals to 12 out of 12 columns, making the smallest unit 1/12 which is equivalent to 8.33%. The best approach for your sidebar would be to make it 3/12 or 25%. Here is the code snippet for achieving this:

<div class="row">
  <div class="col-xs-12 col-sm-3">
    sidebar content
  </div>
<div class="col-xs-12 col-sm-9">
    main content here
  <div class="row">
    <div class="col-xs-12">
      your 100% main content here
    </div>
  </div
</div>

Is my assumption correct that the bottom image represents how it will appear on a mobile device..?

Answer №2

I recently tackled a similar problem and found a solution that worked well for me. Below is the code snippet that I used in a file called test.php:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Toggle Demo</title>
  <link rel="stylesheet" type="text/css" href="PATH TO/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="PATH/demo.css">
  <script type="text/javascript" src="PATH/respond.min.js"></script>

</head>
<body>


<div class="container">
  <div class="row" >
      <div class="col-sm-3 side20" > This is side content</div>
      <div class="col-sm-9 main80">This is the main content.</div>
  </div>
  <button class="btn btn-default" role="button">Toggle</button>
  <div class="row" >
      <div class="col-sm-3 side20" >n</div>
      <div class="col-sm-9 main80" >This is the main content</div>

  </div>

<footer class="row">
<div class="col-xs-6 copyright">
    &copy Toggle Demo - <?php echo date('Y');?>
</div>
<div class="col-xs-6 design">
    <small>Designed by Learner</small>
</div>
</footer>
</div>
<!-- JavaScript -->
<script src="PATH TO/jquery-1.11.0.js"></script>
<script src="PATH TO/bootstrap.min.js"></script>
<script type="text/javascript" src="PATH TO/custom.js"></script>
</body>
</html>

The CSS styles used can be found in demo.css:

.container{
    width: 100%;
    background...
   
   (The remaining CSS styling has been omitted for brevity)
}

To enable the toggle functionality, I implemented the following jQuery script in custom.js:

$(".btn").on("click", function(){
    $(".col-sm-3").toggle();
    $(".main80").toggleClass("col-sm-12");
    $(".main80").toggleClass("col-sm-9");
});

I hope this code snippet helps you with your project. Happy coding!

Answer №3

Discovered a unique CSS solution involving the use of table display:

Utilizing HTML and CSS:

<div class="container">
<div style="display:table;width:100%;border:solid 1px red;height:100px">
    <div style="display:table-row">
        <div id="side" style="display:table-cell;background:#CCC;width:30%">SIDE</div>
        <div style="display:table-cell">
            <div class="row">
                <div class="col-xs-6">6 col</div>
                <div class="col-xs-6">6 col</div>
            </div>
        </div>
    </div>
</div>
<button class="btn btn-primary" onclick="toggle()">TOGGLE</button>

Click here for JS Fiddle link, showcasing cell display toggle functionality

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

Does the Bootstrap 4 flex system allow for arranging self-row items within a parent column?

Displayed below is the navbar code snippet: <div class="d-flex flex-column flex-lg-row"> ...... </div> As shown in the example: .bd-highlight { background-color: rgba(86,61,124,.15); border: 1px solid rgba(86,61,124,.15); } < ...

The display of website content across various screens

I'm relatively new to creating websites using scripts, CSS, etc. But I feel like I'm getting the hang of it pretty well... Now I've reached a point where I want my site to look good on different screen resolutions. Currently, I have somethin ...

Issues with label tags not functioning properly on modal windows

I'm attempting to add label tags to my modal window in order to create a login form, but they're not appearing. You can view the issue on this fiddle Here's my jQuery code: $(document).ready(function () { var activeWindow; $(' ...

Form that adjusts input fields according to the selected input value

I am in need of creating a dynamic web form where users can select a category and based on their selection, new input fields will appear. I believe this involves using JavaScript, but my searches on GitHub and Stack Overflow have not yielded a suitable sol ...

Setting row heights based on text in a chosen column within Excel

Hey there, I'm currently using Java code to generate a report in Excel. I've managed to set the height of rows and width of columns, but I've run into an issue where some data in a column is too large and affecting the height of other column ...

What could be causing Django to fail in including an entry to my Model table?

In my Django project, I am using an HTML page within Views to handle form data and interact with Models to store this data in databases. This process works smoothly when manually inputting entries directly into the database from Views. However, when tryin ...

"Capturing the essence of your online presence: The

I recently completed a website for my Students Organization, which includes a special page dedicated to recognizing the individuals who helped organize our activities. Each member is represented with a photo on this page, sourced from their Facebook profil ...

Is it possible to verify the compatibility of certain at-rules in CSS?

I am in search of a counterpart to @supports specifically for at-rules. I desire the ability to execute: @supports (@counter-style) { things } My goal is to achieve this using only CSS/HTML. Can anyone guide me on how to accomplish this task? ...

Why does the styling of the inner Span adhere to the style of the outer Span?

How can I adjust the opacity of the color "blue" to 1 in the code snippet below? <!DOCTYPE html> <html> <body> <p>My mom's eyes are <span style="color:blue;font-weight:bold;opacity:0"> <span style="color:blue;fo ...

What steps are involved in interactive image coloring using JavaScript?

I am currently working on a web application using the Angular framework, and I am in need of a feature where users can color specific parts of an image or add indicators like colored dots. I want this section to interact with the backend, so I am wondering ...

Transferring Information from EventSource Server Sent Event (SSE) to a Different Variable

I have a webpage that updates latitude and longitude values from the database every few seconds and displays them. I am successfully using Server-Sent Events to achieve this functionality. However, I'm facing an issue with passing these latitude and l ...

The jquery fancybox ajax modal popup fails to display in Internet Explorer 7

Recently, I utilized JQuery fancy box for the first time to display a menu list. When clicking on a specific item, the page is rendered properly in an iframe. However, if there is already an AJAX model popup present on the rendered page, it doesn't di ...

Creating unique CSS div designs based on the nth-child selector

I have created a website layout. https://i.stack.imgur.com/6FSEb.png I have included the file showing the design. The data in the boxes will come from a MySQL database. My query is, can it be done with just one query? Currently, I am running separate q ...

Safari's approach to image height measurement

I am experiencing an issue with some images in Safari. While they display correctly on other browsers, the images are appearing too high on Safari. You can view the website at development.mar-bakker.nl <div class="col-xs-12 col-sm-4 col-md-4"> ...

Show the concealed div while simultaneously hiding others to only reveal one at a time

I have created a list of items in my HTML code. Each item has a hidden div that contains more detailed information about the item. Currently, I am able to display the details of a specific item by clicking on an 'onclick' link. However, I am faci ...

What could be causing the CSS to not display properly on specific routes?

I'm facing an issue where CSS seems to be working fine for certain routes with node.js & express, but not for others. It's puzzling because the HTML file links to the same CSS file in both cases. For instance, CSS works perfectly for the route " ...

What is the best way to eliminate all styles from an element with jQuery?

body{ font-family: Arial; } div{ color: red; margin: 20px; background: blue; font-size: 17px; } <div id="div1">this should be styled with a blue background, red text, and 17px Arial font</div> <div id ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

Outlook 2010 failing to display background-color correctly

Recently, I've been facing a challenge in getting my email to display correctly in Outlook 2010 while maintaining compatibility with web rendering. I'm trying to steer clear of using table styling for obvious reasons but haven't had much suc ...

Is it possible to trigger multiple button clicks using JQuery simultaneously?

Hello everyone, I am new to StackOverflow and this is my first question here. I hope it's not too silly. Thank you in advance for your help! I am trying to achieve a functionality where one button click triggers the clicks of multiple other buttons u ...