Incorporating a flexible header size and scrollable content: Tips for avoiding the need to scroll through the entire page to find

I am currently working on creating a unique page template that has certain requirements:

  • The height of the header may vary
  • The content and aside containers should be independently scrollable
  • Anchor links should not disrupt the page layout

To see what I have implemented so far, you can check out the showcase here.

const template = document.getElementById('tmplt');
const header = document.getElementById('header');
const button = document.getElementById('btn');

const toggleTemplate = () => {
  header.children.length > 1 ? header.children[1].remove() : header.appendChild(template.content.cloneNode(true));
}
button.addEventListener('click', () => {
  toggleTemplate();
});
body {
  padding: 0;
  margin: 0;
}

#page {
  max-height: 100vh;
  height: 100vh;
  overflow: hidden;
}

.container-flex {
  display: flex;
  flex-direction: row;
  overflow: hidden;
  height: 100%;
}

aside {
  flex: 1 1 20%;
  background-color: deeppink;
}

main {
  flex: 1 1 80%;
  background-color: cadetblue;
  height: 100%;
  overflow-y: auto;
}
<div id="page">
  <header id="header">
    <h1>Title</h1>
  </header>

  <div class="container-flex">
    <aside>
      <p>Here could be some information</p>
      <a href="#anchor">This is an Anchor-Link</a> to the Input Element on the bottom of the page
    </aside>
    <main>
      <h2>Here shall be the main stuff</h2>
      Clicking this <button id="btn">button</button> will make the header bigger / smaller by adding/removing elements
      <p>Here can also be some kind of a very looooong text</p>
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo ...
      ...
      et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta ...
      <p> and on the end of the page there will be an element to be focused
        <p>
          <input id="anchor" />
    </main>
  </div>
</div>

<template id="tmplt">
  <blockquote cite="https://www.huxley.net/bnw/four.html">
        <p>Words can be like X-rays, if you use them properly—they’ll go through anything. You read and you’re pierced.</p>
    </blockquote>
    <figcaption>—Aldous Huxley, <cite>Brave New World</cite></figcaption>
</template>

Is it possible to achieve my requirements using only CSS? Any suggestions on how to do this?

Here's one way to solve it:

  • Set the height using JavaScript after changing the content of the header

Answer №1

Why bother setting the height with JavaScript? Let flexbox handle it, adjusting automatically based on content.

  • Add
    display: flex; flex-flow: column nowrap;
    to #page
  • Add header { flex: 0 0 auto; }
  • Replace .container-flex's height: 100%; with flex: 1 1 0;

Here's the updated code:

const template = document.getElementById('tmplt');
const header = document.getElementById('header');
const button = document.getElementById('btn');

const toggleTemplate = () => {
  header.children.length > 1 ? header.children[1].remove() : header.appendChild(template.content.cloneNode(true));
}
button.addEventListener('click', () => {
  toggleTemplate();
});
body {
  padding: 0;
  margin: 0;
}

#page {
  display: flex;
  flex-flow: column nowrap;
  max-height: 100vh;
  height: 100vh;
  overflow: hidden;
}

header {
  flex: 0 0 auto;
}

.container-flex {
  display: flex;
  flex-direction: row;
  overflow: hidden;
  flex: 1 1 0;
}

aside {
  flex: 1 1 20%;
  background-color: deeppink;
}

main {
  flex: 1 1 80%;
  background-color: cadetblue;
  height: 100%;
  overflow-y: auto;
}
<div id="page">
  <header id="header">
    <h1>Title</h1>
  </header>

  <div class="container-flex">
    <aside>
      <p>Here could be some information</p>
      <a href="#anchor">This is an Anchor-Link</a> to the Input Element on the bottom of the page
    </aside>
    <main>
      <h2>Here shall be the main stuff</h2>
      Clicking this <button id="btn">button</button> will make the header bigger / smaller by adding/removing elements
      <p>Here can also be some kind of a very looooong text</p>
      ... (Long snippet continued)
    </main>
  </div>
</div>

<template id="tmplt">
  <blockquote cite="https://www.huxley.net/bnw/four.html">
        <p>Words can be like X-rays, if you use them properly—they’ll go through anything. You read and you’re pierced.</p>
    </blockquote>
    <figcaption>—Aldous Huxley, <cite>Brave New World</cite></figcaption>
</template>

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

Is there a way to retrieve the selected value from a dropdown menu using vue.js?

I have a parent Vue component structured like this: <template> <form> <div class="row"> <div class="col-md-4"> <form-select id="color" name="color" :data="color">Color</form-select&g ...

The background failed to display (potentially due to a hovering function)

I encountered an issue with a div that has a background image. Upon loading the page, the background image fails to display initially. However, when I hover over the div and then move my mouse elsewhere (due to a specific function described below), the bac ...

