Is there a way to limit the width of an inline anchor element?

I tried implementing the HTML tooltip solution from here and here is a snippet of my code:

<!DOCTYPE html>
<html>
  <head>
    <title>Page Title</title>
    <style>
      sup.footnote::after {
        content: attr(data-count);
      }
      sup.footnote:hover {
        cursor: help;
        position: relative;
      }
      sup.footnote cite {
        display: none;
      }
      sup.footnote:hover cite {
        display: inline-block;
        position: absolute;
        top: 0px;
        left: -50%;
        width: 250px;
        background: #f0f0f0 no-repeat 100% 5%;
        border: #c0c0c0 1px dotted;
        border-radius: 8px;
        margin: 10px;
        padding: 8px;
      }
    </style>
  </head>
  <body>
    <p>
      This is an example of a footnote in HTML
      <sup class="footnote" data-count="1">
        <cite>
          <span> [Sample span]</span>&nbsp;with some explanation:
          <a lang="en" href="http://test.gv.at/test.pdf">
            http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
          </a>
        </cite>
      </sup>
    </p>
  </body>
</html>

The issue I'm encountering is with limiting the length of the anchor element. While Firefox handles the anchor well, Chrome struggles. Here is an image: https://i.sstatic.net/6AOcL.png

I've tried solutions like this which suggests setting the display property for the anchor element, but it doesn't work in Chrome. I might be applying it to the wrong elements or with the wrong style.

==> Is there a way to restrict the length of the anchor to, for example, 250px?

Answer №1

There's an issue with your text (link) because it lacks spaces, making it a large word that CSS struggles to handle. This situation has also turned into a CSS meme.

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

For this problem, consider the following solutions:

Solution 1

To address this, you can simply apply the word-break property with the value break-word or break-all.

sup.footnote cite a {
  word-break: break-word;
}

sup.footnote::after {
  content: attr(data-count);
}
sup.footnote:hover {
  cursor: help;
  position: relative;
}
sup.footnote cite {
  display: none;
}
sup.footnote:hover cite {
  display: inline-block;
  position: absolute;
  top: 0px;
  left: -50%;
  width: 250px;
  background: #f0f0f0 no-repeat 100% 5%;
  border: #c0c0c0 1px dotted;
  border-radius: 8px;
  margin: 10px;
  padding: 8px;
}
sup.footnote cite a {
  word-break: break-word;
}
<p>
  This is an example of a footnote in HTML
  <sup class="footnote" data-count="1">
    <cite>
      <span> [Sample span]</span>&nbsp;with some explanation:
      <a lang="en" href="http://test.gv.at/test.pdf">
        http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
      </a>
    </cite>
  </sup>
</p>

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


Solution 2

Alternatively, you can prevent word breaks by using the text-overflow property to maintain tooltip height. Here's how:

sup.footnote::after {
  content: attr(data-count);
}
sup.footnote:hover {
  cursor: help;
  position: relative;
}
sup.footnote cite {
  display: none;
}
sup.footnote:hover cite {
  display: inline-block;
  position: absolute;
  top: 0px;
  left: -50%;
  width: 250px;
  background: #f0f0f0 no-repeat 100% 5%;
  border: #c0c0c0 1px dotted;
  border-radius: 8px;
  margin: 10px;
  padding: 8px;
  overflow: hidden;
}
sup.footnote cite a {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  display: block;
}
<p>
  This is an example of a footnote in HTML
  <sup class="footnote" data-count="1">
    <cite>
      <span> [Sample span]</span>&nbsp;with some explanation:
      <a lang="en" href="http://test.gv.at/test.pdf">
        http://test.gv.at/test/test/abde4/iabcde/009909asdf/vandsfk23/verylong/gghhy.pdf
      </a>
    </cite>
  </sup>
</p>

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

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

Retrieve Images from URL - Similar to Facebook Thumbnails

I am in the process of creating a website similar to "Reddit." Users will have the option to share URLs, and I would like to use PHP to extract the correct image from these links. What I'm looking for is a more advanced script that can retrieve imag ...

Understanding the behavior of the enter key in Angular and Material forms

When creating forms in my MEAN application, I include the following code: <form novalidate [formGroup]="thesisForm" enctype="multipart/form-data" (keydown.enter)="$event.preventDefault()" (keydown.shift.enter)="$ev ...

Refreshing the Table to Update Data

I am working on a jquery application that includes a table with time and number fields. My goal is to increment the values every 3 seconds by creating a function to update the numbers automatically. Initially, the data in the table looks like this: Kobe ...

