Tips for containing `overflow` within a flexbox container

I've organized my body space into three sections: header, content, and footer. To achieve a sticky footer effect at the bottom, I used the flex property in my layout. However, I'm struggling to add a scrollbar to my main container box.

I've attempted it without success. Here is the code:

Header goes here!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde deserunt et, quibusdam quidem dolores minus cupiditate veniam laborum, placeat, maiores illum voluptas, at ipsam voluptates! Alias enim, reiciendis debitis commodi.
Quasi, exercitationem, at! Voluptas, similique corrupti reprehenderit odio fuga omnis cum dicta, fugiat vero, eveniet neque! Illo architecto aut ullam provident, aliquid voluptatum voluptates, consequuntur similique, soluta sunt voluptatem blanditiis!
A animi dignissimos ipsam, minus et odit ea. Dignissimos nesciunt, numquam eius expedita quas quibusdam vitae ea unde molestiae aperiam labore adipisci officia veritatis iste, omnis odio ipsam soluta doloremque?
Eveniet, explicabo, commodi? Accusantium cum hic doloremque ullam, eos explicabo commodi voluptatibus animi nesciunt dicta, nam fuga iure sapiente cupiditate nihil corrupti nemo inventore vel at laboriosam, ea praesentium id.
Ullam vel, sequi dolor distinctio repellendus ipsa assumenda in, fuga ab vitae iusto vero? Facilis quos quam, provident quia, a voluptatum nulla reiciendis nostrum, repellat repudiandae necessitatibus consequuntur odit praesentium.
... (content continues) ...
Magni beatae quod mollitia cupiditate reprehenderit ratione aperiam quae, quasi animi esse neque laborum voluptatibus doloribus error consequuntur autem consectetur illum architecto, similique minima vitae cumque? Consequuntur quos, eveniet minus!
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam ducimus error, ut optio alias? Voluptas, explicabo. Exercitationem temporibus earum perferendis repellendus, tenetur corporis, iste cumque tempora odit atque porro modi?
Debitis sit sint tempore earum quasi impedit est neque vitae, deleniti dolorem, mollitia totam laborum! Praesentium officiis dolorum quibusdam impedit voluptate voluptatibus, vero quas mollitia, ex labore amet aperiam, consectetur.
Voluptas ducimus reprehenderit quae id deleniti rem fuga nostrum alias, tempore nemo ullam aspernatur autem distinctio dicta unde odio dolor dolores amet consequatur nisi quam, consectetur facilis. Ducimus, facilis, laudantium.
div.content{
            background: yellow;
            height: 100%;
            min-height: 100%;
            overflow-y: auto; //not working!!
        }

Answer №1

Great job! You're on the right track, but there are some ways to enhance your HTML structure. Consider removing unnecessary section tags and using height:100vh to ensure full-page coverage for scrollable content. See below:

Expand to full page for optimal viewing

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body { /* The body acts as the main container in this example */
  display: flex;
  background: gray;
  border: 5px solid green;
  flex-direction: column;
  height: 100vh;
}

header {
  background: lightgreen;
}

h1 {
  padding: 2rem;
}

section.content {
  background: yellow;
  flex: 1;
  overflow-y: auto;
}
<header>
  <h1>Header will go here!</h1>
</header>
<section class="content">
  <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde deserunt et, quibusdam quidem dolores minus cupiditate veniam laborum, placeat, maiores illum voluptas, at ipsam voluptates! Alias enim, reiciendis debitis commodi.</div>
  <div>Quasi, exercitationem, at! Voluptas, similique corrupti reprehenderit odio fuga omnis cum dicta, fugiat vero, eveniet neque! Illo architecto aut ullam provident, aliquid voluptatum voluptates, consequuntur similique, soluta sunt voluptatem blanditiis!</div>

... (trimmed for brevity)

  <div>Distinctio rem ab hic quo dolor aliquam, dolorum neque, saepe incidunt unde odit earum maxime officiis consequuntur quos natus commodi. Illo molestiae quo ipsa voluptas odit. Sunt incidunt soluta omnis!</div>
  <div>Sit fuga maxime pariatur praesentium illo ducimus facere veniam molestias in illum doloribus ab quibusdam ipsa iusto sint officia, impedit, quae omnis, molestiae perspiciatis repellat beatae. Eaque quam iusto fuga.</div>
  <div>Voluptatibus unde blanditiis veritatis, ipsa libero asperiores accusamus impedit laudantium doloremque. Reprehenderit similique, mollitia quam repudiandae libero totam doloribus sapiente molestiae officiis. Similique culpa, labore odit quae minus dignissimos,
    distinctio.</div>
  <div>Voluptatibus unde blanditiis veritatis, ipsa libero asperiores accusamus impedit laudantium doloremque. Reprehenderit similique, mollitia quam repudiandae libero totam doloribus sapiente molestiae officiis. Similique culpa, labore odit quae minus dignissimos,
    distinctio.</div>
</section>
<footer>
  <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam ducimus error, ut optio alias? Voluptas, explicabo. Exercitationem temporibus earum perferendis repellendus, tenetur corporis, iste cumque tempora odit atque porro modi?</div>
  <div>Debitis sit sint tempore earum quasi impedit est neque vitae, deleniti dolorem, mollitia totam laborum! .</div>
