How come the div elements in my list are expanding beyond their container?

Below is a simplified version of my code that illustrates the issue I'm facing. I am trying to enclose all the elements within a box and enable scrolling for the .content section. So far, all my attempts at resolving this problem have been unsuccessful.

.container {
  top: 13.5px;
  left: 16px;
  width: 377px;
  height: 140px;
  position: absolute;
  right: auto;
  bottom: auto;

  border: 1px solid red;
}

.content {
  overflow-y: auto;
}

.title {
  height: 30px;
  border-bottom: 1px solid black;
}
<div class="container">
  <div class="title">
    <h2>Title</h2>
  </div>
  <div class="content">
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
  </div>
</div>

Answer №1

To ensure your content displays properly, it is important to define a specific height for it in the CSS code.

.content {
  height: 50%;
  overflow-y: auto;
}

Answer №2

To create a scrolling effect for the content within the .content class, you have set overflow-y: auto. However, to ensure that the content remains contained and does not overflow outside the div, you need to specify a height for the <div class="content">.

This will prevent the content from spilling out of the designated area.

.container {
      top: 13.5px;
      left: 16px;
      width: 377px;
      height: auto;
      position: absolute;
      right: auto;
      bottom: auto;
    
      border: 1px solid red;
    }
    
    .content {
      overflow-y: auto;
       height: auto;
    }
    
    .title {
      height: 30px;
      border-bottom: 1px solid black;
    }
<div class="container">
  <div class="title">
    <h2>Title</h2>
  </div>
  <div class="content">
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
  </div>
</div>

Answer №3

To implement a flexible layout, you can utilize the display:flex property.

Below is the code snippet:

.container {
  top: 13.5px;
  left: 16px;
  width: 377px;
  height: 140px;
  position: absolute;
  right: auto;
  bottom: auto;
  border: 1px solid red;
  
  display: flex;
  flex-direction: column;
}

.content {
  overflow-y: auto;
  flex-grow: 1;
}

.title {
  border-bottom: 1px solid black;
}
<div class="container">
  <div class="title">
    <h2>Title</h2>
  </div>
  <div class="content">
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
    <div><span>Hello</span></div>
  </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 compatibility issue between JQuery function and Python's Bottle framework

I'm attempting to customize the behavior of the submit button in an HTML form to incorporate a JQuery animation before proceeding with the form submission. The JQuery animation functions properly when I run the HTML file from the IDE, but when I load ...

PHP code: Output an empty <td> element for each iteration in a foreach loop

I have two arrays: $arrInfoComment; The first array contains the ID of the users who have commented along with their comments. (For example: 2 comments) $arrInfoExpert; The second array contains the IDs of users. (3 users) I am using a foreach loop to ...

Using jQuery and CSS to Filter and Sort Content

I need help with filtering a list of content based on their class names. The goal is to show specific items when a user clicks on one of two menus. Some items have multiple classes and users should be able to filter by both menus simultaneously. Currently ...

Incorporating a new component into the table tab design with HTML

One more question to wrap up the day. Here is my current setup along with the corresponding CSS: <p>&nbsp; </p> <p>&nbsp; </p> <div class="row tab-cover"> <div class="col-sm-4"><a href="/home" target=" ...

The size of table td is less than 100%

I'm having trouble getting my td to fill 100% of my table. I have tried using the following code: .table-scroll tbody { display: block; overflow-y: scroll; height: calc(100% - 130px); } Even with the display block property, the td element is s ...

Tips for verifying the presence of a value within an array using checkboxes

My firestore database contains a collection named world with a sub-collection called languages I have developed two functions: one to retrieve all documents from the sub-collection languages, and another function to fetch every language if the userUid val ...

Ways to adjust the size or customize the appearance of a particular text in an option

I needed to adjust the font size of specific text within an option tag in my code snippet below. <select> <?php foreach($dataholder as $key=>$value): ?> <option value='<?php echo $value; ?>' ><?php echo ...

Find the value of the nearest input field using jQuery

I'm working with HTML code that looks like this: <tr> <td class='qty'><input class='narrow' value='1' /><i class='fa fa-trash' aria-hidden='true'></i></td> < ...

The Best Practices section of the Chrome lighthouse report has identified an issue with properly defining the charset

I recently noticed an error in my VueJs SPA application when running a Chrome Lighthouse report. The error message indicated that the charset was not properly defined, even though I had added it to my index.html file. Below are screenshots of the issue: ...

Increase the border thickness on the top side of the div

To enhance the design of the .menu div, I decided to include an additional border at the top. Initially, I incorporated top and bottom borders using box-shadow, followed by traditional borders using border-bottom, border-left, and border-right properties. ...

Exploring the possibilities of integrating CSS and JavaScript in Node.js with Express

Here is the folder structure I am working with: lifecoding |login2 |index.html |css style.css |js index.js I have all the necessary modules installed for express to work properly. However, when I try to set the static path as shown below, it keeps ...

Having trouble adjusting the height of <a> elements in the navbar

My navbar has an issue where the listed items do not fill the entire height of the navbar. I have tried adjusting padding and margin, but it doesn't seem to be affecting the layout. Any suggestions would be greatly appreciated. Thank you. #navbar { ...

The component is not responding to list scrolling

Issue with Scroll Functionality in Generic List Component On the main page: <ion-header> <app-generic-menu [leftMenu]="'left-menu'" [rightMenu]="'client-menu'" [isSelectionMode]="isSelectio ...

Tips for preserving HTML markup in a Google Sheets cell without disrupting the tags

I am currently experimenting with using Google Sheets to input the complete HTML of an email into a cell, where I have variables for text, image src, href src, and more. As of now, I am utilizing Google Script to substitute tags with the corresponding val ...

Blazor Server: Seamless breadcrumb navigation updates with JavaScript on all pages

I have implemented a breadcrumb feature in my Blazor Server project within the AppLayout that functions for all pages: <ul id="breadcrumbs" class="breadcrumbs"> <li><a href="#">Home</a></li> ...

Dynamic motion of balloons using jquery technology

I am trying to make an image move smoothly inside a div using jQuery and CSS. I have tried rotation and animation, but nothing has worked the way I need it to. <div class="baloon" style="position:fixed;left:0px;top:160px;"> <img id="rot" class="r ...

When the audio on the device is in use, the video will not play and vice versa

Whenever my video starts playing, the audio from my device (such as iPod or Spotify) stops. If I try to play the audio manually while the video is playing, the video freezes. Interestingly, when I tested playing an audio file directly within the app, it wo ...

javascript snippet for extracting the dynamically created div IDs

Currently, I am developing an application using Java and AngularJS. Within this project, there is an HTML page that iterates through an object to display the values on the page. Here is the snippet of the HTML code: <div id="{{value.pageIndex}}" ng-re ...

What is the best way to sort ng-options in an AngularJS select element?

I am encountering a situation where I have the following code snippet: <select ng-model="person" ng-options="person.name for person in mv.people"> However, within the mv.people array, there are certain members that I need to hide. For examp ...

Tips for resizing an image instead of a line in HTML5 canvas

For instance, we could create a function like this to draw a line: function drawLine(g, n, x1, y1, x2, y2){ g.beginPath(); g.lineWidth = n > 0 ? n : 1; g.strokeStyle = "rgb(0, 128, 32)"; g.moveTo(x1, y1); g.lineTo(x2, y2); g.str ...