When the height of a sibling element restricts the inner content and scroll bar due to the parent element's height being defined in vh

I'm currently working on creating a sidebar that occupies a fixed percentage of the viewport. Inside this sidebar, I want to have an element that remains fixed at the top while allowing the rest of the content to scroll if it exceeds the height of the sidebar.

In the scenario outlined here, the h1 element stays at the top of its parent container, causing the content inside .inner to be partially cut off by the height of the h1 element. The question is how can we display all the content and enable scrolling within the inner container?

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

body: {
  height: 100vh; 
}

.grid-container {
    display: grid;
    grid-template-columns: 1fr 5fr;
    grid-template-areas: 'left right';
    justify-content: space-around;
    grid-gap: 12px;
    width: 90vw;
    height: 100vh;
    margin: auto;
}

.left {
  grid-area: left;
}

.right {
  gird-area: right
}

.side {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
}

.outer {
  height: 90vh; 
  margin: auto;
  position: relative;
  border: 1px solid blue;
  overflow:hidden;
}

.inner {
  height: 100%;
  overflow: auto;
}

h1 {
  height: 100px;
  background: lightgrey;
}

p {
  height: 100px;
}
<div class="grid-container">
  <div class="left">
    <div class="side">
      <div class="outer">
        <h1>other content</h1>
        <div class="inner">
          <p>1</p>
          <p>2</p>
          <p>3</p>
          <p>4</p>
          <p>5</p>
         </div>
       </div>
    </div>

  </div>
  <div class="right"></div>      
</div>

Answer №1

It is best to avoid setting specific heights on elements whenever you can. This approach usually leads to unforeseen issues down the line. Instead, establish guidelines for your flex layout and allow it to adapt accordingly. When you need to create space within a container, keep it simple by adding it directly to the content inside the box, rather than adjusting the box itself. By separating the responsibilities of layout and content, you simplify the process of updating or swapping out individual pieces of content without having to dive deep into the CSS for your overall layout.

Refer to the CSS provided below for additional insights:

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

body: {
  height: 100vh; 
}

.grid-container {
    display: grid;
    grid-template-columns: 1fr 5fr;
    grid-template-areas: 'left right';
    justify-content: space-around;
    grid-gap: 12px;
    width: 90vw;
    height: 100vh;
    margin: auto;
}

.left {
  grid-area: left;
}

.right {
  gird-area: right
}

.side {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
}

.outer {
  height: 90vh; 
  margin: auto;
  position: relative;
  border: 1px solid blue;
  display: flex; /* <--------------- change */
  flex-direction: column; /* <--------------- add */
}

.inner {
  /* height: 100%; <--------------- remove */
  overflow: auto;
}

h1 {
  flex: 0 0; /* <--------------- change */
  background: lightgrey;
}

p {
  height: 100px;
}
<div class="grid-container">
  <div class="left">
    <div class="side">
      <div class="outer">
        <h1>other content</h1>
        <div class="inner">
          <p>1</p>
          <p>2</p>
          <p>3</p>
          <p>4</p>
          <p>5</p>
         </div>
       </div>
    </div>

  </div>
  <div class="right"></div>      
</div>

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 grid area may not properly adjust based on the width size of the browser

When adjusting the width of your browser, you may notice occasional white space in the bottom right corner. https://i.stack.imgur.com/UFV6R.png https://i.stack.imgur.com/VfhMN.png Is there a way to remove this extra margin? In other words, how can I ens ...

What is the best way to initiate a new animation from the current position?

