Ensure that the child div within the parent div occupies the entire width, even when the sidebar is hidden

Is there a way to make a child div take up 100% of its parent div's width?

I'm trying to create a layout with a sidebar and I want the main content area to fill up the remaining space by adjusting its width based on the sidebar

.bodySection {
  height: 100%;
  width: 100%;
  overflow: auto; 
}

.sidenav { 
  top: 0;
  bottom: 0;
  left: 0;

  position: absolute;
  overflow: hidden;
  z-index: 5;
  background: rgb(250, 250, 250);
  width: 230px;
  padding-top: 55px;
  box-shadow: 1px 1px 1px 1px rgba(0.2, 0.2, 0.2, 0.2);
}

.sidenav a {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 15px;
  color: #636363;
  display: block;
  background: rgb(247, 247, 247);
  
}

.sidenav a:hover {
    background: rgb(241, 241, 241);
}

.main {
  margin-left: 230px;
  background: rgb(187, 187, 187);
  /* Same as the width of the sidenav */
  padding: 0px;
<section class="bodySection">

  <div class="sidenav">

    <a href="#about">About</a>
    <a href="#services">Services</a>
    <a href="#clients">Clients</a>
    <a href="#contact">Contact</a>

  </div>

  <div class="main">
    Hello
  </div>

</section>

The goal is for the "Hello" div to take up 100% of the screen width, even when using JavaScript to hide the sidebar, and maintaining a height of 100%

Answer №1

Utilizing the css calc function is an excellent solution for achieving this outcome. Implementing the following code snippet:

.element {
  width: calc(100% - 230px);
}

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

What is the best way to position a single element in React using the Grid Component without causing any overlap?

I am struggling with positioning 3 components on my react page: PageHeader, SideMenu & FeatureList (which consists of Display Cards). Here is the code for each component: App.js // App.js code here... PageHeader.js // PageHeader.js code here... SideMenu ...

Concealing and revealing content using JQuery based on user input in

I'm attempting to create a feature where the visibility of a password field is determined by the value of another input element. For instance, when the username is set to "admin", the password field should be hidden. If any other value is entered in ...

What is the best way to toggle the active class on a ul list?

After clicking, the active class remains on the original "li" instead of changing as it should. I've tried modifying the code but haven't been able to find a solution. Can someone please review what I might have missed? It seems like there's ...

What could be causing my React button to stop functioning as a link to an external website when using anchor tags?

I recently created some buttons for my website using React. Initially, everything was working perfectly with the buttons, but suddenly they stopped functioning. Strangely, I didn't make any alterations to the code and I'm puzzled as to why they a ...

Switch up the arrangement of layout rows and columns using Bootstrap and CSS

On my desktop screen, the order of cols in Bootstrap is as follows: COL-A COL-B COL-C <div class"container"> <div class="row"> <div class="col-lg-4 col-md-4">Text 1</div> <div class="col-lg-4 col-md-4"><img sr ...

Tips for concealing the currently playing track title on a media player

I'm looking for a way to conceal the track title displayed in the media player on Internet Explorer. Here's the code I currently have: <object id="mediaPlayer" width="320" height="240" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type ...

Assign a new class to the anchor tag that has a distinct ID named "link"

I want to add the CSS class "active" to the anchor tag that contains the ID currently active in the URL. Here's what I've tried, but I'm unable to get window.location.hash to function properly. $( "a[href=' + window.location.hash + &ap ...

Allow users to modify multiple rows in a customizable table with Flask

I am currently working on a web application that utilizes Flask/Python as the back-end with SQLite, and HTML/CSS/JS (Bootstrap) for the front-end. In one specific section of the application, users should be able to edit multiple fields simultaneously in a ...

Struggling with generating forms using AJAX, Javascript, and HTML depending on the selection made from a drop-down menu

I am in need of a simple web form for work submissions within my organization. These submissions will fit into 4 Categories, each requiring unique information. Currently, I have a basic form set up with fields such as Requested Name, Requested Date, Acquis ...

Implement a substitution method that will convert designated text into a designated class

Hey there! I'm currently working on creating a jQuery function called replace that will swap out specific text in the HTML to assign it a class. I've been diving into jQuery and Html. Check out this fiddle: http://jsfiddle.net/davidThomas/juTtG/ ...

Setting the transition time for adding or removing components in ReactJS CSS

As the list component changes in size based on its children, it tends to move around abruptly. Applying transition: 'all 0.3s ease' doesn't resolve this issue since it's the layout that is affected by the number of children rather than ...

Certain Android devices may experience compatibility issues with the web app

I am facing an issue with my webapp on Raspberry Pi, as it is not being recognized properly by all my Android devices. The server hosting the website is a Raspberry Pi 3 running Apache and everything is up to date. To protect privacy and security, all IP ...

Is there a method to individually collapse each div?

Each question in the database has a corresponding button. When one of these buttons is clicked, only the answer to that specific question should be displayed. However, the issue currently is that clicking on any button reveals all the answers simultaneousl ...

Avoiding overlap in CSS Grid layout with Internet Explorer 11

Creating a date picker component that utilizes a calendar styled with CSS grid has been successful, except in IE11 where all elements appear in the top left corner. Is there a specific CSS property for IE11 that could fix this, or is it simply not compatib ...

How can I use jQuery to target elements other than the vertical scrollbar when

Here is how I am utilizing the mouseleave jquery event $(document).ready(function(){ $(document).mouseleave(function(event) { //perform a task }); }); Is there any method to prevent this event from triggering when a user scrolls ...

Steps to retrieve the final page number from ngx-pagination with Angular

Is there a way to utilize Custom templates within ngx-pagination in order to ensure that the first and last buttons function properly when clicked? Currently, I have utilized pagination-template to accomplish this... How can I dynamically determine the la ...

Apply a new class to an element's id using jQuery

Looking for a way to change the plus symbol to a minus symbol when clicked in my HTML code. <div class="col-3"> <a href="#tab-1"> <span class="plus" data-tab="tab-1">+</span> </a> </div> <div class="col-3"> & ...

struggling to preserve form data using PHP and MySQL

I am facing an issue with a page that needs to display a form with checkboxes, save the results in the database, and then load the page with the checkboxes checked based on the stored data. Although I realize that my solution of floating "checked" is not ...

The issue encountered is a TypeError stating that it is unable to retrieve properties of an undefined value, specifically in relation to the 'imageUrl

When I include the following line of HTML code: <td> <img align="center" [src]="productByBarCode.imageUrl" /> </td> An error is thrown by the console: ERROR TypeError: Cannot read properties of undefined (reading &a ...

Ways to decrease the height of the navigation bar in Bootstrap version 4.1

Struggling to figure out how to decrease the height of the navbar in bootstrap v4.1. I want it to be specifically 24px tall; Thanks, Richard. ...