Expand the div to fit the rest of the screen following the fixed-size navigation bar

I am looking to create a webpage layout that includes a header, mid section, and footer. In the mid section, I want to have a fixed width navigation bar of 250px, with another div holding the content that stretches to fill the remaining space in the browser window. While this may typically be achieved using JavaScript, I believe it can also be done purely with CSS.

Below is a sample test page:

body {
  margin: 0;
  padding: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

header,
footer {
  width: 100%;
  height: 100px;
  float: left;
  background: red;
}

#content {
  width: 100%;
  height: 80%;
  float: left;
  background: blue;
}

nav {
  width: 20%;
  max-width: 250px;
  /*override*/
  width: 250px;
  height: 100%;
  float: left;
  background: green;
}

#inside {
  width: 80%;
  height: 100%;
  float: left;
  overflow: auto;
}

#inside div,
#inside h1 {
  width: 1000px;
  margin: 40px auto;
}
<header>
  <h1>test layout</h1>
</header>

<div id="content">
  <nav>
    <ul>
      <li>link 1</li>
      <li>link 2</li>
      <li>link 3</li>
      <li>link 4</li>
      <li>link 5</li>
    </ul>
  </nav>

  <div id="inside">
    <h1>I want this box to stretch to the remaining size</h1>
    <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam, ad, ea, cupiditate odio molestiae molestias modi qui est architecto aliquid nostrum voluptatum exercitationem quos omnis ipsa repellat voluptas voluptates alias.</div>
    <div>Error, repellat voluptatibus necessitatibus ad voluptate velit labore quod modi aliquid laborum? Sed, qui, tenetur facere maxime quis molestias accusantium deleniti natus nam odit et odio voluptatem eligendi expedita porro!</div>
    <div>Non, ea, explicabo quasi unde sed quo rerum consequuntur reprehenderit placeat recusandae repudiandae nulla nemo adipisci? Unde, dolor explicabo dicta sint nostrum eligendi obcaecati minus pariatur vero alias magnam eum!</div>
    <div>Quo, alias, placeat, mollitia quisquam impedit ea recusandae officia illum accusantium repudiandae eos nam cum aspernatur tenetur ab explicabo error deserunt officiis voluptas dicta eum praesentium fugiat quas molestiae sed.</div>
    <div>Obcaecati, laudantium cumque dolor sapiente deleniti voluptatem tenetur voluptates iusto nostrum aliquid. Pariatur, at, nemo voluptate repudiandae sapiente quibusdam cum eligendi voluptatibus soluta eos perferendis explicabo magnam asperiores iure
      et!</div>
    <div>Saepe, ut, ad, accusamus, voluptates ex omnis neque delectus aperiam quibusdam maiores beatae nemo blanditiis culpa quaerat corrupti nostrum voluptas ipsam. Ratione, accusantium, nostrum quis corporis recusandae error quos amet!</div>
    <div>Voluptate, et, perspiciatis, voluptates harum vero impedit error libero atque saepe minus distinctio sint officiis laudantium eaque quae dolore incidunt minima enim excepturi aliquid. Provident omnis inventore voluptate explicabo necessitatibus.</div>
    <div>Labore, error voluptatibus modi alias aspernatur quidem maiores cumque provident saepe esse! Aperiam, nesciunt, praesentium, ut, facilis explicabo dolore cumque totam quisquam architecto magni tenetur reiciendis hic blanditiis facere tempora.</div>
    <div>Molestiae, cumque, eum, corporis, expedita quam hic eos repudiandae unde architecto consequatur ducimus odit. Itaque, iste, id, ut laudantium quo reprehenderit ab labore quod laborum consequatur non est. Dolor, culpa.</div>
    <div>Delectus, totam, cum doloremque ad nostrum veniam aliquam non voluptatum eligendi similique saepe laborum sed nesciunt sint voluptate at placeat officia ducimus vel aliquid unde accusamus eius! Itaque, amet, repellat.</div>
    <div>Autem, alias assumenda nihil dignissimos dicta magnam voluptates neque cum eveniet non. Voluptate, quasi, tempora soluta ipsam recusandae qui ab excepturi esse possimus totam corporis quae magnam cum fugit nihil.</div>
  </div>