Middleware that is commonly used by both error handling and regular request-response chains

I am currently working on implementing a middleware that can set a variable in the sequence of both error and non-error scenarios. Is there a way to achieve this without disrupting the normal flow of middleware? Specifically, when I include the err param ...

What steps can I take to ensure that an image does not exceed the size of its parent tag?

Is there a way to ensure that items placed in a container do not exceed the boundaries of the container? I am trying to use my logo as the home button, but when I set the image height and width to 100%, it becomes larger than the nav bar. The CSS for the n ...

Is it possible to efficiently iterate through map keys in axios using Vue.js?

Currently, I am working on Vue.js exercises that I have created. I am successfully populating a table with data from a JSON API, but I have encountered a challenge. The API contains multiple keys with similar names (strIngredient1, strIngredient2, strIngre ...

Transforming all commas to plus signs in a JavaScript codebase for the entirety of

Currently, I am using winston for logging and have created a common method to log throughout the project. However, I am facing an issue with many logging statements that look like this: logger.info("here is the data" , data) The problem arises when trying ...

Refresh Angular 2 data after a related data update

I am facing an issue with my <select> elements in Angular. One for the Districts and another for the Cities. The districts are fetched using the getDistricts() function within the ngOnInit() function without any problems. However, I am unsure how to ...

Setting the flex-direction for <td> elements: A helpful guide

This is a snippet from the render function of one of my components, showcasing a table: return ( ... <td> <div style={{ width: '100%' }}>50</div> {' '} <span clas ...

Inquiring about the Model component within the MVC architecture in a web application created with NodeJs and integrated with

As a beginner in NodeJs, I am venturing into creating web applications using the express framework and MySQL. Understanding that in MVC architecture, views are represented by *.ejs files, controllers handle logic, and models interact with the database. Ho ...

Is there a way to prevent my token from being exposed when making an AJAX call?

When working on my HTML file, I encountered an issue with a Python Django URL integration where I need to pass a token to retrieve certain information. However, the problem is that my token is exposed when inspecting the page source in the HTML document. ...

Combining a Python backend with an HTML/CSS/JS user interface for desktop applications: the perfect synergy?

Is it possible to seamlessly combine Python code with HTML/CSS/JS to develop desktop applications? For instance, I want to create a function in Python that displays "Hello World!" and design a visually appealing user interface using HTML/CSS/JS. How can I ...

The jQuery click event is failing on the second attempt

My PHP code dynamically generates a list, and I want to be able to delete rows by clicking on them. The code works fine the first time, but not the second time. HTML <li> <div class="pers-left-container"> <img src="<?php ech ...

Is there a way to add a scrollbar to the ChartJS tooltip to avoid data

I'm currently working on a chartJS project where I have a callback function to display data in tooltips. However, as the data increases, the overflow section of the tooltip gets hidden from the canvas. Is there a solution to prevent this overflow and ...

Iterate through an array index within a map function in a ReactJS component

I am working with a map that contains images of metros, and I need to increment the number by 1 at each loop iteration. For example, the first loop will display {metrosImages[0]}, then {metrosImages[1]}, and so on until the loop reaches its end. The code ...

Creating a single loop in Javascript to populate two dropdown menus with options

Is there a way to populate two dropdown menus in JavaScript with numbers using the same for loop? Currently, only one is being populated, specifically the last one. for (var i=1; i<10; i++) { var option = document.createElement("option"); option. ...

Postback fails to trigger following JavaScript onchange event

Within my asp.NET application, I have incorporated a control that validates form input data using server-side logic. The concept is simple - drag the control to the desired location, configure it in the code behind, and watch as the form gets validated. ...

Is there a way to retrieve the properties of another function within the same component?

I am trying to place NumberFormat inside OutlinedInput and I also need different properties for the format of NumberFormat. (There will be a select window that defines which format property to use). This is what I have: import OutlinedInput from "@ma ...

What is the best way to mix up the images in my memory game?

My JavaScript memory game is functioning properly, but I would like the images to load in a random order. Unfortunately, I'm not sure how to accomplish this. Here is the current setup: var easyImages = ["img/bat.jpg", "img/bug.jpg", "img/cat.jpg", " ...

Is there a regular expression that can identify whether a string is included in a numbered list?

Struggling with creating a regular expression to determine if a string is part of a numbered list like those in word processors. Need it to return true only if the string starts with a number, followed by a full stop and a space. Easy for single or doubl ...

Error message received while converting webm video to images using Kagami/ffmpeg.js: Unable to locate an appropriate output format for '%04d.jpg'

These are the current versions: node v12.9.1, npm 6.10.2, [email protected] Repository: https://github.com/Kagami/ffmpeg.js The code in decode.js looks like this: const fs = require('fs'); const ffmpeg = require('ffmpeg.js'); c ...