</footer>

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

Unlocking the power of setting dynamic meta content

When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error: SyntaxError: Unexpected identifier in /home/runner/superstrap/ ...

Creating Custom Styles with Polymer for <content> Elements

Seeking help with styling using the ::content selector and custom CSS properties. Any insight would be appreciated! To simplify, I have a Polymer component utilizing a content tag that will always contain a paper-input: <template> <style> ...

Step-by-step guide to implementing a sticky header while scrolling

How can I implement a fixed header on scroll, like the one seen on this website: www.avauntmagazine.com Here is the HTML for my header: <div class="bloc bgc-wild-blue-yonder l-bloc " id="bloc-1"> <div class="container bloc-sm"> &l ...

What is the best method to transfer and incorporate essays and information onto my HTML page?

Here are some unique events and observances for the month of December: DECEMBER 1 World AIDS Day National Pie Day National Eat a Red Apple Day Bifocals at the Monitor Liberation Day Day With(out) Art Day Rosa Parks Day DECEMBER 2 International Day for th ...

Rapid HTML coding dilemma

Currently, I am in the process of quickly marking up my webpage. One issue I am facing is that I would like my title to be displayed below my <nav> tag instead of beside it. Can anyone provide guidance on how to achieve this? Here is the current HT ...

Enhancing React Performance: Storing Component Identity in Redux State

Can I safely pass this to a Redux action creator from a component defined using React.createClass? In my code, I have created the following reducer: const unsavedChangesProtectionReducer = handleActions({ [ENABLE_UNSAVED_CHANGES_PROTECTION]: (unsaved ...

Concealing a button once the collapse feature is toggled in Bootstrap

The following code snippet from the bootstrap website demonstrates how to use collapse functionality: <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> Link with href & ...

The event is being triggered on two separate occasions

Hey there! I'm trying to bind the onclick event to both a parent and child element using the same method. However, I'm running into an issue where the event is being fired twice. Any suggestions on how to prevent this from happening? <div id= ...

Incorporate Multiple Choice Queries into your PHP Online Form

My PHP web form consists of text fields, dropdowns, and radio buttons. When submitted, the information is sent to me via email. I am trying to implement a multiple choice question with checkboxes. Although I can create the form field, I'm struggling t ...

Menu collapse and expand feature similar to that of Gmail

I am attempting to replicate the menu-collapse style found in the new Gmail interface. When a menu exceeds a certain height, I want it to collapse to a predetermined height. Upon hovering over the exposed menu area, I want the menu to expand back to its or ...

How can I populate a select dropdown list in Django using JsonResponse?

I've been working on displaying a list from my Django application in an HTML select option, but I'm having trouble getting it to show up. Also, I believe I need to assign IDs to the option values in the select list, but I'm not sure where to ...

Leveraging Forms for Entering Google Maps Information

Recently, I've been working on an app that aims to generate a custom map based on user input from a form. If you'd like to test the functionality yourself, head over to this page. After filling out all required fields and hitting "Submit", the g ...

Showing a 2D array in HTML using ngFor loop

I need to display a list of arrays in an HTML p-table. Here is the array list: [Array(2), Array(2), Array(2), Array(2), Array(1)] 0: (2) ["abc", "def"] 1: (2) ["ghi", "jkl"] 2: (2) ["mno", "pqr"] ...

"Interactive" - Utilizing javascript and html5 to create engaging game animations

Imagine I have a base class that looks like this: function Tile(obj) { //lots of default variables such as colors and opacity here } Tile.prototype.Draw = function () { ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," ...

Learn how to utilize AngularJS Material to toggle the enable/disable functionality of a checkbox

Can someone assist me in enabling and disabling the checkbox as shown in the attachment image https://i.stack.imgur.com/l6g1C.jpg View the PLNKR angular.module('BlankApp', ['ngMaterial']) .config(['$mdThemingProvider', fun ...

auto-scrolling webpage as elements come into view

Here is some jQuery code I have: $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); $(".third").delay(1800).fadeIn(1000); $(".fourth").dela ...

Reading settings from web.config file in an HTML page

Currently working with ASP.NET 3.5 web forms. I've recently saved the path of my website in the web.config file under the key "CommonPath". I have a basic static HTML page that I would like to utilize this key on. Any suggestions on how to achieve ...

What is the best way to display a scrollbar at the bottom of the table while setting the table height to

After writing the css provided below, I noticed that it works fine on my laptop screen. However, when viewing it on a larger monitor, there is empty space at the end of the table. .wrapper{ width:auto; height: 500px; overflow-x: scroll; ...

Create animations for ng-repeat elements without using ng-animate

Can the transition for changing the order of the model array in ng-repeat be animated using only CSS, without using ng-animate? ...

Input form with multiple fields. Automatically display or hide labels based on whether each field is populated or empty

I am attempting to create a form where the placeholders move to the top of the input when it is focused or filled. However, I have encountered an issue with having multiple inputs on the same page. Currently, my JavaScript code affects all inputs on the pa ...