Use CSS flex to ensure that each row contains a minimum of two items without cutting off the text too much

Currently, I am in the process of designing a CSS layout for items with some specific requirements:

  • The layout should accommodate at least two items per row, and more if the items are narrow enough to fit on one line.
  • If an item's content is too lengthy to fit within the available space, it should be truncated with an ellipsis without wrapping onto multiple lines.
  • The goal is to utilize space efficiently by fitting as many items as possible on each row while minimizing truncation and maintaining a minimum of two items per row.

The primary objectives include maximizing the number of items per row (at least two) while minimizing truncation (utilize space effectively).

--

To achieve these goals, I have attempted to use flexbox for the layout. However, I am facing challenges in ensuring that there are at least two items per row (setting max-width to 50%), while allowing items to expand to fill the space and avoid text truncation.

Here is an example illustrating this issue:

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

In the example, there is excess space after "Item Two..." in the top row. Ideally, this item should extend to occupy the empty space and reduce truncation.

On the second row, "Item Three..." could stretch further to display more content, possibly moving "Item Five" to the next row. However, "Item Four" must remain on the current row to meet the requirement of having two items per row.

I aim to have at least two items per row (or more without excessive truncation), showing as much content as possible for each item. The objective is to minimize whitespace without compromising truncation.

Any suggestions on potential changes or improvements here would be greatly appreciated. One possibility is eliminating the max-width constraint to prevent unnecessary truncation. Yet, removing this constraint might result in longer items occupying the entire line, potentially reducing to only one item per row, which is not desired.

.box {
  display: flex;
  flex-wrap:wrap;
}

