What is the best way to ensure a Grid panel is only as tall as necessary to fit its contents?

Attempting to implement a two-column layout using CSS Grid.

Defined classes for grid columns and rows:

.left_1 {
    grid-column: 1;
    grid-row: 1;
}
.left_2 {
    grid-column: 1;
    grid-row: 2;
}
...

.right_1 {
    grid-column: 2;
    grid-row: 1;
}
.right_2 {
    grid-column: 2;
    grid-row: 2;
}

...

The top headers for each column are added as follows:

<div class="left_1"><h4>Left Header</h4></div>
....
<div class="right_1"><h4>Right Header</h4></div>

The height of the grid panels is causing an issue, making them taller than needed. The headers seem oversized due to vertical alignment within the grid elements. Attempts to adjust with CSS properties like grid-template-rows, height, and display didn't resolve this problem.

How can I ensure that the grid panels adjust their height based on content without specifying fixed pixel values?

Edit: Providing a complete file example to demonstrate the issue:

<!doctype html>

<html lang="en>">
    <head>
        <meta charset="utf-8">

        <style>
            .columns {
            display: grid;
            grid-template-columns: 1fr 1fr;
            column-gap: 10px;
            }

            .left_1 {
            grid-column: 1;
            grid-row: 1;
            }
            .left_2 {
            grid-column: 1;
            grid-row: 2;
            }

            .right_1 {
            grid-column: 2;
            grid-row: 1;
            }
            .right_2 {
            grid-column: 2;
            grid-row: 2;
            }

            h4 {
            background-color: yellow;
            }
        </style>

    </head>

    <body>
        <form>
            <div class="columns">
                <div class="left_1"><h4>Left Header</h4></div>
                <select class="left_2" multiple="yes">
                    <option>Option 1</option>
                    <option>Option 2</option>
                    <option>Option 3</option>
                    <option>Option 4</option>
                </select>

                <div class="right_1"><h4>Right Header</h4></div>
                <select class="right_2" multiple="yes">
                    <option>Option 1</option>
                    <option>Option 2</option>
                    <option>Option 3</option>
                    <option>Option 4</option>
                </select>
            </div>
        </form>
    </body>
</html>

Answer №1

While it may seem like a workaround, the key is to ensure that the headers are clearly identified as labels for the columns.
Simply include this line:
margin-bottom: 0;
in your h4 block within the CSS stylesheet.

Therefore, instead of keeping the current setting, make sure to adjust it to the following:

h4 {
  background-color: yellow;
  margin-bottom: 0;
}

Answer №2

Give this a shot:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8>
    <style>
        .columns {
            display: grid;
            grid-template-columns: auto auto;
            row-gap: 0;
            /*grid-template-rows: min-content min-content;*/
            column-gap: 10px;
            justify-content: center;
        }
        .left_1 {
            grid-column: 1;
            grid-row: 1;
        }
        .left_2 {
            grid-column: 1;
            grid-row: 2;
        }
        .right_1 {
            grid-column: 2;
            grid-row: 1;
        }
        .right_2 {
            grid-column: 2;
            grid-row: 2;
        }
        select {
            display: grid;
            grid-template-columns: min-content;
        }
        h4 {
            background-color: yellow;
            margin: 0;
        }
    </style>
</head>
<body>
<div class="my_class">
    <div class="columns">
        <div class="left_1"><h4>Left Header</h4></div>
        <div class="left_2">
            <select class="" multiple="yes">
                <option>Option 1</option>
                <option>Option 2</option>
                <option>Option 3</option>
                <option>Option 4</option>
            </select>
        </div>
        <div class="right_1"><h4>Right Header</h4></div>
        <div class="right_2">
            <select class="" multiple="yes">
                <option>Option 1</option>
                <option>Option 2</option>
                <option>Option 3</option>
                <option>Option 4</option>
            </select>
        </div>
    </div>
</div>
</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

Steps to include a horizontal divider in the footer section of an angular-material table

Is it possible to add a line between the last row (Swimsuit) and the footer line (Total)? If so, how can I achieve this using Angular 15? https://i.stack.imgur.com/581Nf.png ...

Images that dynamically adjust to different screen sizes with captions that are perfectly

For a while now, I have been utilizing <picture> to display images with more design control. I typically use CSS to show or hide specific images within the <div> block. However, I am now considering using <figure> with <figcaption> ...

What is the best way to retrieve an object from d3.data()?

