Tips for delaying the creation of a new CSS column until absolutely needed

Is there a way to create a fixed column layout where the text fills one column before moving on to the next? Right now, even a single line of text spreads across all columns when I specify 5. How can I make it so that the text fills up each column sequentially before moving to the next one using only CSS?

.content{
-webkit-column-count: 5;
-moz-column-count: 5;
-ms-column-count: 5;
-o-column-count: 5;
column-count: 5;

-webkit-column-gap: 20px;
-moz-column-gap: 20px;
-ms-column-gap: 20px;
-o-column-gap: 20px;
column-gap: 20px;
}

View Example

Answer №1

If you want to ensure your content does not break inside a paragraph, you can wrap it in <p> or a similar tag and apply the break-inside property. Here's an example that demonstrates how this works: https://example.com/jsfiddle

.content p {
    margin: 0;
    -webkit-column-break-inside: avoid;
    break-inside: avoid;
}

Answer №2

I have managed to achieve a sort of working solution using the CSS code below;

column-fill:auto; /* default behavior */
height:100px;

However, this approach does not support percentage (%) sizes. It requires a fixed pixel (px) size which is not feasible for me.

Update

Fortunately, I found this solution that supports relative sizes and performs well on the latest versions of Chrome, Internet Explorer, Firefox, and Safari.

By applying the padding-bottom trick, I was able to establish a relative height for the wrapper container;

.wrapper {
    padding-bottom:10%; /* serving as a spacer - representing my relative height */
    background-color:lightblue;
    position:relative; /* stopping point for absolute coordinates */
}

By implementing this setup;

<h1>short test</h1>
<div class="wrapper">
   <div class="content">
   The quick brown fox jumps over the lazy dog.
   </div>
</div>

I can then proceed with the following adjustments;

.content{
    -webkit-column-count: 5;
    -moz-column-count: 5;
    -ms-column-count: 5;
    -o-column-count: 5;
    column-count: 5;

    -webkit-column-gap: 20px;
    -moz-column-gap: 20px;
    -ms-column-gap: 20px;
    -o-column-gap: 20px;
    column-gap: 20px;

    column-fill:auto;

    position:absolute;
    top:0;
    left:0;
    height:100%; /* filling up the wrapper container */
    width:100%;

    background-color:lightgray;
}

The drawback here is that when there's excessive content, it will continue adding columns horizontally beyond the specified count of 5. It won't expand vertically, but in the broader context, this behavior seems logical.

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

Two-column list using XSLT

I am seeking assistance with xsl transformation. The xml generated by SharePoint Server control is as follows: <rows> <row>A</row> <row>B</row> <row>C</row> <row>D</row> <row>E ...

Is there a way to bring together scrolling effects and mouse movement for a dynamic user

If you want to see a scrolling effect, check out this link here. For another animation on mouse move, click on this link(here). Combining both the scrolling effect and the image movement might seem challenging due to different styles used in each templat ...

Enhancing visual aesthetics with Vuetify on v-slot label components

I am working with Vuetify code that appears like this <v-radio-group v-model="gender" column class="radio-group-full-width"> <v-radio value="Boy"> <template v-slot:label> <v-textarea v-model="answer" v-vali ...

Utilizing CSS transitions to smoothly adjust the width of an element until it completely fills the container div in ReactJS

import React from 'react'; import PropTypes from 'prop-types'; import SearchIcon from '@material-ui/icons/Search'; import InputBase from '@material-ui/core/InputBase'; import { AccountCircle } from '@material-ui ...

Sorting through a table based on the name of the element

