Tips for minimizing the padding/space between dynamically generated div elements using html and css

Currently, I have 4 dropdown menus where I can choose various options related to health procedures: Area, specialty, group, and subgroup. Whenever I select a subgroup, it dynamically displays the procedures on the page. However, the issue I am facing is that the height of the div is divided equally among all rows, resulting in larger padding than desired. I am looking for a solution to reduce the padding by selecting different options. To better illustrate the problem, below are some images:

https://i.sstatic.net/MGfcY.png Undesired display

https://i.sstatic.net/tdvVh.png When I change the procedimento_row div height to 30px or fit-content, the padding decreases but still fills the entire div space

https://i.sstatic.net/lWqSL.png This is how I expect the height and padding to appear once a subgroup is selected

Below is the function responsible for generating the rows:

$("body").on("change", "#subgrupo_procedimento", function() {
    var procedimento = "";

    $.get("/procedimento/procedimentos-por-subgrupo/" + $("#subgrupo_procedimento").val(), function(data) {
        for (let i = 0; i < data.length; i++) {                     
        
            procedimento += '<div class="row procedimento_row">';
            procedimento +=     '<span class="col-md-1 procedimento_options">';
            procedimento +=         '<input class="cb" type="checkbox" name="procedimentos" value="'+data[i].id+'">';
            procedimento +=     '</span>';  
            procedimento +=     '<span class="col-md-3 procedimento_options">';
            procedimento +=         data[i].codigoTuss;
            procedimento +=     '</span>';
            procedimento +=     '<span class="col-md-6 procedimento_options" data-toggle="tooltip" data-placement="top" title="'+data[i].descricao+'">';
            procedimento +=         data[i].descricao;
            procedimento +=     '</span>';      
            procedimento +=     '<span class="col-md-2 procedimento_options">';
            procedimento +=     data[i].valorPreferencial.toFixed(2).replace(".", ",");
            procedimento +=     '</span>';
            procedimento += '</div>';
            
        }
        $("#procedimento_table").append(procedimento);
    });
});

The "procedimento_row" divs are nested inside a container called "procedimento_table". Here is the CSS for procedimento_table:

#procedimento_table {
    width: 100%;
    height: 300px;
    overflow-y: scroll;
    margin-right: 0 !important;
    margin-left:  0 !important;
}

Answer №1

Have you considered setting a max-height of 25px in your CSS to prevent stretching caused by flex?

.table_row {
   max-height: 25px !important; 
}

Update: Take a look at this CodePen example showcasing table CSS https://codepen.io/PavlosKaralis/pen/OJMgqRZ

#table_wrapper{
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 100%;
  height: 300px;
  overflow-y: auto; 
}

#table_row{
  min-height: 25px; 
}

Answer №2

Consider removing the class="row" attribute from the

<div class="row" id="procedimento_table">
element. It seems unnecessary as the class is already present in its children elements, which should suffice.

Alternatively, you can modify your #procedimento_table CSS with the following code snippet to make the row class display as flex:

#procedimento_table {
    width: 100%;
    height: 300px;
    overflow-y: scroll;
    margin-right: 0 !important;
    margin-left:  0 !important;
    /* add this */
    justify-content: flex-start;
    align-content: flex-start;
    align-items: flex-start;
}

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

Camera Capacitor designed to eliminate popup notifications

I am utilizing Angular along with the camera plugin in Capacitor to locally save images on both desktop and tablets. I aim to utilize the CameraSource to directly access the camera or open the gallery for files without displaying a prompt. This is how my ...

Complete Form Validation Issue Unresolved by Jquery Validation Plugin

I'm facing an issue with the jQuery validation plugin while attempting to validate a form. Strangely, it only works for one input field. Can anyone suggest a solution? <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.valid ...

Selenium WebDriver: The challenge of using visibilityOfElementLocated when the element is not actually visible

I've encountered an issue with the Firefox Webdriver where it fails to accurately determine if an element is visible. Here is the code I am using, which incorrectly indicates that the element is visible: wait.ignoring(UnhandledAlertException.class).u ...

Has jQuery eliminated every single element from the page?

Can someone help me test the following function? $("tr#phaseRow"+data[2]).remove(); I'm wondering how I can verify if all elements with tr have been successfully removed. ...

