Issue with resizing a Bootstrap pill containing truncated text

After struggling with this issue for some time, I attempted to use javascript to solve it but have been unsuccessful. The challenge involves a tag list in a table that displays a link when hovered over. Following advice from another member on SO, I managed to further truncate the text in order to make room for the 'x'. My end goal is to maintain the size of the pill while displaying both the truncated text and the link.

To confirm that the badge itself is causing extra white space when hovered over, I placed the relevant div and link within a border. Unfortunately, I have not been able to remove this unwanted space. Any suggestions would be greatly appreciated!

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

html:

.badge:not(:hover)>.tag-remove {
  display: none;
}

.badge:hover>.truncate {
  width: 60%;
  margin-left: -3px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: inline-block;
}

.badge:hover>.tag-remove {
  color: #cc0000;
  font-size: 14px;
  margin-right: -1px;
  white-space: nowrap;
  text-decoration: none !important;
}

.badge-color {
  background-color: #A4A4A4;
  color: #FFFFFF;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="31535e5e45424543504171041f031f01">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>


<div class="badge badge-color">
  <div class='truncate'>Long Tag Title</div>
  <a class="tag-remove" data-confirm="Are you sure you want to delete the tag?" data-remote="true" href="/contacts/100/remove/26">x</a>
</div>

Answer №1

It's recommended to set most of your styles regardless of hover state for a smoother transition. This way, there will be minimal changes when hovering that could potentially affect the badge size or position.

If you want to shrink the badge label when hovering, adjust the width property. Utilize a negative margin to correctly position the "remove" link.

.badge:not(:hover) > .tag-remove {
  display: none;
}

.badge {
  display: inline-flex !important;
  align-items: center;
  justify-content: space-between;
}

.truncate {
  display: block;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge:hover > .truncate {
  width: calc(100% - 1em);
}

.tag-remove {
  margin-left: -1em;
  width: 1em;
  color: #cc0000;
  font-size: 14px;
  line-height: 0;
  text-align: right;
  text-decoration: none !important;
}

.badge-color {
  background-color: #a4a4a4;
  color: #ffffff;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05676a6a71767177647545302b372b35">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="badge badge-color">
  <div class="truncate">Long Tag Title</div>
  <a class="tag-remove" data-confirm="Are you sure you want to delete the tag?" data-remote="true" href="/contacts/100/remove/26">x</a>
</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

Perform different actions based on the state of a Vue JS checkbox: "Do This" when checked and

Attempting to create a checkbox that triggers one function when initially checked, and another when it is unchecked. I have tried the following approach. There is also a variable that has been read out and reflects the current status: <input type=" ...

CSS3 blending modes

I am attempting to create a design similar to this letter on an image using blending modes. Check out my code example <div id="box"> <div id="image"> <img src="https://www.epicurrence.com/images/speakers/julie.jpg" /> </div&g ...

How can I adjust the width of a handle/thumb for NoUiSlider?

Looking to adjust the width of a NoUiSlider using CSS: .noUi-horizontal .noUi-handle { width:8px; height:25px; left: 0px; top: -8px; border: 0px solid #000000; border-radius: 0px; background: #000; cursor: default; box- ...

Can you tell me which specific font Adobe Edge Code (Brackets) uses in its code editor?

Can someone please help me? I'm trying to identify the syntax highlighter being used in this code. Is it Prettify or something else? ...

Positioning the HTML form in the middle of the page

The given HTML code generates a form within a table, but despite aligning the table to the center, it remains on the left side of the webpage. <!DOCTYPE html> <html> <head> <style>input.textfill { float: right; }&l ...

Modifying the input field's name attribute while utilizing multiple datasets in typeahead.js

I am currently utilizing typeahead.js with multiple datasets, following the guidance provided here. I have chosen not to employ Bloodhound in my implementation, resulting in some differences in my code structure, outlined below: $('#search .typeahead ...

Is Fullpage.js functioning only on local servers?

I have decided to create a personal website showcasing my portfolio using the fullpage.js library. Everything seems to be working fine during development, but once I upload the site to my github.io or another public domain via FTP, I encounter GET errors t ...

Show a pop-up notification for a compact disc

Received a CD containing various tutorials created in HTML. I am looking to have a browser window open with specific parameters, such as no toolbars and fixed width/height, to properly display the content. Using window.open with multiple parameters trigge ...

Achieving this result in HTML using a jQuery accordion component

Hey there, I'm looking to create a full-height accordion that extends from the bottom to the top of the page, similar to what you see on this website: . I also have a footer positioned below the accordion. The content should load when the page loads ...

After applying the rotateY function, the z-index appears to be malfunctioning

My current project involves creating a page flip effect, which requires two overlapping div elements. Initially, both divs are side by side with the second div having a -webkit-translateY(180deg) property applied to it. The first div has a z-index of 2, wh ...

Transferring information from an HTML page to a PHP script

I am facing a dilemma with my current PHP script that is called from an HTML link. The setup requires the script to accept non-user input from the page, specifically static data already present. The line calling the script appears like this. <div class ...

``Need help hosting static HTML files on Google App Engine? Here's a step-by-step

Can I use App Engine to host a static HTML website? How can I set up my domain name to work with it? ...

Retrieve information from MongoDB and display it in an HTML table

I have some data that follows this structure: { "jobname" : "2018_09_27_test123_0", "totalcalls" : 1836.0, "success" : 3.0, "fail" : 1833.0, "failureReasons" : [ { "reason" : "Unavailable", "count" : 53 ...

Issue with data type not refreshing in mySQL Workbench

I'm facing an issue with changing the datatype of my "first_name" column. I want it to be VARCHAR(45), but it's stuck at INT(11) as shown in the table diagram and connection provided below. https://i.sstatic.net/0CbkD.png https://i.sstatic.net/8 ...

Retrieve JSON data by making a POST request to a website's API

Can you help me retrieve Json data from a website API using JavaScript? I'm trying to fetch quotes from the following website: for my quotes generator page. While I have some understanding of making GET requests, it seems that this API requires POST ...

Pictures failing to display in their designated location

Having trouble with my social media icons on the page. They either get hidden by other divs despite having a higher z-index or appear below the top section. I want them to be positioned to the right of the church image. I've tried different approaches ...

set available date with angular bootstrap datetimepicker

Currently, I am utilizing an angular bootstrap datetimepicker plugin that can be found at https://github.com/dalelotts/angular-bootstrap-datetimepicker. My goal is to configure the calendar so that only dates within the range of today and seven days in t ...

What is the best way to ensure the footer is always positioned at the bottom of each page

Hey there, I'm having an issue with printing a large table on my HTML page. I want the footer to appear at the very bottom of every printed page, but so far I haven't found a perfect solution. I came across using tfoot, which prints the footer at ...

Ways to stop VoiceOver from selecting the background content when a modal is open

Is there a way to prevent VoiceOver from reading the content behind a modal? I tried using aria-modal=true, but it seems VoiceOver does not support this feature by default like NVDA and JAWS do. I found more information about this on . According to the in ...

Is there a way to show the input value in a different form while still retaining the original value?

When receiving a pagination number from user input, I store it in a variable like so: $html .= "<input type='submit' name='next' value='$next_page_number' />"; Now, I'm looking to replace the displayed text for th ...