I am currently working on filtering an HTML table using a search form. It's working great, but I'm facing an issue where the filtered elements are trying to fill the entire width of the table instead of maintaining their original width (which is ...

Anchor elements and other inline elements not triggering MouseLeave event

Take a look at this CodePen link, particularly in Chrome: https://codepen.io/pkroupoderov/pen/jdRowv Sometimes, the mouseLeave event doesn't trigger when a user quickly moves the mouse over multiple images. This results in some images still having t ...

How can I ensure text remains within a parent div and is positioned above an image?

After writing the following HTML and CSS, an issue arose when resizing the page: <div style="position: relative;"> <img width="100%" src="images/some.jpg"> <div class="header"> <h1>my header</h1> ...

Is it possible to create this layout using Bootstrap 5? I want to stack spans, with one inside the container and one outside

Trying to achieve a specific design using Bootstrap 5.3. https://i.sstatic.net/STifm.jpg This design should be placed over the showcase, with the following code for the showcase displayed below: <div class="showcase d-flex flex-column"&g ...

"Positioned at the top of the page is the alert box, with the

I'm looking to add an alert at the top of my webpage containing a form for visitors to enter their phone number. Currently, I have a "alert alert-info" div with the form inside it placed at the top of my body tag, which works perfectly. However, when ...

The Navbar's expansion is being enhanced by non-collapsible items in the Boostrap 4 dropdown menu

I recently came across a helpful snippet in this question Bootstrap 4 - Navbar items outside the collapse that allowed me to create non-collapsible items for smaller viewports. However, I encountered an issue with a dropdown menu inside one of these items ...

Is there a way to clear the search box in a wenzhixin bootstrap table without refreshing the page when clicked on?

Is there a way to clear the search box in a wenzhixin bootstrap table without refreshing the page when clicking anywhere on the page? <table id="view_table" data-toggle="table" data-search="true" data-page-list="[5, 10, 20]" data- ...

Make sure the top edge of your Bootstrap 4 dropdown menu is perfectly aligned with the bottom edge of your

Trying to make a Bootstrap 4 navbar dropdown display right below the navigation bar, but it consistently shows slightly above the bottom edge of the navbar. Is there a way to make this happen without fixing the height of the navbar and using margin-top fo ...

Determining the optimal line break position in HTML text for a dynamic design

Is there a special CSS or Javascript trick that can be used to mark the preferred line break locations in HTML text when it becomes crowded? I am looking for a solution to optimize line breaks in cramped text areas. Any suggestions? ...

Alignment of Images in a Navbar According to Height

I am in the process of developing a website using Bootstrap 4 and I am trying to create a centered navbar with the navbar-brand. By placing navbar-nav on each side of the navbar-brand, I was able to achieve this layout. However, my issue is that the navb ...

Assign a specific style to Fancybox links and buttons for managing the functions of 'next', 'previous', and 'close'

Utilizing the following jQuery script to hide a div whenever anything that does not have the class 'click' is clicked (used for empty space and certain other divs). $(document).click(function (event) { if (!$(event.target).hasClass('cli ...

Is it possible to create a single button that, upon clicking, fades in one image while simultaneously fading out another?

My goal is to have the blue square fade in on the first button click, then fade out while the red square fades in on the second click. Unfortunately, it seems that my current code is not achieving this effect. I'm open to any suggestions or help on h ...

customizable path settings for sprites using css

Currently, I have a situation where I am using many sprites in my CSS file, and all the image locations are hardcoded within the CSS itself. Now, I am considering moving all these images to a CDN. The problem is that it's quite cumbersome to change th ...

Make sure the div is always positioned above everything, including any built-in pop-up windows

When working with two forms, one for user input and the other as a pop-up window to display results, users sometimes close the pop-up window prematurely if they think there is a network issue causing a delay in data execution. To prevent this, I am consi ...

Tips for creating an indent in a new line

https://i.stack.imgur.com/O8Xbv.png I've been trying to figure out how to make the text indent on each new line when it's pushed to a new line due to screen resizing. I've tried using 'display:block' but it doesn't seem to wo ...

Guide to repositioning elements and fixing the functionality of the hamburger button: [ HTML & Bootstrap ]

Looking for help with creating a navbar for a website using HTML and Bootstrap. Need to move the header and link to the ends of the navbar and ensure that the hamburger button displays correctly on different screen sizes. Here's how it looks on deskt ...