Tips for positioning two spans next to each other at full width using CSS

In my custom file manager, I am looking to display truncated filenames in a unique way. It is crucial to show the beginning and ending of the filename, so simply using overflow:ellipsis is not sufficient.

The tiles displaying the filenames can adjust their width within bootstrap columns. Since they are rendered with handlebars, I have the ability to manipulate the string before it is rendered using a helper function. I prefer not to rely on additional JavaScript, such as event listeners for resizing.

My concept involves splitting the filename string with regex while the tile is being rendered. For example,

"long_filename_with_number_at_the_end_001.jpg"
would become
["long_filename_with_number_at_the_end_", "001.jpg"]
.

Next, I aim to include these string segments in two spans inside a parent div, where the right span expands to fit its content while the left span fills the remaining space. The left span should have the text-overflow:ellipsis property.

While exploring solutions, I came across this helpful answer that aligned with my goals. I made a few modifications, but did not achieve the desired outcome.

* {
    position: relaitve;
}
.outer {
    width: 110px;
    height: 22px;
    border: 1px solid #ccc;
}
#colA {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    background:yellow;
}
#colB {
    float:right;
    background: pink;
}
<div class="outer">
    <div id="colA">long_filename_foo_bar</div>
    <div id="colB">001.jpg</div>
</div>

Answer №1

To rearrange the order, simply interchange the positions of #colA and #colB in your HTML code.

<div class="outer">
    <div id="colB">001.jpg</div>
    <div id="colA">long_filename_foo_bar</div>
</div>

For reference, here is the JSFiddle link: http://jsfiddle.net/skeurentjes/0nam7L2q/

Answer №2

Utilizing Flex for the Solution:

.file-info {
    display: flex;
}
.file-info .start {
    flex-shrink: 1;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}
.file-info .end {
    flex-shrink: 0;
}

/* Additional Styling */
.column {
    width: 150px;
    border: 1px solid #666;
    overflow: hidden;
}
.file-info:hover {
    display: flex;
    background: #ddd;
}
<div class="column">
    <div class="file-info">
        <div class="start">long_filename_foo_bar_</div>
        <div class="end">001.jpg</div>
    </div>  
    <div class="file-info">
        <div class="start">long_filename_foo_bar_</div>
        <div class="end">123001.jpg</div>
    </div>
    <div class="file-info">
        <div class="start">short_</div>
        <div class="end">001.jpg</div>
    </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

The valueChanges event of a Reactive Form is activated whenever options from a datalist are selected

Whenever a user types into the input field, I am making an API call to retrieve and display data in a datalist for autocompletion (typeahead). control.get('city').valueChanges.pipe( map((searchText) => searchText.trim().toLowerCase()), fi ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

Enhancing the functionality of radio buttons through event changes and optimizing related features

I am searching for a more efficient method to reuse functions that serve a similar purpose. For example, I would like to modify various radio buttons that toggle a hide class on different divs. JSFiddle Link How can jQuery be used to create a reusable fu ...

Is it possible for me to utilize a validation function to display error messages within a specific span id tag?

document.getElementById("button1").addEventListener("click", mouseOver1); function mouseOver1(){ document.getElementById("button1").style.color = "red"; } document.getElementById("button2").addEventListener("click", mouseOver); function mous ...

Is it possible to include jQuery's selectors contain and closest within my CSS selector?

I created a script to hide specific table rows as follows: $('.ms-formtable nobr:contains("Question")').closest('tr').hide(); However, I'm unsure if there is a way to achieve the same result using only CSS. Can anyone provide adv ...

Is there a way to prevent cards in a carousel slide from wrapping onto two lines?

Currently, I am utilizing Bootstrap 5 to develop a website and an accompanying carousel. My objective is to align all the items in the carousel vertically. Unfortunately, I am encountering some difficulties in achieving this layout. Can anyone provide assi ...

Adding CSS files to a JSP page in a Struts2 application

Hello, I am currently learning Java and working on a Struts2 project. I have added a stylesheet in the following path: /WEB-INF/common/css/style.css and also included Bootstrap in this directory: /WEB-INF/common/css/bootstrap/bootstrap.min.css I ...

Are there any resources available to ensure my jQuery script is compatible with multiple browsers?

After realizing that Internet Explorer does not support certain selectors in jQuery, I began to question how to ensure my code will function properly during the writing process. As a Linux user, my testing options are limited to Chrome and Firefox. Is ther ...

What are some ways to create a traditional HTML form submission and incorporate jQuery solely for the callbacks?

Given that my page consists solely of a simple "name and email" registration form, I see no reason why I shouldn't stick to the traditional approach: <form action="/Account/Register/" method="POST" id="registration-form"> <fields ...

Enhancing the bottom border of an unordered list when incorporating styled bullets

I have a list with some items. I used a CSS trick to style the bullet as a circle. However, I would like to add a border underneath each item to separate them. Because I removed the default bullets using list-style: none, the "bullet" is positioned -1em, ...

What is causing the error message "Error: Cannot update active font: 'Fira Sans' is not included in the font list" in react-font-picker?

For my project, I have implemented a font picker component in the following manner: <FontPicker apiKey={process.env.REACT_APP_GOOGLE_API_KEY} activeFontFamily={activeFontFamilyMobile} ...

Utilizing Angular's ng-repeat with varying directives for each iteration

I am trying to utilize ng-repeat within Angular to iterate through multiple <li> elements with a directive embedded. My goal is to have the first two items in the ng-repeat display differently in terms of styling and content compared to the remaining ...

The element <li> is not permitted to be a descendant of the <select> element

Encountering an error when using the native attribute in the Select component: <TextField select value={options.length ? value : defaultValue} SelectProps={{ native: true, }}> Utilizing the material UI Textfield component. validateDOMNe ...

Alignment of Card Element at Bottom in Bootstrap 5

Hey there! I'm new to Bootstrap and I'm facing an issue where I want to position a button element at the bottom of a card, even when the body text is minimal. However, no matter what I try, the button does not end up in the bottom right corner as ...

JavaScript's addition of CSS animation not functioning as intended

I am facing an issue with a web page utilizing Bootstrap 5. The webpage consists of one element stacked on top of another. My goal is to fade-out the top element after a certain period. Here is what I have tried: HTML <div id="host" class=&qu ...

There seems to be an issue with the SQL syntax. Please refer to the corresponding manual for MySQL

An SQL error has occurred. Please refer to the MySQL server manual for the correct syntax near ' product_code, product_name, product_desc, price FROM products' at line 1 <?php include('include/conn.php'); $per_page = 5; $adjacen ...

The JavaScript code in the HTML files is not functioning as expected

Upon form submission in my program, the form action directs to xyz.php. Within xyz.php, I simply use INCLUDE("abc.html") to redirect to another HTML page. The abc.html page contains HTML tags and JavaScript code, but after the form submission, the page red ...

Develop a design utilizing a foundational database entity

I'm new to AngularJS and I am seeking guidance on how to properly separate the model from the controller. In my previous experience, I have always integrated models within the controllers. For example: angular.module("app").controller("customerContr ...

Can you explain the distinction between locating an element by its class name versus locating it by its CSS selector?

Class name: var x = document.getElementsByClassName("intro"); CSS selector: var x = document.querySelectorAll("p.intro"); I'm a bit puzzled, are there any distinctions between the two methods or are they essentially the same? ...

I have an HTML table with multiple cells containing inner HTML tables. I have implemented a function along with buttons to filter the main table, excluding the inner tables

My HTML Table is generated from my database, containing information about machines and their status pulled from emails with HTML Tables. Each row has a click option to open/hide the <td> tag showing the original table for more details and better trac ...