Tips for navigating through a div with an unknown height

I am currently working on developing a sleek messaging system and I have encountered an issue with the interface design. My project is built using Materialize, however, I am open to tweaking it with custom CSS if necessary. The layout consists of a list of messages on the left side, while the selected message is displayed in the remaining space on the right. This setup is quite standard.

The problem arises when there are numerous messages in the list or the text within the messages is too lengthy to fit within the viewing height without scrolling.

I strive for a non-scrollable page overall, but I also require the ability to scroll through the message list and view individually.

In my attempts to address this issue, I implemented the following CSS:

.message-view {
    height: 40vh;
    overflow-y: scroll;
}

While this approach somewhat works, it falls short of my desired outcome. What I truly aim for is the .message-view div to dynamically adjust its height to occupy the remaining space. Manually calculating heights based on preceding elements feels cumbersome to me and might not ensure precise heights across all devices.

TLDR: I seek a solution where a div can adapt its height dynamically to facilitate scrolling using either CSS or Materialize. Any suggestions?

Below is an illustration of the current dilemma (the list on the left is scrollable with a suboptimal height set arbitrarily at 80vh, along with the message view lacking scrolling functionality):

html, body {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.content {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: space-between;
}

.message-list-scroller {
  width: 30%;
  height: 80vh;
  overflow-y: scroll;
}

.message-list { 
  width: 98%;
  height: 100%;
  border: 1px solid gray;
}

.message-list-item {
  width: 100%;
  border-bottom: 1px solid gray;
}

.message-view {
  width: 70%;
  height: 100%;
  padding: 10px;
}
<html>

<body>

<h1>Message page</h1>

<div class="content">

  <div class="message-list-scroller">
  <div class="message-list">
    <div class="message-list-item">
      <h3>Title 1</h3>
      <p>Preview of the message</p>
    </div>
    <div class="message-list-item">
      <h3>Title 1</h3>
      <p>Preview of the message</p>
    </div>
    <div class="message-list-item">
      <h3>Title 1</h3>
      <p>Preview of the message</p>
    </div>
    <div class="message-list-item">
      <h3>Title 1</h3>
      <p>Preview of the message</p>
    </div>
    <div class="message-list-item">
      <h3>Title 1</h3>
      <p>Preview of the message</p>
    </div>
  </div>
  </div>
  
  <div class="message-view">
    <h2>Title</h2>
    <p>
    	Excessively long content...
    </p>
  </div>

</div>

</body>

</html>

Answer №1

Apologies for the blunt example provided - however:

1) Start by creating a page container with a height of 100vh and set the overflow to hidden.

2) Utilize the grid system to establish two columns, also setting their height to 100vh.

3) Within these columns, insert a .scrollable div with a maximum height of 100vh.

<div class="messages">
  <div class="row">
    <div class="col m4">
      <ul class="scrollable">...</ul>     
    </div>
    <div class="col m8">
      <div class="scrollable">...</div>
    </div>
  </div>
</div>

.messages {
  height:100vh;
  background-color: silver;
  overflow: hidden;
}

.col.m4 {
  background-color: white;
}

.col.m8, col.m4 {
  height:100vh;
}

.scrollable {
  max-height:100vh;
  overflow:hidden;
  overflow-y:scroll;
  padding-bottom:20px;
}

View on Codepen.

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 HTML image link is adorned with a tiny blue checkmark underneath

My a tag with an image inside seems to be extending below the image, resulting in a small blue tic mark on the bottom right side. I've attempted different CSS solutions such as setting border to none, but nothing has resolved the issue. Any assistance ...

Make sure PTable maintains a horizontal layout on any device with PrimeNG

When I view my Ptable on desktop or iPad, it looks like this: https://i.stack.imgur.com/ONqZV.png However, when I switch to a device like an iPhone X, it changes to this layout: https://i.stack.imgur.com/H2q7j.png I want the horizontal layout to displa ...

Webpage Text Animation

