Preventing Line Breaks in CSS Grid Autofil Experiment: A Guide

My journey in grasping the concepts of CSS Grid starts with this exploration.

As you adjust the display width, observe how the text "Client code" switches between being displayed on one line and then breaking onto a new line repeatedly.

.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    border: 0.5px solid Magenta;
}
.flex-display {
    display : flex;
    border: 0.5px solid Green;
}

.dbForm *{
    font-size: x-small;
}

.dbForm-nonTable {
    margin: 2px;
    padding: 2px;
    border: 0.5px solid #000000;
    overflow: auto;
    background-color: #ffec80; /* light gold */
    min-height: 10px;
    min-width: 30px;
}

.dbForm-label {
    margin: 2px;
    padding: 2px;
    overflow: auto;
    min-height: 10px;
}
<body>
<div class="dbForm" dbase-source="invoices" >

    <div class="grid-container">
        <div class="flex-display">
            <div class="dbForm-label">Date:</div>
            <div class="dbForm-nonTable">bumble doo</div>
        </div>
        <div class="flex-display">
            <div class="dbForm-label">Client code:</div>
            <div class="dbForm-nonTable">Dodo</div>
        </div>
        <div class="flex-display">
            <div class="dbForm-label">Notes:</div>
            <div class="dbForm-nonTable">Nabble</div>
        </div>
    </div>

</div>
</body>

Access the JSFiddle version here.

The goal is to keep the "label" and "dbase value" boxes on the same line using display : flex.

Uncertainty arose over the switching pattern observed. It was hypothesized that due to minmax(100px, 1fr), additional "imaginary" DIVs (each taking up "1fr") are inserted to the right of the original 3 DIVs as the grid widens. This sometimes results in a line break for the "Client code" text.

The preference is for only the initial 3 DIVs to exist and either expand indefinitely as the grid widens or maintain their width when all 3 are on one row without any line breaks. Line breaks should ideally occur only in extremely narrow viewports, currently arising at two specific points - one with 2 columns and another with 3.

The aim is to prevent the addition of these "imaginary" DIVs altogether.

Seeking advice on achieving this desired outcome.

An attempt was made by inserting a non-breaking space &nbsp; between "Client" and "code," resulting in a different alternation pattern upon widening: one-line ... horizontal scrollbar ... one-line ... horizontal scrollbar...

Additionally, changing to minmax(150px, 1fr) prevents "Client code" from experiencing line breaks but lengthening the text in the dbase box causes it to start breaking again. The aim is for the dbase box to allow internal text to wrap but avoid line breaks for the label except in the aforementioned situations of 2 or 3 columns.

Answer №1

If you're searching for the auto-fit property, not auto-fill:

I came across a detailed explanation about this topic here:

auto-fill fills up the row with as many columns that can fit. It generates implicit columns each time there is space for a new column because its goal is to fill up the row with maximum possible columns. The newly added columns may be empty but they will still occupy specified spaces in the row.

auto-fit adjusts the currently available columns in the area by expanding them to occupy any extra space. This happens after filling up the surplus space with additional columns (similar to how it works with auto-fill) and then collapsing the empty ones.

.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    border: 0.5px solid Magenta;
}

.flex-display {
    display : flex;
    border: 0.5px solid Green;
}

.dbForm *{
    font-size: x-small;
}

.dbForm-nonTable {
    margin: 2px;
    padding: 2px;
    border: 0.5px solid #000000;
    overflow: auto;
    background-color: #ffec80; /* light gold */
    min-height: 10px;
    min-width: 30px;
}

.dbForm-label {
    margin: 2px;
    padding: 2px;
    overflow: auto;
    min-height: 10px;
}
<body>
<div class="dbForm" dbase-source="invoices" >

    <div class="grid-container">
        <div class="flex-display">
            <div class="dbForm-label">Date:</div>
            <div class="dbForm-nonTable">bumble doo</div>
        </div>
        <div class="flex-display">
            <div class="dbForm-label">Client code:</div>
            <div class="dbForm-nonTable">Dodo</div>
        </div>
        <div class="flex-display">
            <div class="dbForm-label">Notes:</div>
            <div class="dbForm-nonTable">Nabble</div>
        </div>
    </div>

</div>
</body>

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

Unable to apply styling to table cells that are dynamically added using JQuery within an Angular

Currently working on setting up a form using Angular. Within the hotel.view.html file, there is a table that looks like this: <table id="rooms"> <tr> <td>Room Type</td><td>Beds</td><td>Q.ty</td ...

What is the best way to restrict the border to just one element?

I've configured my navigation bar using a list with li elements. To give it a rounded appearance, I applied a border-radius to the background. Now, I'm looking to remove the border-left specifically from the home element. Below are the code snipp ...