Here is my custom box: <div class="customBox"></div> It features a unique animation: .customBox{ animation:right 5s; animation-fill-mode:forwards; padding:50px; width:0px; height:0px; display:block; background-color:bla ...

Manipulate component properties using CSS hover in React with Material-UI

I am attempting to modify the noWrap property of a Typography element when hovering over it. I have defined a custom CSS class for the parent element and the hover functionality is working correctly. However, I am unsure how to adjust the noWrap property u ...

Tips for choosing the ancestor's ancestor in CSS

<div id="maincontent"> <div id="content"> <div id="admin"></div> </div> </div> Looking for a CSS rule that will target #maincontent only when #admin is present? I have a background color set in the admi ...

Ensure that the sidebar automatically scrolls to the bottom once the main content has reached the bottom

I am facing an issue with a sticky sidebar that has a fixed height of calc(100vh-90px) and the main content. The sidebar contains dynamic content, which may exceed its defined height, resulting in a scrollbar. On the other hand, the main content is lengthy ...

PHP code failing to save form information into database

I am experiencing difficulties with inserting form data into my database. The connection to the database is successfully established without any errors, but no information is being entered. Below is my code - any assistance would be greatly appreciated. ...

Text that disappears upon clicking on show/hide按钮

Could someone please help me figure out how to prevent the "See more" text from disappearing when clicked in the example below? I want it to stay visible. Thank you! ...

Unable to execute specific php function using ajax

I have created a script that utilizes an ajax request. This script is triggered when a user clicks a button on the index.php page, like so: Risorse.php <form method='post'> Name <input type='text' name='nome&apo ...

Text in various styles is layered on top of an image, ensuring that text in one style does not overlap text in another style

Struggling with placing text over an image? Trying to achieve different text styles but ending up with overlapping lines? Here's a snippet of HTML that works with only one text style: <li style="" class="portfolio-content-CV"> <div class ...

Disable checkboxes upon page initialization

I am working with a form that includes checkboxes. Whenever the page loads, clicking on the checkboxes automatically checks them. However, I am looking for a solution where the checkboxes are disabled or not clickable during the page load process. Once th ...

Incorporating the use of font color in CSS styles and

I am creating a newsletter template using foundation. Within the table, there are 2 <th> elements containing the content "Reservationnumber" and "23425-FF3234". I have multiple instances of these <th>. I want to apply a colored font to them. Wh ...

Exploring the possibilities of ReactJS and the sleek design of

How can I alter the background color of a RaisedButton without having the CSS class name appear in the autogenerated div? Here is the button: <RaisedButton className="access-login" label="Acceder" type="submit"/> This is the specified CSS: .acces ...

Trouble with Bootstrap modal not popping up following the switch to Bootstrap 5 and jQuery 3 on ASP.NET Core platform

In a recent update, I made changes to jQuery and Bootstrap versions in my ASP.NET Core project. After updating jQuery from v2.2.4 to v3.7.1 and Bootstrap from v2.2.2 to v5.0.2, I encountered an issue where modal pop-ups were not triggering when buttons wer ...

Having difficulty arranging the elements in a straight line

I am currently working on designing the footer for my website. My goal is to have 2 divs and one list displayed inline within this footer. However, I am facing an issue where using the CSS Property display:inline-block is not achieving the desired result. ...

Coloring weeks in fullcalendar's two-shift calendar

Can FullCalendar create a calendar with alternating colors for odd and even weeks? Visit the FullCalendar demos here For example, see image below: ...

Unsuccessful conversion of JSON data to HTML using json2html in Python

I have been experimenting with converting JSON data to HTML using json2html. The JSON data structure I am working with is as follows: json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username" ...

What is the best way to showcase a String variable with spaces in the value field of a form within JSP?

When working with a String variable in JSP and trying to display it in a form field, there might be an issue if the string contains spaces. Only the first word is displayed instead of the entire sentence. Below is the code snippet along with the resulting ...

The Bootstrap column class is malfunctioning

Recently, I've been diving into the world of web development. I attempted to create three divs in one row using Bootstrap col class, but unfortunately it didn't turn out as expected. Two divs ended up in a row with some blank space, while the thi ...

Organizing Angular Material Styles using Namespacing

In an attempt to develop reusable components with Angular 1.4.3 and Angular-Material 1.0.5, the goal is to seamlessly integrate these components across various applications. However, a challenge arises as the Angular Material CSS contains styling rules th ...

Populating table with information stored locally

Hello there, I am currently working on a journal project where I am facing an issue with the getItem function of localStorage. Whenever I add entries to the table and refresh the page, all the entries disappear except for one row with default input values ...