The functionality of Angular/Typescript class.name appears to fail during a production build

Using Angular 5, I encountered an unusual problem with the class.name property. We have a TypeScript function as shown below: export class ApiService { public list<T>(c: new(values: Object)=> T) { var cname = c.name; .... } } When ...

Tips for detecting the creation of an iframe before executing any javascript code

Before executing some JavaScript, I am trying to wait for an iframe to be created. I have attempted several methods, but none seem to effectively wait for the iframe. The console error that keeps appearing is Uncaught (in promise) TypeError: Cannot read pr ...

Reliable selection menu for changing fields

Can someone help me with this coding issue? I am attempting to create a dependent dropdown menu where selecting a category will also display relevant equipment. However, in my current setup, only the category field is functioning properly. When I try to a ...

The ExpressJS app generator seems to be struggling to identify and interpret the flags

I seem to be having trouble running the express app generator with flags. For example, when I run express --version, it interprets the --version part as a target directory and creates the app there. This is happening on Windows XP SP3. Could I be doi ...

Slider in MaterialUI featuring a vibrant spectrum of colors

Currently, I am delving into MaterialUI and my focus is on creating a range slider. I came across this example that I found useful: https://material-ui.com/components/slider/#range-sliders My goal is to assign different colors to the "high," "medium," and ...

Unlocking new possibilities with Passport.js - sharing tokens across multiple servers

Currently, I am utilizing passport.js and MongoDB to handle user login and postAPI authentications. However, every time I deploy my node server to a different AWS instance, I find myself having to go through the signup process, log in, and obtain a new tok ...

Implementing AJAX in Rails for the new/create action can greatly enhance the user

I have a timeline with event sections on it that allow you to create, view, update, and delete events directly on the timeline page. Currently, the functionality for deleting an event is working smoothly as the delete section refreshes along with the timel ...

placing a command button to the right side

Currently, I have successfully implemented a graphical solution using jsf2.2 primefaces 6.0 to allow users to view, download, and print pictures through the galleria component of primefaces. However, I am facing an issue with positioning the print command ...

Generating arrays of arrays from a string

Hello everyone, I am currently learning and trying to figure out how to convert a string into an array. var myString = 'one,two\nthree,four\nfive\nsix,seven' What I aim to achieve is converting the string into an array of arrays ...

problems encountered when including next.config.js for sitemap creation in a nextjs application

Just recently, I made an update to my next.config.json file in hopes of boosting SEO performance. However, this change has caused some issues with the next-sitemap module, preventing me from building or starting my app. //next.config.js const withSitemap = ...

Executing a callback function when a window confirmation is triggered during the onbeforeunload event

Currently, I need to implement a feature where a confirmation window pops up when the user tries to close the page. The code snippet for this functionality is included below: window.onbeforeunload=function(){ if(...) { return "Are you sure you want to ...

Event handler assigned to a group of constantly changing elements

Here is my JavaScript code: $(document).ready(function(){ $('#data1').change(function(){ title = $('#title1').val(); url = $('#url1').val(); $.post('library/edit.php',{title:title, url:ur ...

Tips for accessing a URL with a specific value in the HTML file

This form submits a URL in this format: <form action="search.php?search=" method="GET" id="search-form"> <input id="search-text" name="search" type="text" maxlength="30" placeholder="type keyword"/> <button type="submit" name ...

If I dared to eliminate the emphasized line, this code would completely fall apart

<!DOCTYPE html> <html> <head> </head> <body> <h1 id="message-el">Ready to play?</h1> <p id="cards-el"></p> <p id="sum-el"></p> <butto ...

The Facebook app is experiencing issues on Chrome, yet is functioning properly on Firefox

Lately, I've ventured into the world of creating Facebook Apps on heroku. After creating a test app and uploading a page with HTML5, CSS, and Javascript, I encountered an issue where the app wasn't displaying correctly in Google Chrome , but work ...

Concealing a column within an Angular Material table

I'm currently faced with a challenge involving an Angular Material table containing numerous columns. My goal is to selectively hide certain columns based on specific CSS media queries. This is the code snippet I have experimented with so far: HTML: ...