Prevent span/button clicks by graying them out and disabling the ability to click using HTML and JQuery

I am facing a challenge with my two spans that reveal specific information when clicked. I want to make one span appear as a disabled button (greyed out) when the other span is clicked. I tried using $("#toggle-odd").attr("disabled", tr ...

Angular 11.0.3 displaying ngClass issue (Unable to bind ngClass as it is not recognized as a property of div)

While working on an angular project, I implemented a light and dark theme using mat-slide-toggle to switch between themes. The theme is stored as a boolean called isDark in a Behavioral Subject service. There are two lazy-loaded modules - one for the home ...

maximum distance a button can respond to in a CSS layout

I'm trying to figure out why my CSS button has such a large clickable area. Is there a way to reduce it? When I hover over the button, the clickable area extends beyond the text, which is not ideal. Any suggestions on how to fix this without altering ...

Coordinate the timing of CSS animation with the loading of the page

Despite the abundance of similar questions, none have quite addressed my specific query. I've created a preloader using CSS animation and I want it to synchronize perfectly with the page load. While I can trigger the animation on page load, I'm s ...

Embedding a PDF file into an HTML form

For days, I have been on a relentless search for a solution to my problem to no avail. My ideal scenario involves embedding a fillable pdf form into an intranet html form for submission to the server, with the ability to parse field/values being a bonus b ...

Linking issue with href tag

I am having trouble with my download links for PDFs of my Chinese articles. I've tried different approaches, but none of them seem to work. I even attempted linking it to my homepage as a test, but still no luck. <link href="http://unique-domain.c ...

Is there a way to narrow down the font choices using HTMLPurifier?

- Currently in my project, I have incorporated an HTML-WYSIWYG Editor that allows users to utilize various Webfonts for styling their texts. The editor I am utilizing can be found at NiceEdit. It generates code snippets such as: <font face="c ...

Utilize a CSS selector to target all deleted nodes using the MutationObserver in the HTML5 API

Is there a way to retrieve all removed nodes from the DOM that have specific classes using CSS selectors? Below is an example of how it can be done with some complexity, but is there a simpler and more general approach? new MutationObserver(mut =& ...

What is the process of sending data to a MongoDB database using node.JS?

Well, the code below is not exactly neat, but we're all learning, right? The goal here is to POST a basic user profile into a database, but it seems like I'm getting a 404 error. I'm relatively new to these technologies, so if anyone could ...

Steps for invoking the Struts Action class and presenting the information on the screen utilizing jQuery

In my JSP page, I have a table displaying data in a tabular format with three different links in one column. Each link triggers a different method in the Action class, which then appends rows based on the returned list size. Using struts iterator, I iterat ...

Adding a Unique Custom Menu Item in _tk Wordpress Theme: Incorporating a Button and Search Form

I am currently working on customizing the _tk theme for WordPress, and I have hit a roadblock when it comes to the Nav Menu functionality. Adding menu items from the WordPress admin page is easy for custom links and pages. However, I am looking to include ...

Adjusting column styles on mobile or desktop devices

My row has two columns <div class="row"> <div class="col-9"> hello </div> <div class="col-3"> world </div> </div> But I want to change the column classes for small screens, so they would be ...

Position the button at the bottom of the page with MUI v5 in a React application

How can I ensure the button is always positioned at the center bottom of the page, regardless of the content height? This is a snippet from my code: const useStyles = makeStyles({ button: { bottom: 0, right: 0, position: "absolute" ...

Managing images in JavaScript while conserving memory

Welcome I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px. My vision involves creating a captivating visual experience for users as t ...

Guide to making a dynamic image map with interactive links using HTML and CSS

Seeking advice on how to create clickable areas within an image that lead to different pages on my website. Image maps seem like a good solution, but the issue is that they require specific coordinates which may not work well for responsiveness. I was hop ...

Creating a sleek and stylish panel with a static horizontal table size in Bootstrap 3.0

I'm having trouble with the nested table staying a static large size while the panel decreases as I resize my browser. Any tips on how to fix this? <div class="panel panel-primary"> <table class="table"> ... Here is an exampl ...

Experience a seamless transition to the next section with just one scroll, allowing for a full

I've been attempting to create a smooth scroll effect to move to the next section using Javascript. However, I'm encountering issues with the window's top distance not being calculated correctly. I'm looking to have the full screen div ...

invoking a function from an attached HTML file using Angular

How can I invoke a function in Angular using inner HTML? I attempted the following: cellTemplateFunc(cellElement, cellInfo){ var subContainer = document.createElement('div'); subContainer.innerHTML = "<a href='javascript:v ...