prevent the container from occupying any additional available space

I'm currently working on a project that aims to replicate the functionality demonstrated in this code snippet:

.overall-container {
  height: screen;
  border: 1px solid black;
  padding: 2px;
}

.scroll-menu {
  overflow-y: auto;
  border: 1px solid blue;
  padding: 2px;
  height: 100px; // this should be the rest of the screen height
}
<div class="overall-container">
  <div>
    Header
  </div>
  <div class="scroll-menu">
    <div>Element 1</div>
    <div>Element 2</div>
    <div>Element 3</div>
    <div>Element 4</div>
    <div>Element 5</div>
    <div>Element 6</div>
    <div>Element 7</div>
    <div>Element 8</div>
    <div>Element 9</div>
    <div>Element 10</div>
    <div>Element 11</div>
    <div>Element 12</div>
    <div>Element 1</div>
    <div>Element 2</div>
    <div>Element 3</div>
    <div>Element 4</div>
    <div>Element 5</div>
    <div>Element 6</div>
    <div>Element 7</div>
    <div>Element 8</div>
    <div>Element 9</div>
    <div>Element 10</div>
    <div>Element 11</div>
    <div>Element 12</div>
    <div>Element 1</div>
    <div>Element 2</div>
    <div>Element 3</div>
    <div>Element 4</div>
    <div>Element 5</div>
    <div>Element 6</div>

  </div>
</div>

The main issue I'm facing is: I want the scroll-menu to act as a container for scrolling, disabling scrolling for the overall-container. The header should always remain visible in this setup. This requires limiting the height of the scroll-menu, but I'm uncertain about how to achieve this.

Answer №1

To solve this, utilize the power of Flexbox. Simply define the parent container overall-container with the property display: flex; height: 100vh;, and set the child container scroll-menu to occupy all available space by using flex-grow: 1 or its shorthand flex: 1.

body, html { margin: 0; padding: 0 } /* Eliminate default margin and padding */

.overall-container {
  height: screen;
  border: 1px solid black;
  padding: 2px;

  /* Apply these CSS properties */
  display: flex;
  flex-direction: column;
  height: 97vh;
}

.scroll-menu {
  overflow-y: auto;
  border: 1px solid blue;
  padding: 2px;

  flex: 1; /* Ensure it takes up available space */
}
<div class="overall-container">
  <div>
    Header
  </div>
  <div class="scroll-menu">
    <div>Element 1</div>
    <div>Element 2</div>
    <div>Element 3</div>
    <div>Element 4</div>
    <div>Element 5</div>
    <div>Element 6</div>
    <div>Element 7</div>
    <div>Element 8</div>
    <div>Element 9</div>
    <div>Element 10</div>
    <div>Element 11</div>
    <div>Element 12</div>
    
    <!-- Continue adding elements as needed -->
  </div>
</div>

Answer №2

Encountering an issue with the height: screen; property, as the value screen is unrecognized in Chrome Devtools Style editor. Despite intending to use -webkit-fill-available, it's non-standard and experimental, yielding suboptimal results. Avoid this method for now and await potential enhancements.

Instead, consider setting the container height like so:

height: 100vh;
  

This will match the height to 100% of the browser screen height or less.

For the div containing the header, prioritize using h[1-6] tags or other semantic heading elements over a div if feasible. However, retaining the div remains acceptable.

You can also utilize CSS as follows:

height: calc(100vh - 25px);
  

Using the calc() function in CSS helps calculate viewport height (100vh) and subtracts 25px accurately based on element heights.

If designing a menu, contemplate exploring details/summary HTML elements as an alternative for unique style presentation. Another technique involves making the header sticky to prevent scrolling along with content, eliminating the necessity for a second div:

Notice the deliberate red background color for the header CSS to distinguish it from other content visually. This distinction showcases the fixed nature of the header amidst scrollable elements. Merging the header with the container's orange color would blur this differentiation.

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 function TagBuilder.MergeAttributes is malfunctioning

My attempt at creating a custom helper in MVC is not working as expected. The custom attributes are not being added to the HTML: Custom Helper Function public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, strin ...

Is there a way to use HTML alone to center a navigation link without using CSS?

Can I center these navigation links using only HTML without needing CSS? The navigation links are currently aligned to the left and I would like to center them on the webpage. <ul class="nav nav-tabs" id="myTab" role="tablist&q ...

How to successfully implement ng-show with Html5's <dialog> tags