</div>
<!-- END CONTENT -->

<footer>
  stuff
</footer>

If CSS solution isn't possible, here is the JavaScript code that could achieve the desired layout:

var inside = document.getElementById('inside');

function resize(){
    var width = window.innerWidth - 250;
    inside.style.width = width + 'px';
}

window.addEventListener('resize', resize, false);

resize();

Answer №1

It appears that there were several layout errors in your code, with the most noticeable being the use of overflow:hidden; on the body tag. This can result in content being hidden from users.

I have rectified most of these errors and created a layout that matches what you are looking for:

VIEW THE LAYOUT

CSS:

 body, html {
     height: 100%;
     margin:0;
     padding:0;
}
 h1 {
     margin: 0;
 }
 header, footer {
     width:100%;
     height: 100px;
     background: red;
 }
 #content {
     position:absolute;
     top:100px;
     bottom:100px;
     width:100%;
     background: blue;
 }
 nav {
     width: 20%;
     max-width: 250px;
     /*override*/
     width: 250px;
     height: 100%;
     float: left;
     background: green;
 }
 #inside {
     height:100%;
     overflow:auto;
 }
 footer {
     position:absolute;
     bottom:0;
 }

Answer №2

Check out this code snippet on jsFiddle

    body {
    margin: 0;
    padding: 0;
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

header, footer {
    width: 100%;
    height: 100px;
    float: left;
    background: red;
}

#content {
    width: 100%;
    height: 80%;
    float: left;
    background: blue;
}

nav {
    width: 20%;
    max-width: 250px;
    /*override*/
    width: 250px;
    height: 100%;
    float: left;
    background: green;
}

Answer №3

Here is your customized code snippet

<body>
<div id="Main_div">
<header><h1>test layout</h1></header>

<div id="content">
<nav>
    <ul>
        <li>link 1</li>
        <li>link 2</li>
        <li>link 3</li>
        <li>link 4</li>
        <li>link 5</li>
    </ul>
</nav>

<div id="inside">
    <h1>Custom message goes here</h1>
    <div>Lorem ipsum dolor sit amet...</div>
    <div>Error, repellat voluptatibus...</div>
    <div>Non, ea, explicabo quasi unde...</div>
    <div>Quo, alias, placeat, mollitia...</div>
    <div>Obcaecati, laudantium cumque...</div>
    <div>Saepe, ut, ad, accusamus...</div>
    <div>Voluptate, et, perspiciatis...</div>
    <div>Labore, error voluptatibus...</div>
    <div>Molestiae, cumque, eum consectetur ...</div>
    <div>Delectus, totam, cum doloremque...</div>
    <div>Autem, alias assumenda nihil...</div>
</div>


</div> <!-- END CONTENT -->

<footer>
stuff
</footer>
</div>
</body>

CSS styling for the above HTML

body {
    margin: 0;
    padding: 0;
    position:absolute;
    width: 100%;
    height: 100%;

}
#Main_div{
height:100%;
width:100%;

}
header, footer {
    width: 100%;
    height: 20%;       
    background: red;
}

#content {
   width:100%;
    height:100%;        
    background: silver;

}

nav {
    width: 30%;        
    /*override*/        
    height: 100%;
    float: left;
    background: green;

}

#inside {
margin-left:32%;    
width: 65%;
    height: 100%;        
    overflow: auto;
}

Check out the design below:

Please review the visual representation of the header, footer, and content

