Expanding scrollable row implemented with CSS grid

How can I create a CSS grid with a single column and two rows?

I want the first row to expand when there is space and to scroll when there is no space, while keeping the second row at the bottom of the div.

I attempted using display: flex; in the first row, but it did not work as expected.

Here is my current HTML setup:

<div class="parent">
  <span class="top">my<br>long<br>long<br>long<br>content</span>
  <span class="bottom">always visible</span>
</div>

And here is the CSS I am using:

body {
  overflow-y: hidden;
}

.parent {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 1fr 0fr;
}

.top {
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
}

span {
  border: solid; 
}

You can view a demo of the code here.

However, I am facing issues with the first row not scrolling when needed and not expanding when there is space.

Answer №1

Personally, I prefer to utilize the flex model for this type of functionality even though grid can also achieve the same outcome ;)

html * {
  box-sizing:border-box;
  padding:0.25em;
  margin:0;
}
body {
  height:100vh;
}
.parent {
  display: flex;
  flex-direction: column;
  max-height: 100%;
  overflow: hidden;
}
.top {
  flex-grow: 1;
  overflow: auto;

  /* style aside */
  background:lightblue;
  margin:2px;/* or 0 */
  border:solid;
}
.bottom {
  flex-shrink: 0;

  /* style aside */
  background:tomato;
  margin:2px;/* or 0 */
  border:solid;
}
 /* demo purpose */
.top .demo {
  display: block;
  max-height: 0;
  overflow: hidden;
  transition: 1.5s;
}
.top:hover .demo {
  max-height: 300vh;
}
<div class="parent">
  <span class="top"><b>Hover me to show my long content</b>
  <span class="demo"><br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content<br>long<br>long<br>long<br>content</span>
  </span>
  <span class="bottom">always visible</span>
</div>

Answer №2

Your code does not have any triggers for an overflow condition.

To make content overflow, it must surpass a width or height limit (for example, height: 300px), which will then activate the scrollbar.

According to MDN:

In order for overflow to take effect, the block-level container must have either a fixed height (height or max-height) or white-space set to nowrap.

In essence, without a set height, you won't see the vertical scrollbar... in Chrome! Interestingly, in Firefox and Edge, the rule mentioned in MDN doesn't apply, and your layout functions correctly.

.parent {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 1fr 0fr;
  grid-gap: 2px;
  background-color: black;
  height: 100vh;
}

.top {
  overflow-y: auto;
}

span {
  background-color: white;
}

body {
  margin: 0;
}
<div class="parent">
  <span class="top">my<br>long<br>long<br>... [truncated for brevity] ...<br>long<br>long<br>content</span>
  <span class="bottom">always visible</span>
</div>

jsFiddle demo


Note on browser rendering differences: It's unclear why Firefox and Edge show a scrollbar on a block-level container without a set height or max-height, contrary to what MDN suggests. They might be making an intervention or interpreting the specification differently than the MDN contributors.

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

Assigning the "action" attribute of a form to direct to the homepage

Working on a login form, I've encountered an issue with the action attribute when set to "index.cfm". This seems to work fine for all other pages except for the index page. Looking for some insights if anyone else has faced this problem before. I&apos ...

Creating a column heading that appears at the top of each column break with the help of CSS column-count

Is there a way to output a list into x number of columns within a content div without specifying the exact amount of columns in CSS? I also want to include a heading at the top of each column: Firstname Lastname State Firstname Lastname State ...

Can you guide me on setting up a multi-selection option for a field in my Wordpress Theme by Templatic?

My website is http://aisiel.ro, a real estate agency website. Under the slider, there is a search field where we have the option to select an area called "Zona" - Area in English. Currently, users can only choose one area for their search, but I want to ...

How can you use C# code to control the visibility of a table row in HTML?

I am trying to display or hide a table row based on the current month using DateTime.Now.Month in my HTML code, but I can't seem to remember the correct syntax. The code I have tried is not working as expected. Can anyone help me with the correct synt ...

