Designing the presentation of two overflowing text containers positioned next to each other

Hey there, I'm currently working on styling something that shows both the user's username and the title of an item on the same line, similar to how it's done on GitHub:

username / title

I want to make sure that if the combined width of the username and title is less than the container width, it displays the full text of both. However, if they exceed the maximum width of the container and start to overflow, I'd like them to be restricted to a 50-50 or maybe 40-60 ratio with ellipsis, like this:

long_usern... / long_ti...

After playing around with max-width, min-width, and percentages, here's where I've ended up:

.username {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.title{
    min-width: 50%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

You can see the implementation in action here.

However, sometimes when the title is too long, it tends to obscure the username, reducing it to just a few dots. I'm not quite sure why it behaves this way and how priority is given to each element when deciding how much width it takes up.

I would like the username to take up at least 30-40% of the total width, regardless of the length of the text in each element. It's okay if the solution imposes a hard limit on the username to prevent it from exceeding 40% width, especially in cases where the username is long and the title is short.

I've tried using min-width for the username class, but it creates unnecessary blank space before the slash when the username is short:

i      / loveyou

Additionally, using a max-width of 50-50 doesn't achieve the desired effect when the username is short and the title is long:

i / hateyousomu...         |
                           | <-- maximum available width for container
username / long_title_na...|
                           |
i / hateyousomuch555       | <-- ideal solution

In conclusion, the perfect solution should maintain a 40-60 ratio of max-width when the combined widths of both elements surpass the available width of the container. Furthermore, it should display the complete width of text in both elements otherwise.

Edit: A big thank you to @robbieAreBest for providing an insightful answer. One issue I encountered was that adding width: fit-content to the .username class didn't yield any results. I discovered that the percentage width the username occupies within the container directly correlates with its length. The balanced 50/50 ratio only holds true when the username length matches or exceeds that of the title. For more information, check out this example. If anyone has insights into why this behavior occurs or suggestions for improvement, I'd greatly appreciate your input!

Edit 2 + current solution I'm content with: I've found a solution, but as always, there's room for enhancement. The current approach covers basic scenarios and caps the username at about 40% of the total width, even with a short title. If there are any overlooked combinations where my code fails, or if someone devises a better solution for handling cases with long usernames and short titles, please share your findings. The ultimate goal is to handle long username and title combinations elegantly while maximizing the displayed information—for instance, limiting the username to 60% width in instances of lengthy usernames paired with shorter titles. Feel free to offer suggestions on displaying the full username in situations where it significantly outstrips the title length. Thanks to everyone who has contributed thus far—I truly value your assistance.

Answer №1

To optimize the display, consider including width: fit-content in your .username class

.username {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: fit-content;
}

.title{
    min-width: 50%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

This adjustment allows the username to shrink and allocate more space to your .title span, while utilizing half of the available area when necessary.


UPDATE AFTER COMMENT

If the previous solution doesn't yield desired outcomes, switching to grid display might be worth considering:

html:

<div class="wrap">
<span class="username">usern</span>
<span class="title">&nbsp;/ super_duper_long_title</span>
</div>

css:

.username {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.title{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.wrap {
  display: grid;
  width: 150px;
  grid-template-columns: auto minmax(20%,100%);
}

https://jsfiddle.net/eu2t0fq3/1/

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

Dependency injection of an Angular app factory toaster is causing the application to malfunction

I am currently working on an Angular application that utilizes Firebase as its backend. My goal is to inject 'toaster' as a dependency within my authorization app factory. Below is the initial setup of the app.factory: app.factory('principa ...

Is it possible to combine the :last-child pseudo-class with the universal selector (*) in CSS or Sass

Here is the code I am currently using: .loken5 * padding-right: 5px .loken5:last-child padding: 0 I am wondering if there is a way in SASS to achieve the same effect with just one '.loken5' tag for saving space. Thank you :) ...

The local variable within the Angular constructor is not initialized until the ngOnInit() function is invoked

I am encountering difficulties with making backend calls from Angular. In my component, I am fetching the "category" parameter from the URL as shown below: export class ProductsComponent{ productList = [] category = "" $params; $products ...

Discover the process of translating words within app.routing.module.ts in Angular

Having some trouble with my Angular routing module and ngx-translate languages. I'm trying to retrieve words from the languages in this module, but the code I've attempted isn't working: import { NgModule } from '@angular/core'; im ...

React Material-UI TextField with Throttling

After exploring React Material UI, I am interested in implementing a TextField component that offers both debouncing and controlled functionality. When we refer to "controlled," we mean that the value persistence is managed externally, with the component a ...

Guide to importing simulated data from a JSON file in Angular 2 Karma Jasmine tests

Currently, I am working on creating karma jasmine test case for angular 2. We have encountered a requirement to mock data in a separate JSON file due to the large amount of data (to maintain code cleanliness). After extensive research, we were unable to f ...

Is it possible for a DIV in one bootstrap column to appear on top of the content in another column?

Is there a way to get a div in the first bootstrap column to overflow the x-axis and have its content display above the second column to the right? <div class="row h-100"> <div class="col-3 bg-info"> <div class="LEFT ...

Node.js and JavaScript promises are not pausing for the sequelize query to complete

The promise mentioned below should ideally return the customer and blox slot as part of the booking record in the second .then(). However, it seems that addCustomer and addBooking have not been executed yet. When I added await to addBooking or addCustomer ...

Creating Multiple Choice Forms in React: A Guide to Implementing Conversational Form

I've been exploring React (Next.js) and I came across an interesting example of how to use multiple choice in simple HTML on Codepen ([https://codepen.io/space10/pen/JMXzGX][1]). Now I'm curious about how to implement a similar functionality in R ...

Arrange the angular datatables in a specific order without displaying the usual user interaction triangles

I am looking to arrange the data in a fixed manner without any user interaction for sorting. However, it seems that I can either completely disable ordering like this: this.dtOptions = { paging: false, lengthChange: false, searching: false, orderi ...

Issue encountered when trying to integrate a CSS sticky footer with a liquid/fluid layout

I've been attempting to incorporate the CSS Sticky Footer technique demonstrated at cssstickyfooter.com. (I also experimented with Ryan Fait's solution without success). I believe I've followed all the instructions correctly. I have a conta ...

How can multiple elements be connected to an onclick action?

I'm having trouble figuring out how to use an onClick action with jQuery for all the elements in a list of attached files. Here is a snippet of my HTML file: <p>Attachments</p> <div> <ul id="attach"> <li id="KjG34 ...

Problem with disabling dates on jQuery datepicker

After spending several days trying to solve this issue, I've decided to seek help. The problem at hand is disabling dates in my bootstrap datepicker using the following HTML code: <head> <!-- Required meta tags --> <meta charset="utf-8 ...

Displaying individual information for each Bootstrap modal in Angular can be achieved by properly binding the data

While looping through images, a modal pops up with its information when clicked. The issue is that all the modals associated with different images display the same content as the first image. This is what has been attempted: data:any; HTML <div *ngFo ...

The functionality of the document download button using Express.js and node.js appears to be malfunctioning

For my current project, I am aiming to enable users to effortlessly download a document by simply clicking on a designated button. https://i.sstatic.net/QenII.png Project Outline: https://i.sstatic.net/SxvfV.png public/client.js console.log(&apos ...

Get rid of the white border surrounding the object tag

Help needed in removing a thin white border around the object tag. Below is the code snippet and what has been attempted: .hr-ob { background-color: #003262; width: 50px; height: 10px; border: none; outline: none; ...

What is the best way to incorporate progressive JPEG images onto a website?

I am currently working on a website called winni.in that is built using Java, Html, and Javascript. I would like to incorporate progressive image rendering upon page load, but I lack the necessary knowledge. I have come across and explored the following re ...

The child module is unable to locate the route URL for the parent module

I'm new to Angular and I'm working on organizing my code into modules. So far, I have an admin module that responds to the /admin request, but now I want to add a child module called Portfolio Module. Everything is working fine, except for the f ...

Use CSS media queries to swap out the map for an embedded image

Looking to make a change on my index page - swapping out a long Google Map for an embedded image version on mobile. The map displays fine on desktop, but on mobile it's too lengthy and makes scrolling difficult. I already adjusted the JS setting to "s ...

Developing play and pause capabilities using JavaScript for the existing audio player

Currently, I am developing a Chrome extension that includes an HTML popup with buttons to play audio files. However, I feel that my current approach is not the most efficient and I am struggling to find ways to simplify the process. The method I am using n ...