Check out this creative CSS solution that utilizes the data-*
attribute and two ::after
pseudo-elements. I've also included an optional hover effect and a way to show all text (the #fileName::after
pseudo-element must be removed when displaying full text).
Sample 1
#fileName {
position: relative;
width: 100px;
}
#fileName p {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
#fileName:after {
content: attr(data-filetype);
position: absolute;
left: 100%;
top: 0;
}
/*Show on hover*/
#fileName:hover {
width: auto
}
#fileName:hover:after {
display: none;
}
<div id="fileName" data-filetype="txt">
<p>This is the big name of my file.txt</p>
</div>
Exploring Advanced Techniques — concealing the appended filetype for short filenames
The #fileName p::after
has a background color that blends with the text's background, effectively covering the ".txt" for brief filenames that don't get cut off by overflow: hidden
.
Note the use of padding-right: 22px
which positions the ".txt" beyond the ellipsis.
For different approaches with varying browser compatibilities, refer to examples 2 and 3 below. It's challenging to hide the ".txt" uniformly across all browsers.
Sample 2
Test Results on Chrome and Firefox: Compatible.
The #fileName p::after
uses a matching background color technique to obscure the ".txt" when dealing with truncated filenames due to overflow: hidden
.
Note the application of padding-right
in each ::after
pseudo-element. padding-right: 22px
extends the cover past the ellipsis, while padding-right: 100%
assigns the pseudo-element its width. Unfortunately, Edge and IE 11 do not respond to padding-right: 100%
.
#fileName {
position: relative;
width: 122px;
}
#fileName::after {
content: attr(data-filetype);
position: absolute;
right: 0;
top: 0;
}
#fileName p {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
padding-right: 22px;
}
#fileName p::after {
content: '';
background: #FFF;
position: relative;
padding-right: 100%;
z-index: 1;
}
/*Show on hover*/
#fileName:hover {
width: auto;
}
/*Hide .txt on hover*/
#fileName:hover::after {
display: none;
}
<div id="fileName" data-filetype=".txt">
<p>This is the big name of my file.txt</p>
</div>
<div id="fileName" data-filetype=".txt">
<p>Short.txt</p>
</div>
Sample 3
Browser Compatibility Check: Supportive on IE 11, Edge, and Chrome.
The expansive content: ... complex string ...
on #fileName p::after
defines its width. This, combined with display: inline-block
, is presently the sole method effective on Edge / IE 11 as well as Chrome. However, display: inline-block
disables this mechanism on Firefox, leaving the ".txt" exposed for short filenames.
#fileName {
position: relative;
width: 122px;
}
#fileName::after {
content: attr(data-filetype);
position: absolute;
right: 0;
top: 0;
padding-right: 10px; /*Resolves Edge Browser Issue*/
}
#fileName p {
white-space: nowrap;
overflow: hidden;
padding-right: 22px;
text-overflow: ellipsis;
}
#fileName p::after {
content: '.........................................................................................................................';/*Resolves Edge Browser Issue*/
background: #FFF;
position: relative;
display: inline-block;/*Resolves Edge Browser Issue*/
z-index: 1;
color: #FFF;
}
/*Show on hover*/
#fileName:hover {
width: auto
}
#fileName:hover::after {
display: none;
}
<div id="fileName" data-filetype=".txt">
<p>This is the big name of my file.txt</p>
</div>
<div id="fileName" data-filetype=".txt">
<p>Short.txt</p>
</div>