What is the best way to make sure my scrollbar only appears within a specific div?

I'm currently developing an angular single page application (SPA) that needs to generate multiple searchable lists of data within separate tabs. I have structured them like this:

    <body>
      <tabset>
        <tab>
          <div class="listedData">
            <ul>
              <li class="searchBar"></li>
              <li ng-repeat="iterateHere"></li>
            </ul>
          </div>
          <div class="dataDetails">
          </div>
        </tab>
        //... other tabs
      </tabset>    
    </body>

The goal is for the page to take up the full height of the screen, and since the data list will likely exceed that height, I want to implement a scroll bar solely inside the <ul> (excluding the first element - searchBar). Although it's not ideal, I can work with including the search bar in the scrollable area.

To achieve this, I've set up my CSS as follows:

html, body {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}
.listedData {
  ul {
    width: 15%;
    float: left;
    max-height: 95%;
    overflow-y: auto;  
  }
}

However, the outcome isn't what I expected - the list simply extends off the screen to accommodate all elements, and I can scroll the entire page to view them. I attempted setting the height property for all tags in the hierarchy (<tabset>, <tab>, and <div>), but saw no change. Is there a way to achieve my desired layout using only CSS, or am I required to utilize JavaScript?

Answer №1

If you want to assign a percentage value to ul, you need to adjust the height of the enclosing div.

To display a scrollbar, you can apply styles as shown below. I have modified the HTML and values for clarity in this example.

html, body {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}

.listedData {
  height: 35%;
  width: 100%;

}

.listedData ul {
  width: 20%;
  max-height: 70%;
  overflow-y: auto;  
}
<body>
      <tabset>
        <tab>
          <div class="listedData">
            <ul>
              <li class="searchBar">content li 1</li>
              <li ng-repeat="iterateHere"> content li 2</li>
              <li ng-repeat="iterateHere"> content li 3</li>
              <li ng-repeat="iterateHere"> content li 4</li>
              <li ng-repeat="iterateHere"> content li 5</li>
              <li ng-repeat="iterateHere"> content li 6</li>
              <li ng-repeat="iterateHere"> content li 7</li>
              <li ng-repeat="iterateHere"> content li 8</li>              
              <li ng-repeat="iterateHere"> content li 9</li>
              
            </ul>
          </div>
          <div class="dataDetails">
          </div>
        </tab>
        //... other tabs
      </tabset>    
    </body>

Note: If you only want the scrollbar to exclude the search bar, it may require more effort. Consider placing an input before the ul element for improved usability.

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

Ensure that the responsive SVG image remains centered even when resizing

I have been attempting to center crop the responsive SVG image while resizing. Even though I used preserveAspectRatio="xMidYMax slice", the image width does not stay consistent with my screen size. body{ margin:0;padding:0; } .wrapper { position: re ...

Enhancing a bootstrap gallery with a zoom in effect

I need help creating a gallery using Bootstrap that resembles the style shown in this example: While everything is functional, I am facing an issue with aligning the pictures properly after adding a hover effect. I tried placing each picture in individual ...

What is the best way to adjust an element to match the height of the browser window?

I've coded a solution to ensure that the height of a section (#home) matches the height of the window, but it's causing issues. Here is the code snippet I used: // Adjust Home section height based on window height function fitHomeToScreen() { ...

The integration of Vue JS is not displaying properly in an Electron application

My electron app is loading Vue JS 2 from my local machine, but when I attach my el to an element, it completely empties the element and replaces its contents with a Vue JS comment. What could be causing this issue? index.html <!DOCTYPE html> <htm ...

PHP-generated graphic. CSS glitch causing scaling issues. SVG polygons involved

I've developed a script that utilizes SVG Polygons to present a chart. Here's the scenario. I have a product with a maximum price and a minimum price. Every Monday (referred to as a cycle), I gather this information. My system then computes a b ...

Can you identify the method used to apply CSS classes to an element?

Looking for clarification, I've noticed a pattern of "adding" (although unsure if that's the correct term) classes to elements in HTML UI frameworks. For instance, considering an element with these class attributes: class="mif-earth mif-2x" To ...

What is the best way to select both an inline element and a block-level element at the same time?

After receiving a shortcode from a WordPress theme, the output is: <div class="heading clearfix"><i class="icon icon-list-ol h2"></i> <h2> Hello World! </h2></div> I am looking to style both the icon and h2 elements to ...

Is it possible to combine Ajax, JS, HTML, PHP, and MySQL in a single project?

I am looking to create a web application and wondering if I can integrate Ajax, JavaScript, HTML, PHP, and MySQL all together? ...

Struggling to achieve the desired outcomes despite utilizing JQuery for page refreshing

I am facing an issue with using Jquery to refresh the page. Currently, when I select an option from my category, the page refreshes but then my selected category disappears. What I actually want is for the page to refresh when I select an option, but for ...

When hovering, apply style 1 to all elements with the same id, and style 2 to the hovered element

I'm working with some user-generated divs that I want to dynamically highlight when hovered over, while simultaneously blurring the other divs. My challenge is figuring out how to change the style of the hovered div separately from all the others. Th ...

AngularJs components are restricted from accessing Groovy code

Within the template of my AngularJs 1.5 component, I have some awesome code that unfortunately cannot be executed in an HTML file. The component template property restricts us to only using HTML files. Is there a workaround for this limitation? I attempte ...

When you first open material-ui cards, there are no default CSS styles applied

https://i.sstatic.net/EVdO8.png My cards created using material-UI are displaying strangely when the page loads or re-loads. Initially, they look different with no apparent CSS styling, but after a few seconds, they appear as intended. I'm not sure i ...

Troubleshooting problem with displaying background images on iPad and iPhone devices

A recent project involved building a website for a new client, but there seems to be an issue with the background rendering on his iPad and iPhone. The image is displaying incorrectly, showing only a small portion from the top right corner rather than the ...

Can PHP offer a more streamlined method for displaying the chosen item from SQL in a dropdown menu?

I am attempting to extract the name of the country from the database and display the chosen country. The current method I am using involves listing all 193 countries individually, which seems inefficient. Is there a simpler way to accomplish this task? & ...

Showing a database table in an HTML format using JavaScript

I am currently working on displaying a database table in HTML. I have written a PHP code snippet which retrieves the table data and formats it into an HTML table without any errors: function viewPlane() { if(!$this->DBLogin()) { $ ...

Use a boolean value to determine the styling of multiple items simultaneously

I'm currently attempting to modify the appearance of the bars in each area (a total of 12), so that a value of 1 equates to true (displayed as green) and a value of 0 equates to false (displayed as red). This will dynamically change the color of each ...

Issue with Dismiss Button Display in Bootstrap Alert

I've been experimenting with Bootstrap and I realized that even though I followed all the instructions on their Alert page, my close button just appears as a large grey square with an X in it. Any suggestions on what might be missing? https://codepe ...

Is there a way to align these buttons in the middle?

I am currently designing a webpage at The challenge I'm facing is: How can I center those buttons on the page? They need to remain centered and responsive even when the page is resized. Additionally, the spacing between the buttons is inconsistent. ...

Utilize the Angular 1 function again

Imagine you have two sets of items: available items and associated items. I need to create a search function that utilizes different API methods to search each list separately. After obtaining the search results, I must store them in models using the $scop ...

Making changes to Angular within Protractor

Here is my angular controller: .... .controller('EndCtrl', ['$scope', '$location', 'Players', function($scope, $location, Players) { console.log('players', Players); $scope.players = Players; $scope. ...