My force directed graph contains a variable with all my link data. var linkData = d3.selectAll(".link").data(); In order to change the class of certain edges based on their values, I am using a for loop to iterate through the data and identify the desire ...

Struggling with incorporating a Carousel feature while navigating through an array

I am struggling to properly implement a carousel using the data from my Videos Array. Instead of getting the desired output where videos should slide one by one, all the videos in the Array are being rendered at the same time. <div id="carouselEx ...

Maximize a video to fit the complete dimensions of the screen's height and width

Issue : I am facing an issue where I am trying to make a video fill the entire screens height and width, but for some reason it is not filling the container I have wrapped around it. The <video> is wrapped inside a <div> with the followin ...

Tips for Creating Sand Text Effects in CSS (Using Text-Shadow)

I am attempting to create a CSS effect that resembles text written in the sand. Here is an example of what I am trying to achieve: As a newcomer to CSS, I would appreciate any guidance on how to make this effect possible. I have tried using the following ...

Guide to automatically changing a background image according to different routes within a React framework

I'm facing an issue with updating the background image dynamically based on different routing options, as no background is being displayed. I've attempted using inline styling and verifying the correct directories are referenced. Initially, the ...

How can we stop mobile email applications from automatically converting emails into a "mobile-friendly" layout without permission?

My email design is optimized to look great on mobile, web, and desktop mail clients. However, I'm facing a challenge with some mobile clients like Gmail on Android automatically reformatting the email to make it more mobile-friendly. This leads to a m ...

Angular does not display results when using InnerHtml

I'm currently in the process of creating a weather application with Angular, where I need to display different icons based on the weather data received. To achieve this functionality, I have developed a function that determines the appropriate icon to ...

cannot display static css files in an express jade template

Looking for a solution to the issue with the dollar answer (same problem). I have app.coffee: pp.configure -> publicDir = "#{__dirname}/public" app.set "views", viewsDir app.set "view engine", "jade" app.use(stylus.middleware debug: true, src: view ...

The presentation of CSS and JavaScript

I'm having trouble pinpointing the error within my .css file. Currently, my JavaScript content is displaying on the far left of the page instead of centered as I desire. Specifically, I want the address and contact information to be centered below the ...

I require the ability to switch between images similar to switching tabs on a menu bar

I am currently working on a billing application for the web and I require a tab menu bar structure. Each tab has 2 images to indicate active and inactive states. Below is the code snippet. <html> <head> <style> .gold1 { position:absolut ...

LESS keyframe mixing is a powerful feature that allows for easy

After extensive searching, I have yet to find a mixin that can create prefixed keyframes declarations without requiring messy fixes. In my quest, I have crafted the following mixin: .keyframes(@identifier, @animation) { .frames { content: ~"'&apo ...

Load images in advance using jQuery to ensure they are cached and retrieve their original sizes before other activities can proceed

When a user clicks on a thumbnail, I aim to display the full-size image in a div. To achieve this, I want to determine the original size of the image based on its source URL before it loads, allowing me to set the appropriate dimensions for the container. ...

Tips for pausing keyframes animation on its final animation frame

Is there a way to halt my animation at the 100% keyframe? What I'm attempting to accomplish is animating these boxes so that when you click on the top one, it moves to the bottom and vice versa. Any suggestions or ideas on how to achieve this? <h ...

Upon clicking on another div, command it to expand to full height and width

I am encountering an issue when loading an HTML file into a content div after clicking on a menu bar option. The HTML file contains 5 tables, but the div only displays the first 5 lines of the first table before requiring a scroll bar to view the rest of t ...

What could be causing this :after element to be influenced by line breaks?

Recently, I have developed a straightforward menu structure as shown below: <ul id="main-menu" class="container"> <li><a href="#">About Us</a></li> <li><a href="#">Home</a></li> <li>& ...

Explore the styling options for SVG using CSS to manipulate different properties of the

After saving my SVG image in Illustrator, the code appears like this: <?xml version="1.0" encoding="utf-8"?> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBo ...

What is the best way to preserve the original formatting and structure when returning raw text?

A unique mini-application has been created where users are able to paste any text and then share it with others. Let's take a look at an example of what the user inputted below. Example: ╔══╗ ╚╗╔╝ ╔╝(¯`v´¯) ╚══`.¸.[Stack ...

The text for the Google chart is nowhere to be found

After creating a test project using the Google Chart example, I noticed that some text from the chart is missing after adding CSS. Here is the CSS code I used: html, body { font-size: 100%; height: 100%; width: 100%; } body { background: ...