How can we use the nth-of-type selector to target a set of eight elements in a

I am trying to style a group of divs in such a way that every set of eight elements will have the same left positioning. Rather than just styling every eighth element individually, I want to apply the styling to groups of eight elements at once. However, ...

I'm looking for a method to retrieve the value of an option tag and then pass it to a helper within handlebars. Can someone

Currently, I am utilizing express and handlebars in my project. I have a specific view where I aim to display certain information based on the event when a client selects an option from a select tag. However, I have some queries regarding this: Is it fea ...

Would this code effectively disable the right-clicking menu for MathJax?

My current approach involves utilizing the following code snippet: <script type="tet/x-mathjax-config"> MathJax.Hub.Config({ showMathMenu: false }); </script> I intended for this code to disable the right-click menu on my math webs ...

Show the JSON information found on a specific website URL

I need help figuring out how to display a json file on a web address so that Swagger UI can recognize it. When I visit , I see json data and can save it as a .json file. However, I'm struggling to replicate this using html. Here's what I've ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

saving the JSON information for dropdown options within a variable

Greetings everyone I have successfully retrieved the options for my select tag in json format and used this code to populate the select tag: var mySelect = $('#'+select_id) $.each(response, function(key,val){ $('<option/>&apo ...

The Battle of Naming Styles in HTML and CSS: CamelCase versus Underscores

After reading numerous articles discussing the pros and cons of camelCase and Underscore naming conventions, I have always leaned towards camelCase due to its byte-saving nature. However, upon discovering BEM, I must admit that I am now in a state of conf ...

Using inline styles for progress bars in React

Struggling to apply styling to the element labeled as "progress", I've tried multiple options but just can't seem to get it right. Any assistance or clues would be greatly welcomed! Here is the code snippet for the component: constructor(pr ...

Is Your Website Sluggish because of an Excessive Amount of JavaScript on Page

After finally resolving some of the Javascript issues I was facing, I have streamlined my code to utilize just one library now, which is a huge improvement from how chaotic it was before. However, I have noticed a slight delay in the page load time, and I ...

Automatic width table design

I am trying to position the "User ID" label closer to its value by aligning it to the right side, as shown in the picture below. I have attempted using additional colspan's but haven't been successful. The User ID can contain anywhere from 1 to 9 ...

Ensuring the validity of input tags

I encountered an issue with an input tag that has a specific logic: https://codepen.io/ion-ciorba/pen/MWVWpmR In this case, I have a minimum value retrieved from the database (400), and while the logic is sound, the user experience with the component lea ...

Resolve background image alignment issue in Firefox browser for Bootstrap carousel

I am currently utilizing this particular bootstrap template. My goal is to have the carousel background images fixed, however, when I include the following CSS: background-attachment: fixed; like so: <div class="carousel-item active" style=&q ...

What is the best way to initiate a change event using JavaScript?

I am attempting to enable my 4th checkbox automatically when there is a new value in the textbox (myinput). If the textbox is empty, I want to disable it. Currently, you have to tab out before the change event is triggered. Also, how can I check for an e ...

Update the jQuery script tag with new content - rewrite everything

I am facing an issue with jquery. I need to change the link to src only when "document.write" is present. Below is my code: myscript.js document.write("TEST ABCD"); test.html <html> <body> <button id="example">click me</button&g ...

My attempts to connect to the FreeSwitch server and make calls to the SIP client (XLite) using the SIPml5 client have been met with challenges

I am encountering issues with registering to a FreeSwitch server and making calls to a SIP client (XLite) using the SIPml5 SIP client. Below is the HTML5 code I am using: <!DOCTYPE html> <html> <head> <meta content="charset=utf-8"/ ...

ASP.NET user control cannot locate hidden field

In an asp.net usercontrol (.ascx) that contains an HTML hidden field, there are some issues to address. <input id="HiddenField1" type="hidden" runat="server" /> When the user control triggers a HTML pop-up window, users input values which are then ...