This is the visual representation of my webpage: https://i.stack.imgur.com/tYq34.png I am seeking to implement a uniform animation effect for the text "Carlos Qiano" and "Lorem Ipsum", similar to the link below: https://i.stack.imgur.com/XVnBS.jpg Note: ...

How can jQuery determine the amount of line breaks within a div element?

My div wrap size is a percentage of the screen width, containing multiple .item divs that break into new lines as the window size decreases. I attempted to calculate the total number of boxes that could fit based on their widths compared to the container ...

Show or hide the expand/collapse button based on the height of the container

Looking for a way to hide content in a Div if it's taller than 68px and display an expand option? The challenge lies in detecting the height of the responsive Div, especially since character count varies. I attempted using PHP to count characters bu ...

Could there be an issue with my website's bootstrap where badges are not being applied properly?

Currently, I am in the process of learning ReactJS and running into an issue where my bootstrap is not working within my .jsx file. Despite following a tutorial (MOSH) diligently and extensively researching on stack overflow, I have not been able to find a ...

Methods to disregard class prefix in css document

When I inspect elements in the Chrome console, I notice styles applied to certain divs/buttons like this: /* Sample Chrome browser styles */ .ItemClass1-0-3-171.ItemClass2-0-3-173: { background-color: "red" } I'm wondering how I can def ...

Inline-block styling behaving incorrectly within span element

I am facing an issue with aligning a validation message inline with a textbox in my HTML code. Despite trying various options, the validation message appears just below the textbox instead of beside it. Below is the snippet of the code in question: HTML ...

What is the process for adjusting the color of the underline in Material UI input fields

I am facing an issue with a Material UI Select component that is on a dark background. I want to change the text and line colors to white just for this particular component, while keeping the rest of the Select instances unchanged. Although I have managed ...

Modify the color of every element by applying a CSS class

I attempted to change the color of all elements in a certain class, but encountered an error: Unable to convert undefined or null to object This is the code I used: <div class="kolorek" onclick="changeColor('34495e');" style="background-c ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

How can I modify the color of a hover button?

How can I change the color of a link on hover? The code I tried is not working properly. Below is the CSS snippet I have already added: a:hover {color: red;} ...

Include a tab button within a vertical tab list using Angular Material

I have utilized Angular Material to create a vertical tab, and I would like to incorporate an Add Tab button within the tab listing itself. Currently, when I add the button, it appears at the bottom of the layout instead. For reference, you can access the ...

Store text in a table format in your local storage

I need help figuring out how to save product and price information to local storage when an "add to cart" button is pressed. Can someone provide guidance on how to do this? Here is the code I currently have: body> <!-- Header--> ...

CSS: Ensuring the width is adjusted to the content or wrapper that is wider

I am facing an issue with mouse-over highlighting on a set of list items. Below is the HTML structure I am working with: <div id="wrapper"> <div id="box"> <div class="item">Lorem ipsum dolor sit amet, sit nonumy utamur ponderum ex& ...

Ways to enlarge the diameter of the inner circle

I've implemented some CSS with JQuery, as seen in this JSFiddle. However, I'm having trouble increasing the width and height of the green circle to be slightly larger than the gray circle (referred to as the main circle) which is positioned at a ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

The "div width 100%" property functions flawlessly when tested locally, yet fails to operate on the live server

Hello, I recently launched my new website at www.leafletsdistributors.com. However, I'm encountering an issue with the Get in touch section (the light grey colored area). Despite setting the width to 100%, it's not fully extending across the scre ...

Generate a new style element, establish various classes, and append a class to the element

Is there a more efficient solution available? $("<style>") .attr("type", "text/css") .prependTo("head") .append(".bor {border:2px dotted red} .gto {padding:10px; font-size:medium}"); $("input:text").addClass("bor gto").val("Enter your t ...

Is it recommended to place the styles created by material-ui for child components after the styles generated for the parent component?

Utilizing material-ui, which utilizes JSS for styling a website. I have a component named Layout that applies margin to all its children (using the all children selector & > * in its style). This functionality works, but I would also like the child ...