Answer №4


    <body>
    <div id="Main_div">
    <header><h1>custom layout</h1></header>

    <div id="content">
    <nav>
        <ul>
            <li>link A</li>
            <li>link B</li>
            <li>link C</li>
            <li>link D</li>
            <li>link E</li>
        </ul>
    </nav>

    <div id="inside">
        <h1>I want this box to expand to fit the rest of the space</h1>
        <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam, ad, ea, cupiditate odio molestiae molestias modi qui est architecto aliquid nostrum voluptatum exercitationem quos omnis ipsa repellat voluptas voluptates alias.</div>
        <div>Error, repellat voluptatibus necessitatibus ad voluptate velit labore quod modi aliquid laborum? Sed, qui, tenetur facere maxime quis molestias accusantium deleniti natus nam odit et odio voluptatem eligendi expedita porro!</div>
        <div>Non, ea, explicabo quasi unde sed quo rerum consequuntur reprehenderit placeat recusandae repudiandae nulla nemo adipisci? Unde, dolor explicabo dicta sint nostrum eligendi obcaecati minus pariatur vero alias magnam eum!</div>
        <div>Quo, alias, placeat, mollitia quisquam impedit ea recusandae officia illum accusantium repudiandae eos nam cum aspernatur tenetur ab explicabo error deserunt officiis voluptas dicta eum praesentium fugiat quas molestiae sed.</div>
        <div>Obcaecati, laudantium cumque dolor sapiente deleniti voluptatem tenetur voluptates iusto nostrum aliquid. Pariatur, at, nemo voluptate repudiandae sapiente quibusdam cum eligendi voluptatibus soluta eos perferendis explicabo magnam asperiores iure et!</div>
        <div>Saepe, ut, ad, accusamus, voluptates ex omnis neque delectus aperiam quibusdam maiores beatae nemo blanditiis culpa quaerat corrupti nostrum voluptas ipsam. Ratione, accusantium, nostrum quis corporis recusandae error quos amet!</div>
        <div>Voluptate, et, perspiciatis, voluptates harum vero impedit error libero atque saepe minus distinctio sint officiis laudantium eaque quae dolore incidunt minima enim excepturi aliquid. Provident omnis inventore voluptate explicabo necessitatibus.</div>
        <div>Labore, error voluptatibus modi alias aspernatur quidem maiores cumque provident saepe esse! Aperiam, nesciunt, praesentium, ut, facilis explicabo dolore cumque totam quisquam architecto magni tenetur reiciendis hic blanditiis facere tempora.</div>
        <div>Molestiae, cumque, eum, corporis, expedita quam hic eos repudiandae unde architecto consequatur ducimus odit. Itaque, iste, id, ut laudantium quo reprehenderit ab labore quod laborum consequatur non est. Dolor, culpa.</div>
        <div>Delectus, totam, cum doloremque ad nostrum veniam aliquam non voluptatum eligendi similique saepe laborum sed nesciunt sint voluptate at placeat officia ducimus vel aliquid unde accusamus eius! Itaque, amet, repellat.</div>
        <div>Autem, alias assumenda nihil dignissimos dicta magnam voluptates neque cum eveniet non. Voluptate, quasi, tempora soluta ipsam recusandae qui ab excepturi esse possimus totam corporis quae magnam cum fugit nihil.</div>
    </div>


    </div>


/***** css  *****/

body {
    margin: 0;
    padding: 0;
    position:absolute;
    width: 100%;
    height: 100%;

}
#Main_div{
height:100%;
width:100%;

}
header, footer {
    width: 100%;
    height: 20%;       
    background: red;
}

#content {
   width:100%;
    height:100%;        
    background: silver;

}

nav {
    width: 30%;        
   /*override*/  
    height: 100%;
    float: left;
    background: green;

}

#inside {
margin-left:32%;    
width: 65%;
    height: 100%;        
    overflow: auto;
}

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

Upon hitting submit, the form remains unresponsive

I have been developing a permissions system for my NodeJS project (Using the SailsJS MVC) and encountered an issue. After resolving my initial error as detailed here, I am now facing a problem where the form is unresponsive. It neither shows an error in th ...

What is the most effective method for showcasing HTML content in a recyclerView?

Within the recyclerView, I showcase comments that are stored in HTML format. My attempt to display the comments in a webView was successful, allowing me to use custom *.css, but it caused significant lag in the recyclerView when scrolling. Additionally, m ...

Exploring the power of Vue element manipulation

I'm diving into the world of web development and starting my journey with Vue on an online learning platform. Check out the code snippet below: <div id="app"> <form @submit.prevent="onSubmit"> <input v-model="userName"&g ...

What is the best way to align my two buttons in the center?