Embed schema information into HTML tags using JavaScript

Is there a way to insert text, specifically schema data, into an HTML div tag using JavaScript? While I know how to modify existing values within a tag like class, href, and title, I am struggling to find a method to add something entirely new. Essentiall ...

Is there a way to exclude the Unicode character from the first menu item while still utilizing it in the other items on my menu?

I have been working on customizing my menu by using the unicode character \25C6 to represent a black solid diamond as a spacer between labels. After searching for solutions on Stack Overflow, I attempted to implement the suggested method using the bef ...

CSS declarations that have not been acknowledged or acknowledged

While working on a client's stylesheet today, I came across the following code: p { -webkit-hyphens: auto; -webkit-hyphenate-character: "\2010"; -webkit-hyphenate-limit-after: 1; -webkit-hyphenate-limit-before: 3; -moz-hyphens: manual; orphans: ...

Show the content of a div inside a tooltip message box in an MVC application

I have a MVC application where I need to show tooltip messages when hovering over my HTML div. HTML: <li class="Limenu" data-placement="right"> <span class="LessMenuspan btn-primary" pid="@i.ConsumerNo_" onmouseover="FacebookprofileTip(1, 0,thi ...

Dynamic resizing in NextJs does not trigger a re-render

My goal is to dynamically pass the width value to a component's styles. Everything works fine on initial load, but when I resize the window, the component fails to re-render even though the hook is functioning as intended. I came across some informat ...

Explore lengthy content within Angular 2 programming

I have a lengthy document consisting of 40000 words that I want to showcase in a visually appealing way, similar to HTML. I aim to include headers, paragraphs, and bold formatting for better readability. Currently, I am developing an Angular application. D ...

Exploring Java Programming with HTMLEditorKit

Here is my source code: I am trying to change the font color using only CSS. This is how I am inserting HTML: <span class="tag1">I love <span class="tag2">apple</span></span><span class="tag2"> pie</span> When using ...

How can I transfer a JavaScript value to PHP for storage in an SQL database?

<script> var latitudeVal = document.getElementById("latitude"); var longitudeVal = document.getElementById("longitude"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); ...

Enhancing text with custom gradient using CSS styling

I am facing an issue where uploading an image with text is not helpful for Google search visibility. I am looking to add a specific gradient style to my text. Could anyone assist me in achieving this particular text look that I have in mind? I came acros ...

Unable to make jQuery animate top function work

I have three rectangles set up like this. I've managed to make them disappear when clicking the close button on the right side of each rectangle, but I'm struggling with getting the remaining rectangles to move upward to fill the empty space. Her ...

I'm struggling to understand why the background-color is affecting the margins as well

Check out this code snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <style type="text/css"> * {border: 0px; padding: 0px;} ...

The page layout is completely messed up due to the floating right button

In my jQuery v3.3.1 / Bootstrap v4.1.2 application, I am attempting to add a button at the top right of the page. Please visit for credentials [email protected] 111111 The code I am using is as follows: <section class="card-body content_block_ad ...

Utilize the HTML canvas to sketch on an image that has been inserted

I have a HTML canvas element within my Ionic application. <canvas id="canvas" color="{{ color }}" width="800" height="600" style="position:relative;"></canvas> Within this canvas, I am loading an image. Below is the code snippet from the con ...

Creating a modern Bootstrap 4 navigation bar featuring two rows and a sleek inline form

I've tried experimenting with code from similar questions, but I'm still stuck! I've managed to get 2 flex rows in a flex column with the brand image vertically centered, but I'm struggling with the horizontal spacing. On the first row ...

The text exceeds the width of the paragraph

Something very odd is happening, a rare occurrence in my over 20 years of web development experience. The text within the paragraph seems to be overflowing its boundaries: https://i.sstatic.net/bmB1zg7U.jpg My client specifically requested the Duffy Scri ...

Problem with displaying JSF Bootstrap Glyphicons

I recently encountered a problem in my web app where my Glyphicons in Bootstrap suddenly stopped working, appearing as empty squares for no apparent reason. Even after ensuring that the font files were intact and replacing them with fresh ones from versi ...

The collapsible navigation bar vanishes when toggling in Bootstrap 4

My first time using Bootstrap 4, and I'm encountering a challenge that was much simpler in Bs3: I want the link items in my navbar to collapse into a hamburger menu when the screen size is xs. While the documentation and another answer touch on this, ...