This box is designed to be slim and vertical, perfect for holding just a small amount of content. But don't worry, a handy scrollbar will

Looking to create a scrollable box with Bootstrap 4 that adjusts its height based on content? Check out the images below for reference:

https://i.sstatic.net/BTVpi.png

The goal is to eliminate unnecessary vertical white space, like in this example:

https://i.sstatic.net/2TPoO.png

Current HTML code being used:

    <div class="col-sm-3">
      <h2>Domains (258)</h2>
      <ul class="overflow-auto domain-list">
        <li><a href="...">1bh3A00</a></li>
        [...]
        <li><a href="...">8prnA00</a></li>
      </ul>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <a href="....csv"><button class="btn"><i class="fa fa-download"></i> Domains CSV</button></a>

CSS code currently in use:

.domain-list { height: 30em; }

Struggling to find a solution? Let's brainstorm and make it work seamlessly together!

Answer №1

To make your container scrollable when the content exceeds a certain height, you can use the max-height property instead of specifying a fixed height. This way, the container will expand to fit its contents but not exceed the maximum height set. Here is a simple example: once the 7th row is added, the container will become scrollable as it contains more content than the allowed max-height.

let rowNumber = 2;

document.getElementById('add').addEventListener('click', function() {
  const newRow = document.createElement('div');
  newRow.classList.add('row');
  newRow.innerText = `Row #${rowNumber++}`;

  document.getElementById('list').appendChild(newRow);
});
.list {
  max-height: 120px;
  background-color: red; 
  overflow-y: auto;
}

.row {
  height: 20px;
  line-height: 20px; /* this is optional and just for text alignment in the example */
  background-color: #ccc;
}

.row:nth-child(even) {
  background: rgb(15, 156, 250);
}
<html>

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" type="text/css" href="styles.css" />
</head>

<body>
  <div id="list" class="list">
    <div class="row">Row #1</div>
  </div>
  <hr />
  <button id="add">Add row</button>
  <script src="script.js"></script>
</body>

</html>

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

Customize your Outlook emails with HTML and CSS to right-align content for a polished look

My current project involves generating a report to be sent via email, with Outlook being the mandatory application in our system. The report's content is HTML-based and utilizes a table layout. Part of the content needs to appear right-aligned within ...

Implementing a watermark on a website - design and structure

I have a question regarding the addition of watermarks to web pages, such as using the logo of an internet provider. I'm not specifically looking for HTML or CSS code samples. One idea I had was to use a proxy server that would automatically add water ...

Show or conceal input fields depending on the selection of radio buttons

Hello everyone, I am new to JavaScript and currently learning. Can someone please assist me in fixing this code? The required inputs are: radio-button-1; radio-button-2; input-fields-set-1; input-fields-set-2; input-field-submit. My objective is: Upon ...

Get rid of any unnecessary class on the element

I'm currently working on an image slider that displays before and after images when the user drags left to right. The issue I'm facing is that the 'draggable' class is being added not only to these images but also to my hero images. It ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Radio button - arranging the background image and text in alignment

I'm struggling to align my custom radio buttons alongside text. Here is an example image: https://i.sstatic.net/2g5w8.jpg Despite using CSS for styling, I am unable to properly align the radio buttons. Below is a snippet of my HTML: label.item { ...

Is Popper.js malfunctioning when bootstrap CSS is included?

Encountered a rather peculiar issue where Popper.js and Tooltip.js refuse to function properly when Bootstrap CSS is being utilized. Here is the code snippet used for testing: <script src="https://unpkg.com/popper.js/dist/umd/popper.min.js"></sc ...

Ways to eliminate the blue outline on a checkbox in bootstrap 4.1

Bootstrap 4.1 Is there a way to get rid of the blue border on a checkbox that shows up when it is in focus? https://i.sstatic.net/e77BV.png I attempted to use outline, but it didn't work as I had hoped. This is the code I have been using: <div ...

Inject an external page within a DIV container to limit the styling effects of the external page to just that specific area

Imagine you have a webpage divided into two main sections: <div id='section_1'>[various content]</div> <div id='section_2'></div> <!-- an external site will be displayed here --> In section_1, there is a ...

Place the <span> element at the bottom of the <ul> list

Struggling to align the captions of an image carousel at the bottom of the <ul>. No matter what I try, it just doesn't look right. Check out my Fiddle here Below is the HTML structure: <div class="carousel"> <ul> <l ...

Discovering the center of an element and implementing a left float

I'm looking for a way to dynamically position the element #ucp_arrow in the middle of a div using float: left. Here is an illustration: https://i.stack.imgur.com/nzvgb.png Currently, I have hard-coded it like this: JAVASCRIPT: $("#a_account").cli ...

Transforming the sentence into a particular keyboard symbol

Looking for a way to transform this code snippet: <tr> <td width="100%" bgcolor="#96002D" height="3" style="font-size: 1px; line-height: 1px;">&nbsp;</td> </tr> From just a single line into a series of "/" characters like so ...

Position absolute at the bottom must not disrupt the text's natural flow

Is it feasible to position an element absolutely at the bottom within its relative parent while maintaining the flow of its text content from top to bottom? This is a sample layout I am referring to --- <div class="relative" style="padding-bottom: 2em ...

The HTML Search Bar is Misaligned

once more. I seem to be having trouble with my search bar, as it is not appearing in the position or size that I coded for it. The search bar should look like the one shown in this link: https://www.w3schools.com/howto/howto_css_searchbar.asp However, ...

Navigation isn't aligning correctly on the right side

I am having trouble with my navigation menu not aligning properly to the right. <div class="logo"> <h2><i class="icon-reorder"></i> Frosty</h2> </div> <div class="nav"> <a href="#">Home</a> </div& ...

CSS animation not working properly on mobile browsers due to custom code

I've been attempting to utilize a CSS animation as shown below, but I'm encountering issues in mobile browsers. This is likely due to setting overflow to hidden and white-space to no-wrap as part of the animation. p { color: black; white-sp ...

How can I troubleshoot an image not appearing in Bootstrap within a Laravel Blade file while using PHPStorm?

Forgive me if this sounds like a silly question: I attempted to use the following src attribute in an img tag to show an image on a website created with Bootstrap, while using phpstorm with a laravel blade file: src="C:\Users\MAHE\Pictures&b ...

In this guide, we will explore the process of designing unique styles for ng

What is the proper way to customize CSS for a specific element? &.ng-select-focused { &:not(.ng-select-opened) > .ng-select-container { border-color: $ng-select-highlight; box-shadow: $ng-select-box-shadow; } } The offi ...

The animations in my CSS are not functioning properly on iOS gadgets

I am facing an issue with my CSS and JS code. When I slide down the page, my logo should appear from the left with opacity set to 0. However, when I scroll back up to the top, the logo is supposed to move back to its original position on the left and have ...

Organize elements in a grid using CSS styling

I have designed a layout using grids, featuring two menus on the left and right, with a central area for content. Within the content area, there is a 4x4 grid layout. Here's an example of the 4x4-grid layout: https://codepen.io/mannberg/pen/qemayy h ...