It's incredible how simple Html5 dialogs are. It feels like they should have been here 15 years ago! I'm having trouble getting ng-show to work with dialogs. Any tips on how to make it happen? <!DOCTYPE html> <html ng-app="project"> ...

The back-to-top feature is malfunctioning in the new Bootstrap 4 update

I've been working on adding a back-to-top button to my website using JavaScript in Bootstrap 4 with the Font Awesome icon set. It was functioning perfectly until I made some changes to the site, and now it's not working anymore. I'm pretty n ...

Contact Form Appears to Be Functioning Properly, Yet Emails Are Not Being Received (HTML5 & PHP)

Hello everyone, I need some assistance with creating a contact form. This is my first time working on this, and I am facing an issue. I am using HTML5, which I have some knowledge of, and PHP, which I am not very familiar with. When I try to test the form ...

How to open a PDF file in a new tab using Angular

Within my Angular 12 application, I am seeking to enable users to "view" a PDF file stored locally in the application within a new tab on Google Chrome. Currently, when implementing the code below in HTML, the file downloads rather than opening in a new ta ...

Having trouble with Javascript not detecting when it's empty?

Recently, I have been attempting to modify the color of a submit button when a form is empty. As a beginner in this area, I am somewhat puzzled as to what mistake I might be making. I will share the current code with you in hopes of receiving some guidance ...

Form submission caused by automatic loading of the page results in an endless loop

https://i.sstatic.net/cg5b3.jpgi'm currently implementing a Filter with the use of a Form. The desired functionality is that when a user lands on the list page, the form should automatically submit so that the user can view only the values they want t ...

Scrape dynamic web data with JSOUP

Having trouble grabbing the price? I cannot seem to get any output for the price and its weight. I've attempted different methods, but nothing is working Document doc = Jsoup.connect("https://www.jakmall.com/tokocamzone/mi-travel-charger-20a-output- ...

Using the flexslider to override CSS styles in an <li> element

I have implemented the flexslider by whoothemes on my responsive website to showcase tiles. However, I am facing an issue with setting a specific width for the tiles on various devices. Upon inspecting with Chrome, I see this code: <li style="width: 21 ...

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 ...

Is it possible to change the hover highlight rotation on a link without affecting the surrounding elements?

Is it possible to rotate the highlight on a link when hovered? I'm new at this, so apologies if this question seems basic. This is how my css/html is currently structured: .links { display: block; } .links a { color: #000000; text-decoratio ...

Trigger a notification based on the selected choice

Here is some sample HTML code: <div id="hiddenDiv" style="display: none;"> <h1 id="welcomehowareyou">How are you feeling today?</h1> <select name="mood" id="mood"> <option value="" disabled selected>How are you feeling?</o ...

`Fetching HTML content using a web browser``

Seeking a Linux browser, possibly console-based, with the capability to read an HTML page from standard input. For instance, I am interested in performing the following command: generate_html | browser Appreciate any suggestions! ...

Leveraging JavaScript for Validating Radio Buttons

I've been following a tutorial guide on this specific website , but I'm encountering some difficulties despite following the exact steps outlined. Can someone provide guidance? Here is what I have done: <html> <script> fu ...

Align the central element in the Bootstrap menu between two other elements

My menu layout looks like this : .navbar { box-shadow: 0 5px 15px rgba(0, 0, 0, .15); background: #fff; border: 0; max-height: 73px } .navbar-center>span>a { display: inline-block; position: relative; } #header>.navbar>div:nth-child(2)> ...

What is the best way to ensure that my <h1> header stays above my <h2> header at all times?

As a novice in web design, I recently took on the challenge of creating my own website. I am a graphic designer and came up with a mock-up of how I envision the site to look. While I managed to complete most parts of it, there's one specific area wher ...

Exploring HTML segments with Python and BeautifulSoup

As a beginner, I am exploring web scraping examples from Automate the Boring Stuff. My goal is to create a Python script that automates downloading images from PHD Comics using the following steps: Identify the image link in the HTML and download it. ...

Having trouble getting my JavaScript code to function properly on Firefox browser

I have created a script where the cursor automatically moves to the next form field once it reaches its maximum length, in this case 1. Here is the JavaScript code: window.onload=function(){ var container = document.getElementsByClassName("container")[0] ...

Does a specific HTML element exist that is unable to have JavaScript within its content?

Let me pose a question, and while the answer may be "no", it never hurts to inquire... I am in need of taking in formatted text as HTML markup and later displaying the HTML to showcase the formatted text. This is an extremely common situation. Even popula ...