I'm having trouble aligning my two buttons to the center. Additionally, I'm not sure how to remove the gray color from inside the buttons. Here is the code: index.html <!DOCTYPE html> <html> <head> <met ...

Ensure that two elements containing content of varying width are equal in width

I am facing a challenge with a container that contains two child elements (previous and next links). These links have labels of different lengths, but I need them both to have the same width on the page. Ideally, each link should be as wide as the widest e ...

Turning off the ability to horizontally scroll in an iframe using touch controls

I am trying to disable horizontal scrolling in an iframe on my website, particularly when a user uses touch input and drags horizontally. The scroll bars can still be visible and draggable when touched directly. Unfortunately, I do not have control over th ...

What is the best way to transfer information to a different NextJS page?

I want to implement a search input field. The idea is to allow the user to type something into the search bar and then send that input to another page where an API request will be made with the search content. Essentially, when the user types "something" ...

What's the best way to use a single tag with a class to replace multiple nested blockquote tags

I have a collection of messy HTML files filled with repeated blockquote tags used to showcase lines of poetry. For example: <blockquote><blockquote>roses are red</blockquote></blockquote><br> <blockquote><b ...

Looking for an xpath to target elements within a neighboring table

I am having trouble creating an xpath expression to find the text located in the span tag that contains text to locate in the second portion of the xpath. The text should be found by first locating an image inside a table row within a table data cell in a ...

Choosing one item from an array to showcase in a view [Angular]

I've previously inquired about this issue without success. My current challenge involves a store app created with AngularJS. I am trying to implement a feature where clicking on an item will open it in a separate "item" view, displaying only the info ...

Is it possible to include a border/stroke on a Raphael picture?

Currently, I am attempting to enhance a Raphael image element by adding either a border, stroke, or drop shadow. My end goal is to create an animated glowing border effect. Previously, I successfully implemented this on traditional HTML elements using Java ...

Easily automate button clicks on a new tab website

Seeking assistance below, following codes are sourced from "example.com" as assumed: <a href="http://www.example.org" target="vo1" onclick="gp(1)" rel="nofollow">Click Me</a> Upon clicking on ...

Issue with plain CSS animation not functioning in NextJS with Tailwind CSS

I found this amazing animation that I'm trying to implement from this link. After moving some of the animation code to Tailwind for a React Component, I noticed that it works perfectly in Storybook but fails to animate when running in next dev. It si ...

The NextJS Link component does not seem to be receiving the styles from Tailwindcss

I am currently working on a nextjs frontend project that has tailwindcss integrated. Below is an example of the code I am using: import Link from 'next/link' return( <div> <Link className="text-sm hover:text-gray-600" ...

How can I trigger the second animation for an object that is constantly moving within a specific range in the first animation?

I am looking to create a simulation of rising and falling sea levels. Currently, when I click the button, the level rises from its starting point to the top, but I am unsure how to achieve this effect in my code. Below is the code I have written so far, an ...

What could be preventing certain child elements from rendering in the browser when using scss/ compiled css?

I currently work with the Visual Studio Code editor and execute "npm run sass" in the bash terminal. Whenever I modify the scss file, the terminal displays the following message: => changed: C:\Users\kenne\modern_portfolio\scss&bso ...

Adjust the font size and center alignment in the navigation bar using Bootstrap

I'm a beginner when it comes to using bootstrap and I'm having trouble adjusting the font size of my application's title. While I was able to change the color of the text, I can't seem to find where the font-size property is located for ...

Tips for creating a responsive graphic using Bootstrap CSS

I am currently trying to determine how to create individual graphics that can be directly loaded via CSS and made responsive. When I resize the screen, the graphics also adjust accordingly. However, on smaller displays such as mobile phones, the graphics ...

What strategies can I use to include multiple spans within div elements without causing the code to become overly lengthy?

I'm working with 20 spans representing movie genres, each accompanied by another span that "closes" the genre when selected. The closing span has an .active class, similar to this example: My version currently looks like this: To achieve this, I hav ...

What is the correct placement of DESC in a MySQL query when using PHP?

Here's the issue I'm facing: $query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage"; I've been attempting to sort the results in descending order (latest on top) by adding DESC to the query. However, every t ...