Parent element with 'overflow: hidden' in CSS is causing child styles to be cropped out

.util-truncate {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

a {
  text-decoration: none;
}

a:focus {
  box-shadow: 0 0 0 3px blue;
  text-decoration: underline;
}

a:focus-visible {
  outline: 0;
}
<div class="util-truncate">
  <a href="#">This is a long link that will be truncated. This is a long link that will be truncated. This is a long link that will be truncated.</a>
</div>

Challenge:

In an attempt to add a ring using box-shadow around the anchor element when focused, I encountered an issue where the focus style was not fully visible due to the parent's overflow hidden property. The `truncate` class being a utility class restricts me from modifying its styles. Is there a pure CSS solution to ensure the focus style displays correctly for the anchor element?

The following methods were explored to address this focus state styling challenge:

  1. box-shadow
  2. outline (with offset)
  3. box-shadow with z-index
  4. box-shadow with inset (this approach closely resembles the desired output but does not match it exactly)
  5. Utilizing `before/after` pseudo-selectors

Solution:

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

Answer №1

To avoid cutting off the rendering, simply restrict the width of the a element. There's no need for a separate container div; just use display: inline-block to ensure it adheres to max-width.

a {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-decoration: none;
}

a:focus-within {
  box-shadow: 0 0 0 3px blue;
  text-decoration: underline;
}

a:focus-visible {
  outline: 0;
}
<a href="#">This is a long link that will be truncated. This is a long link that will be truncated. This is a long link that will be truncated.</a>

Answer №2

To implement the truncation effect, set

.util-truncate a { display: inline-block; max-width: 100%; }
and then include
.util-truncate a:focus { box-shadow:inset 0 0 0 3px blue; }
or utilize a pseudo-element:

.util-truncate {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.util-truncate a {
  text-decoration: none;
  display: inline-block;
  max-width: 100%;
  padding: 3px;
  box-sizing: border-box;
}

.util-truncate a:focus {
  box-shadow: inset 0 0 0 3px blue;
}
<div class="util-truncate">
  <a href="#">This is a long link that will be truncated. This is a long link that will be truncated. This is a long link that will be truncated.</a>
</div>

Answer №3

Adjust the line-height and incorporate an inset box-shadow

.util-truncate {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

a {
  text-decoration: none;
  line-height: 1.4;
  padding: 2px;
}

a:focus {
  box-shadow: 0 0 0 2px blue inset;
}

a:focus-visible {
  outline: 0;
}
<div class="util-truncate">
  <a href="#">This is a long link that will be truncated. This is a long link that will be truncated. This is a long link that will be truncated.</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

Is there a way to seamlessly transition between different Angular components without having to refresh the entire webpage?

I'm currently working on implementing a navigation bar that allows users to switch between three components without having the navbar reload. The goal is for only the new component to load when the user clicks on a different section of the navbar, kee ...

What is the process for updating the h1 header with text entered into an input field?

I'm working on an assignment that requires me to change the h1 heading to reflect whatever is entered into the input field. To accomplish this, I need to create a function using getElementByID. Here's what I've done so far: <!DOCTYPE htm ...

Show session in HTML using ng-click after reloading the page

I am currently using the express-session package to store product prices in my application. Everything seems to be functioning correctly, however I am encountering an issue when trying to display the session data in HTML. For example, if there are 2 produc ...

What are some strategies for handling analytics tracking in Flux architecture?

Imagine I'm working on a cutting-edge single page application similar to Airbnb. One essential aspect of such an application is keeping track of when someone signs up for an account. There are numerous services available to assist with tracking, incl ...

Encountering an error when attempting to access undefined property while using a method as a callback

Exploring OOP and angular is new to me. I am currently trying to implement a reusable table with pagination that triggers an API request when the page changes (pagination within the table component). The issue arises when I attempt to access my method usi ...

How can I stop the setinterval function in JavaScript?

My issue revolves around intervals. Upon declaring a function with setInterval, I find that even after clearing the interval, the function continues to execute. Here is my code: if (score == 1) { leftBlinkTimer(0) } else if (score == 0) { leftBlin ...

Is manually coding HTML for email necessary, or are there any practical tools or converters available?

Creating HTML emails is a different challenge compared to designing websites, as it requires working with tables instead of CSS. I'm in need of recommendations for tools specifically designed for HTML email creation, preferably from individuals who h ...

Vue is actively eliminating a background image through the use of the style attribute

There appears to be an issue where Vue is removing both the style attribute and the background image from this tag. <section style="background: url(images/banners/profiles/profile-banner-1.png);" class="profile-banner flex items-end md:items-end md:j ...

Issues involving the handleChange function in React can be a frustrating and common hurdle

I am encountering an issue with the handleChange function in my React project. Upon loading data from JSONPlaceholder and clicking on the 'Edit' button, a form is displayed with the selected user's information. However, when trying to edit ...

When utilizing the Express framework, the object req.body is initially empty when collecting data from a basic

I'm encountering an issue where I receive an empty object in req.body when submitting my HTML form. This problem persists whether testing in Postman or directly submitting the form from localhost in the browser. Upon logging it in the node console, t ...

Setting the initial date value for an Angular Material md-datepicker to a specific date

When using Angular Material md-datepicker, by default the system's current date is set as today's date and highlighted in the calendar. However, I am looking for a way to manually set any given date as today's date instead. Please see this i ...

Attempting to deploy my node.js application on Heroku resulted in an error message saying that the web process failed to bind to $PORT within 60 seconds of launch, causing the process to exit with status

I recently encountered an issue while attempting to deploy my node.js app on Heroku. The error message stated that the Web process failed to bind to $PORT within 60 seconds of launch, and the Process exited with status 137. I'm unsure of how to resolv ...

The Angular multi select tree feature seems to be malfunctioning

I recently incorporated a plugin called angular-multi-select-tree from GitHub into my project. Here is how I added it: First, I installed it via bower using the command bower install angular-multi-select-tree --save. This updated the bower.json file to i ...

Unable to get 100% height to work properly in HTML5 ASP.net because of the DOCTYPE

I'm currently working on creating an Asp.net website with a unique single-page portfolio style for the homepage. Each project or section should be 100% height of the viewport, stacked underneath each other to make use of anchor tags. However, I'm ...

Focus event in IE does not always work as expected after an ajax request is

Our current focus is on supporting IE8 exclusively. An ajax call retrieves data from the server, replaces the HTML in a container div with the response, and then attempts to focus on an element within the response. However, there seems to be inconsistenci ...

Experiencing difficulties in transmitting images/files to API through reactjs and Material UI upload component

Recently, I tackled the task of creating an image upload component by utilizing an upload component from Material UI. While I have experience with this process using a simple HTML file input in the past, I found myself feeling a bit perplexed this time aro ...

Obtain specific fields from a multidimensional array object using lodash

My dilemma involves working with an object that has the following structure: var data = [ { "inputDate":"2017-11-25T00:00:00.000Z", "billingCycle":6, "total":1 },{ "inputDate":"2017-11-28T00:00:00.000Z", "bi ...

What could be causing this minimal Angular - WebTorrent configuration to fail?

The setup appears to be quite straightforward. Check out the Webtorrent usage reference here This is how I have my setup: import WebTorrent from 'webtorrent'; @Component({ selector: 'app-root', standalone: true, template: `bla` ...

PHP failing to insert data

I'm facing an issue where my PHP code is not inserting any data. I have a form that displays values, but the update code doesn't seem to be working. Can someone please help me with this problem? Here is the PHP code snippet: if (isset($_POST[&ap ...

WordPress: Issue with Dropdown onchange Event Not Triggering

I'm having trouble getting an event to fire when the dropdown value changes. Can anyone help me figure out where I might be going wrong? JavaScript code jQuery(document).ready(function($){ jQuery('#_ccounts select').on('change&apo ...