Tips for resizing the width automatically within a container?

I am facing an issue with a container that contains 3 DIVS: left sidebar, main content, and a right sidebar. I have made the main content responsive by setting the width to width:calc(100% - 400px); (where 400px is the combined width of the sidebars). However, the problem arises when one of the sidebars is hidden as the width of the main content does not adjust accordingly.

I am looking for a solution that does not involve using JavaScript (such as addClass/removeClass) on the main-content class. Is there a way to achieve this without JavaScript?

Fiddle

HTML

<nav><h2>Navigation menu</h2></nav>
<div ng-app="myApp" class="container" ng-controller="myCtrl">

<div ng-hide="showme" class="left-sidebar sidebar">
<h2>Left sidebar</h2>
<hr/><hr/>
  <center><button ng-click="showme=true">Collapse</button></center>
  <hr/><hr/>
</div>
<div class="main-content">
<h1>Documents</h1>
<table>
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Description</th>
      <th>Sender</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="json in jsonObj.documents">
      <td ng-bind="json.id"></td>
      <td ng-bind="json.name"></td>
      <td ng-bind="json.description"></td>
      <td ng-bind="json.sender"></td>
      <td ng-bind="json.date"></td>
    </tr>
  </tbody>
</table>
</div>
<div ng-hide="showme2" class="right-sidebar sidebar">
<h2>Right sidebar</h2>
<hr/><hr/>
  <center><button ng-click="showme2=true" >Collapse</button></center>
  <hr/><hr/>
</div>
</div>

CSS

.container {
  width: 100%;
  background: grey;
  height: 200px;
}

.container div {
  height: inherit;
  display: inline;
}

nav {
  background: black;
  height: 50px;
}

nav h2 {
  text-align: center;
  color: white;
  line-height: 50px;
}

.sidebar {
  background: blue;
  width: 200px;
}

.sidebar h2 {
  color: white;
  text-align: center;
}

.sidebar hr {
  margin-top: 25px;
  width: 150px;
}

.left-sidebar {
  float: left;
}

.right-sidebar {
  float: right;
}

.main-content {
  width: calc(100% - 400px);
  background: red;
  float: left;
}

.main-content h1 {
  text-align: center;
}

table {
  margin: 0 auto;
}

table,
thead,
th {
  background: white;
}

table tbody,
tbody,
td {
  background: grey;
}

Answer №1

To create a structured layout, consider using the table display property.

.container{
   .......
  display: table;
}

.container div{
    .....
 display: table-cell;
 vertical-align: top;
 }
....

.main-content{
  width: 100%;
  background:red;
}

http://jsfiddle.net/afelixj/8q7L3kfy/2/

Answer №2

Utilize flexbox for easy layout.

.container{
  width:100%;
  background:grey;
  height:200px;
  display: flex;
  flex-flow: row wrap;
}

nav{
    background:black;
    height:50px;
}
nav h2{
  text-align:center;
  color:white;
  line-height:50px;
}
.sidebar{
  background:blue;

 max-width:200px;
  flex: 1 auto;
}
.sidebar h2{
  color:white;
  text-align:center;
}
.sidebar hr{
  margin-top:25px;
  width:150px;
}
.left-sidebar{

}
.right-sidebar{

}
.main-content{
  background:red;
  flex: 2 0px;
}
.main-content h1{
    text-align:center;
}
table{
    margin:0 auto;
}
table, thead, th {
    background:white;
}
table tbody, tbody , td {
    background:grey;
}

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

The Angular Material md-input-container consumes a significant amount of space

Currently, I am exploring a sample in Angular Material. It appears that the md-input-container tag is taking up a substantial amount of space by default. The output is shown below: However, I have come across the md-input-content tag, which occupies less ...

Enable compatibility with high resolution screens and enable zoom functionality

My goal is to ensure my website appears consistent on all screen sizes by default, while still allowing users to zoom in and out freely. However, I've encountered challenges with using percentages or vh/vw units, as they don't scale elements prop ...

What is the best way to use PHP in order to retrieve the element containing the visit ID from the specific row within an HTML table?

I am working on a project that involves populating an HTML table with data from a database. The table includes an approval button for administrators to approve visits and change their status to APPROVED. I am trying to figure out how to select the element ...

How can I determine the specific quantity of XPATH links with unique identifiers in Selenium?

Seeking automation with Python3 and selenium to streamline searches on a public information site. The process involves entering a person's name, selecting the desired spelling (with or without accents), navigating through a list of lawsuits, and acces ...

