Expandable Section with only header visible when collapsed to the side

I am currently working on developing a Panel in the form of a div with two additional divs inside (one for the title and one for the content). Here is what I have in mind:

<div class="left-panel">
    <div class="title">
        <span>title<span>
    </div>
    <div class="content">
        <span>some content</span>
    </div>    
</div>

My goal is to be able to collapse this panel by clicking on an arrow. When collapsed, it should transform into a narrow bar on the left side, allowing adjacent panels to expand wider. The desired effect can be seen in this image: https://i.sstatic.net/gQSk6.jpg I am encountering issues with writing the necessary CSS to achieve this functionality. How can I adjust the CSS to make it look like the example? Specifically, how can I make the content part disappear and rotate the entire panel along with the title div to the left side?

Answer №1

To implement this functionality in JavaScript, you can toggle the class ('collapsed' or a similar name) on the parent element (left-panel). You can then add additional CSS rules for .left-panel.collapsed, like so:

.left-panel.collapsed > .content {
    display: none;
}

.left-panel.collapsed {
    transform: rotate(-90deg);
}

These rules handle basic rotation and hiding of content. To enhance the animation, size, position, etc., you may need to include more CSS properties.

Answer №2

Utilize this code snippet to achieve the desired outcome!

REFERENCE: https://www.w3schools.com/howto/howto_js_sidenav.asp

function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
body {
  font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
<h2>Animated Sidenav Example</h2>
<p>Click on the element below to open the side navigation menu.</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>   
</body>
</html> 

Answer №3

$('#toggle').click(function toggle(){
$("#sidebar").toggleClass("collapsed");
$("#content").toggleClass("col-sm-12 col-sm-9");
});
.collapsed {
        display: none;
        margin-left: -25%; /* same width as sidebar */
    }
.btn{
  display: block !important;
  }
<!DOCTYPE HTML>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <div class="row" id="row-main">
        <div class="col-sm-3" id="sidebar">
            Your sidebar content
        </div>
        <div class="col-sm-9 pull-right" id="content" style="float:left;">
               <button class="btn" id="toggle">Toggle</button>
      </div>
    </div>
</div>
</body>
</html>
Place the following code to the left side of the webpage

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

Unable to retrieve value from a hidden input field using JavaScript

My goal is to retrieve a value from a hidden inputbox using JavaScript. However, I am encountering issues where sometimes I receive an "undefined" error and other times there is no output at all. When I utilize alert(document.getElementById('hhh& ...

What is the best way to transfer data between HTML files using Python?

Is it possible to pass values from one HTML file to another and then retrieve those values in the second HTML file? This is the code I am currently using: with open('page.html', 'w') as myFile: myFile.write('<html>&apo ...

Extract latitude and longitude data using Mapbox's autocomplete feature

Currently, I have integrated Mapbox with autocomplete in a Vue component: <template> <div> <div id='geocoder'></div> </div> </template> <script> import mapboxgl from 'mapbox-gl& ...

Issues with uploading files in NodeJs using express-fileupload are causing errors

I created a REST API in NodeJs for File Upload which is functioning correctly, however I am facing an issue. When I upload more than 2 images, only 2 or 3 get uploaded and sometimes one gets corrupted. I suspect that the loop is running too fast causing th ...

Show exclusive results of search queries from the database

I'm currently facing an issue with my PHP code where it displays all the data from the database instead of the specific data I am searching for. I have created a form to enter the desired data but it seems to be pulling all the information. Any sugges ...

Encountering problem with selecting values in Select2 with ajax

I have implemented the Select2 plugin in my code as shown below: JavaScript function initAssignmentsAjax() { $("#assignments").empty(); $( "#assignments" ).select2( { placeholder: "Select an assignment", allowCle ...

Issue with Bootstrap: Navbar elements shifting when page width is decreased

I am facing an issue with my two navbars, one above the other. The top bar is thinner and consists of two navigation items aligned to the right of the page. Despite setting it not to collapse when the page width is reduced, the items suddenly disappear whe ...

The error message encountered with React Easy DatePicker is: "Uncaught ReferenceError: process is not defined."

I am attempting to integrate the stylish DatePicker from https://www.npmjs.com/package/sassy-datepicker?activeTab=readme Here is my implementation : import DatePicker from "sassy-datepicker"; function App() { const [date, setDate] = useSta ...

Verify that the select field has been chosen using Protractor

Currently, I am in the process of developing a test that places importance on whether a select field has already been populated. it('should automatically select a field if it hasn't been selected previously', function() { var usersField = ...

Performing AJAX callback function on success in jQuery

I've been trying to troubleshoot an error but none of the solutions I've found so far seem to be working for me. I have an ajax request set up to retrieve JSON data from the server. I can see the JSON data that I want to capture when using consol ...

Manage your profile images effortlessly by automatically removing any that are not currently in use

Seeking assistance here! I want to develop a PHP script activated by a button on the admin page. The script should locate all image names stored in the "photos" database table and remove any images from my 'userimages' directory that are no longe ...

Angular 2: The window.crypto.subtle.importKey function functions properly on 'localhost' but encounters issues on an 'ip' address

As a newcomer to Angular 2, I am working on creating a login form that encrypts and sends the user's emailid and password to the server. I have successfully implemented AES-ECB using AES-CTR from the following link: https://github.com/diafygi/webcry ...

jQuery functions not functioning properly when triggered by an event

I encountered an issue with my page using jQuery and Bootstrap. Everything was functioning properly until I tried calling a function on an event, which resulted in the console showing an error message saying $(...).function is not a function. For example: ...

Tips for getting the setInterval function to work with a progress bar even when a tab is not in focus

After browsing through similar questions, I couldn't find the answer I was looking for. As a newbie in programming experimenting with JavaScript Progress Bar, I encountered an issue where the progress bar would pause and the counter wouldn't coun ...

Use a variable to determine which image to display on various pages

As a PHP beginner, I have created a loop on my page to display images in a gallery format. Each image has a title displayed below it, which is actually a link leading to a second page where only that specific image is shown. The issue I'm facing is th ...

Keep an ear out for upcoming occasions once the transition has been completed

I am looking to create a smooth transition effect that will push the main content downwards when the Twitter Bootstrap collapse menu is activated using the toggle button. However, I have noticed an issue where if I quickly double click the toggle button, ...

Placing horizontal text over a Bootstrap 5 grid

Currently working my way through Boostrap5 while developing my website and I am extremely pleased with the progress so far. However, I have hit a snag - I want the name to appear vertically when hovering over the images on the bottom left, but for some rea ...

Using AJAX (jQuery) to process and refine JSON data through filtration

I need assistance with filtering a JSON array using AJAX but I'm unsure of how to proceed. { posts: [{ "image": "images/bbtv.jpg", "group": "a" }, { "image": "images/grow.jpg", "group": "b" }, { "image": "images/tabs.jpg", ...

Retrieving PHP information using Javascript and storing it in an array

I have a PHP script that fetches data from a server and stores it in an array. I then convert this array into a JSON object using the following function: echo json_encode($result); Now I want to access this array in my JavaScript and display it. I want t ...

Trigger a function in jQuery when the DOM undergoes changes

Up until now, I have been utilizing livequery which has served its purpose. However, it tends to slow down the page browsing experience, so I am in search of an alternative solution. I have a function that performs ajax on elements with a specific class l ...