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

Displaying JSON data in HTML proves to be a challenge

I have collected JSON data and organized it into two arrays, then displayed it in JSON format. Now I'm trying to showcase this data in HTML using two loops by correlating the IDs of one array with the userIds of another array. {"personaldetails":[{"i ...

Email Embedder - Incorporating HTML source code into email communications

I've encountered an issue where the CDNs (Bootstrap and FontAwesome) do not work when I embed my HTML source code into an email. Despite using inline styling throughout, the receiver still faces this problem. Any insights on why the CDNs are failing t ...

Download personalized HTML design

Situation When exporting to HTML, the resulting code appears as follows: <html> <head> <title></title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <style type="text/css" ...

What is the process for gathering information using selenium from a webpage?

Attempting to retrieve data using Selenium and Python from a website. Below is the code that has been used so far: url = 'https://www.kicker.de/1894559/spieldaten/bayern-muenchen-14/borussia-mgladbach-15' chrome = webdriver.Chrome() select = Sel ...

Prevent web browsers from interpreting media queries

Creating a responsive website has been challenging, especially when it comes to accommodating IE7. I'm considering turning off ALL media queries for just IE7 while maintaining the styling and layout in desktop mode. Is this possible? ...

Access the HTML file from a different directory

I have a directory called "myWebsite". Within this directory, there is a file named "index.html" and another subdirectory named "other". Inside the "other" directory, there are CSS files, JS files, and "page2.ht ...

Target the CSS element with a specific class only if it is not the first child within its parent

In my coding project, I have created a div.container which contains multiple p elements. Some of these p elements have the class p.special. My goal is to select the p.special elements that are not positioned at the very top of the parent container (i.e., n ...

Determine the precise x and y coordinates of a centered element using JQuery

How can I determine the exact left and top positions of an element? The parent container has text-align: center, causing potential confusion when there are multiple elements on the bottom row. For instance, the first one may have a position of 0px instea ...

Included Image Hyperlink with Limited Clickability in HTML

When trying to turn my image into a hyperlink, I enclosed it with the tag. Oddly enough, only the lower portion of the image is responsive as a link. The upper part seems unresponsive. Here's the code snippet I used: <a href="link-here"& ...

Understanding the distinction between .navbar and .navbar a in CSS

Can you explain the distinction between .navbar and .navbar a? .navbar { overflow: hidden; background-color: #333; font-family: Arial; } .navbar a { float: left; font-size: 16px; color: white; ...

Using Javascript or jQuery to Enhance the Appearance of Text on a Website

Is there a way to automatically apply styling to specific phrases on our website by searching for instances of those phrases? For example: <div> This is what you get from <span class="comp">Company Name</span>. We do all kin ...

Exploring jQuery's ability to retrieve CSS values

Is there a way to use jQuery to read a specific CSS value from a class? The situation is as follows: HTML: <div class="box"></div> CSS: .box { margin-top: 100px; } I want to determine if the .box class has a margin-top of 100px in order t ...

Accordion button refuses to compress

After following all the steps in my accordion lesson, I encountered an issue where my button refuses to collapse when clicked. Can anyone provide guidance on how to solve this problem? Adding a class of "show" allows the content to display, but it still d ...

Clicking on the background does not alter the onclick function

Can anyone help me troubleshoot my code? My function load() isn't changing the background of .firstDiv for some reason. I've checked multiple times but I can't seem to spot any errors. function load() { document.getElementsBy ...

Divs are stubbornly resisting the urge to behave like block elements

.useless { float: right; clear: right; border: 1px dashed blue; height: 50px; width: 100%; } div.pretraga { border: 3px groove red; width: 20%; float: right; margin-right: 5%; border-top: 0; display: flex; justify-content: center; height: 250px; <div cl ...

The word-break CSS property is ineffective in this situation

I've been experimenting with the word-break css property, but I just can't seem to get it to work even when using a simple example. Here's my code: React: render() { return ( <h5 className="word-break">A very very long line.... ...

Ways to create a distinct highlight for the selected link in the navigation upon clicking

Similar Inquiry: How can I highlight a link based on the current page? Situation I have been struggling to keep a selected link in my navigation menu highlighted after it has been clicked. Unfortunately, I haven't come across any straightfor ...

Having trouble with the position function in JavaScript?

I'm working on creating a small game, but I've encountered some difficulties at the start. Every time I attempt to obtain the position of track or trackCont, it consistently returns x: 0, y: 0. The DIV doesn't move to the correct position w ...

Receiving an UNDEFINED INDEX error when trying to send data from an HTML form to a

I've created an HTML form that is styled using CSS for weekly work data entry. <form action="wwinsert.php" method="post"> <fieldset> <legend>Weekly Work</legend> <p><label class="lab1" for="StoreNumber">Store:</ ...

Show Api Image

Trying to display an image from an API call in PHP to an HTML img tag with the following code: <div class="col-md-3 col-lg-3 " align="center"><img alt="User Pic" <?php echo 'src="'. $data['response']['avatar'] . & ...