.box > * {
  flex: 0 1 max-content;
  max-width:calc(50% - 4px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}


/* Just for visual styling purposes */
.box {
  border: 2px dotted rgb(96, 139, 168);
  width: 300px;
}

.box > * {
  border: 2px solid rgb(96, 139, 168);
  border-radius: 5px;
  background-color: rgba(96, 139, 168, 0.2);
}
<div class="box">
  <div>Item One</div>
  <div>Item Two two two two two two two two two two two two two two two</div>
  <div>Item Three has more content and so has a larger size</div>
  <div>Item Four</div>
  <div>Item Five</div>
  <div>Six</div>
  <div>Seven</div>
</div>

Answer №1

Do you think this solution would meet your needs?

.box {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.box>* {
  flex: 1 1 150px;
  max-width: calc(50% - 4px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.box {
  border: 2px dotted rgb(96, 139, 168);
  padding: 4px;
  width: 100%;
}

.box>* {
  border: 2px solid rgb(96, 139, 168);
  border-radius: 5px;
  background-color: rgba(96, 139, 168, 0.2);
  padding: 4px;
}
<div class="box">
  <div>Item One</div>
  <div>Item Two two two two two two two two two two two two two two two</div>
  <div>Item Three has more content and so has a larger size</div>
  <div>Item Four</div>
  <div>Item Five</div>
  <div>Six</div>
  <div>Seven</div>
  <div>Eight</div>
</div>

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

Show a portion of decimal numbers without altering their true numerical values

Currently, I am immersed in a web project that involves incrementation animations. To achieve this, I have devised a counter function similar to the one shown below: const counters = document.querySelectorAll('.counter'); counters.forEach ...

Position the label and the select dropdown side by side within the sweetalert 2 component

Can anyone provide an example of aligning a label and dropdown in the same row using sweetalert2? I attempted to customize the label placement, but it appears on a separate line. I would like to log the selected dropdown item upon clicking the OK button. ...

Enhancing text with custom gradient using CSS styling

I am facing an issue where uploading an image with text is not helpful for Google search visibility. I am looking to add a specific gradient style to my text. Could anyone assist me in achieving this particular text look that I have in mind? I came acros ...

The React-Bootstrap Table in a Vite project lacks vertical scrolling when the "responsive" property is enabled

I'm encountering an issue with my React-Bootstrap Table overflowing the viewport in the x-direction on smaller screens. I've tried making it responsive by using the responsive prop, but the overflow-x: scroll feature isn't functioning proper ...

What is the best way to include start and end labels on Bootstrap's progress bars?

Is there a way to add start and end labels directly below the progress bar in Bootstrap? Here is an example of what I want: [||||||||------------] <Start Label> <End Label> I have been searching for a solution, but it seems ...

Monitor Jenkins live logs with a personalized dashboard for real-time viewing

Our Jenkins CE is currently running close to 4000 jobs, with users accessing a Dashboard that utilizes Jenkins APIs. Previously, we provided an href link with a logs button for viewing logs, which would open the Jenkins logs page in an iFrame when clicked. ...

The safari browser is not displaying the menu

I have recently been working on a new website and created a fresh menu for it. While the menu functions perfectly on browsers like Chrome and Firefox, I encountered an error when testing it on Safari. On Safari, the menu appears to be completely hidden. ...

An error occurred when attempting to run the command npm run compile:sass, displaying the message: npm ERR! missing script:

Everything seems to be in place with the sass folders and files, so what could be the issue? I have my package.json file set up correctly with the following code: { "name": "starter", "version": "1.0.0", " ...

Choose an option to perform an action

When I click on vault1 to vault5, something should happen <form id="someform"> <select id="selectid"> <option selected="selected">Choose Vault</option> <option value="1"> Vault 1 </option> ...

Embed SVG text from a different webpage into the image source

I am working with two aspx pages. The first page (Page1.aspx) includes the following code snippet: <img src="Page2.aspx" /> The second page (Page2.aspx) contains this code in its code-behind file: Dim svgString as String = "<svg height="100" wi ...

Troubleshooting problems with rotate3d() in Safari on iOS

There seems to be an issue in Safari iOS version with the transform: rotate3d() I am testing it out using this HTML code <span class="safari-box"></span> For instance, when I use the following code, the animation is visible /* test ...

Item within text box remains untouched

I am working with a 1) Text field 2) Drop down list 3) List box I have written code to add the selected value from the drop down list to the list box and text field, but I'm having trouble removing the value from the text field. Can someone please he ...

Guidance on utilizing jQuery to display values depending on a dropdown selection

This code snippet displays movie data from a JSON variable in a dropdown list based on the selected city. It also needs to show the movie name and theaters list when a user selects a movie, along with the theater dropdown remaining unchanged. Here is my H ...

Using Python to display MySql information in HTML documents

I am currently working on a project that involves displaying a dynamic list of items from a MySQL database on an HTML page. The challenge here is that the list needs to update automatically as the database is modified. Each item on the list should also lin ...

Microsoft Edge and Google Fonts display strong and distinctive lettering

When using Microsoft Edge on Windows 10, all text appears with bold UTF-8 characters: https://i.sstatic.net/JfN0S.png I'm unsure of what the issue is. Is there a solution available? ...

Display a different DIV based on the presence of a URL variable, either hiding one or

If the URL contains the word 'email,' I want to hide the <div id="sociallocker"> and display the <div id="emaillocker">. I attempted the following code but it did not work: <script type="text/javascript"> $(function () { ...

Position on the chart in the Highcharts

I have developed 2 highcharts in the code below: <div> <p id="container"> </p> <p id="cont"> </p> </div> I specified the width and height as follows: $('#cont').highcharts({ char ...

Creating a well-aligned form using Material-UI

Exploring Material-UI for the first time! How can a form be built where certain fields are arranged horizontally, others stacked vertically, and all aligned perfectly both vertically and horizontally? Check out this example image: https://i.sstatic.net/5R ...

Utilize flexbox to create a list that is displayed in a column-reverse layout

I am facing a challenge in displaying the latest chat person in the 1st position (active) using Firebase. Unfortunately, Firebase does not have a date field which makes it difficult to achieve this. I have attempted converting the date into milliseconds an ...

``There seems to be an issue with Firefox's background position percentage

After combining multiple images into one to reduce HTTP requests, I utilized background-position in percentage values to control which image was displayed. Since the width of the images is dictated by the parent element, using percentages rather than pixel ...