Can one retrieve the CSS/XPath from a Capybara Element?

Is it possible to extract CSS/XPath from a Capybara Element? I have attempted to use #path but it doesn't appear to be effective. Here is the link I tried: The test is being executed on Selenium Webdriver. ...

Tips for deactivating all JavaScript events on a webpage that has been loaded within an iframe

My website is equipped with various JS click/mouseover/mouseout/scroll events and incorporates several JS libraries to operate features such as carousels, bootstrap modal pop-ups, and more. I am looking to halt all events on a specific element. Although I ...

What is the best way to implement a secondary sticky navbar that appears on scroll of a specific section, positioned below the primary fixed-top navbar, using jQuery and Bootstrap 5?

I am facing a challenge with my webpage layout. I have a fixed-top navbar at the top, followed by 3 sections. The second section contains nav-tabs. What I want is for the nav-tabs navbar to stick to the top of the page below the fixed-top navbar when the u ...

Unable to reinitialize the DataTable using Angular Datatable

I've been working on an Angular application that has a simple CRUD functionality. Initially, I tested my data with a static HTML table and everything was functioning as expected. However, I decided to implement a data table framework called Angular da ...

Checking if the group of radio buttons has been selected using JQUERY

Need help with validating a group of radio buttons. If none are checked, display an error message in the span element to prompt users to select an option. The strange issue I am facing is that the validation code works only when placed below the radio butt ...

Utilizing ReactJS to fetch data from Material-UI's <TableRow/> component upon selection - a comprehensive guide

I've integrated Material-UI's <Table/> (http://www.material-ui.com/#/components/table) component with <TableRow/> containing checkboxes in a ReactJS project. While I can successfully select rows by checking the boxes, I am struggling ...

`The LiipImagineBundle HTML encoding error`

Utilizing LiipImagineBundle, I efficiently resize my images after uploading. However, I encountered an error specifically when trying to upload certain JPEG files due to HTML encoding issues. The resizing process works fine with JPEG files from my camera, ...

Revealing child elements by expanding the absolute div from the center

I am trying to create a rectangular mask that expands on hover to display its children with varying heights. Currently, I have achieved this using an absolutely positioned div that adjusts its left, width, and max-height properties. However, I would like i ...

Optimizing Bootstrap for Tablet Browsing in Internet Explorer

Hey there, I have a question regarding Internet Explorer and tablets. As I revamp this website and integrate Bootstrap into it, my focus is currently on the small-sized IE browser for tablets. My dilemma arises from not knowing if there are any tablets th ...

Tips for overriding ng-disable in AngularJSUnleashing the

This is a comment box using AngularJS: <tr> <td colspan="5"><br/>Comments* <div><textarea class="form-control" rows="3" cols="60" ng-model="final_data.drc_scope" placeholder="Add comments here" ng-disabled="is_team==0 || isDis ...

Form tag definitions are missing on the login page

Here is a snippet of code I am using: <%= form_tag :action => 'process_login'%> Username: <%= text_field "user", "fullname" %>&#x00A; Password: <%= password_field "user", "password" %>&#x00A; <%= submit_t ...

Tips for deleting the drop-down arrow from Mozilla Firefox

Is there a way to eliminate the dropdown arrow that FireFox usually displays? I have included an image below showing what I am referring to: This is the CSS code I'm using: @-moz-document url-prefix(){ .class select { width: 110%; } } .class > ...

Executing a Firebase JavaScript script on a remote web server client

I have limited experience with Javascript and I am struggling to get my code to execute. I have already completed the Android java portion, but when I attempt to run the html file, nothing happens. I am unsure if there are bugs in my code or if it needs to ...

Retrieve the values and IDs for every span element within the form

I have been attempting to retrieve all span elements within a form and make them editable input text fields. Once you click away, they should revert back to span elements. I will provide a live example in the attached fiddle. I tried my hand at it, but the ...

Utilizing a Web Interface for Remote Monitoring of Windows Servers

I am in need of creating a webpage that will indicate whether a server is currently operational or not. These servers are all Windows based, with some running on 2008 and others on 2003. They are spread across different networks within various client locat ...

CSS guidelines for handling single line breaks with the white-space property set to pre-wrap

Is it considered acceptable to include solitary carriage return characters in an HTML block styled with white-space: pre-wrap? I am working with an application that receives SOAP messages and logs them to a file. The logging process involves removing new- ...