CSS property that determines where a string can be broken into lines based on specific characters

Is it possible to specify characters on which a long string, such as a URL, should break in browsers?

For instance:

https://www.this-is-my-url.org/upload_dir/2015/01/foo_bar_faq.pdf

If the box only allows 40 characters per line, it might break like this:

                                        | <- this is 40 chars mark
https://www.this-is-my-
url.org/upload_dir/2015/01/foo_bar_faq.pdf

The last line could even overflow because there is no “breakable char.” I am seeking a CSS property that accomplishes this, like:

p.url {
  word-break-chars: "_/.";
}

This way, the URL could break on underscores, slashes, or periods and look more like:

                                        |
https://www.this-is-my-url.org/upload_
dir/2015/01/foo_bar_faq.pdf

Using word-break: break-all; is not an option for me because it looks really bad:

                                        |
https://www.this-is-my-url.org/upload_di
r/2015/01/foo_bar_faq.pdf

Answer №1

Perhaps you could consider utilizing the CSS property text-overflow: clip;

p{
    white-space: nowrap; 
    width: 12em; 
    overflow-x: auto;
    text-overflow: clip; 
    border: 1px solid #000000;
}
<p title="https://www.my-new-url.org/upload_dir/2016/04/fancy_faq.pdf">https://www.my-new-url.org/upload_dir/2016/04/fancy_faq.pdf</p>

Answer №2

Thanks to a helpful tip from JavaForAndroid, I was able to devise this clever jQuery solution:

$( document ).ready(
    $('.download a').each(function(){
        var newText = $(this).text().replace('_', '_\u200B')
                .replace('/', '/\u200B')
                .replace('.', '.\u200B');

        $(this).text(newText);
    })
);

Here, the mysterious code \u200B represents the 'Zero Width Space' Unicode character.

The structure of the HTML is as follows:

<div class="download">
    <a href="https://www.this-is-my-url.org/upload_dir/2015/01/foo_bar_faq.pdf">
        https://www.this-is-my-url.org/upload_dir/2015/01/foo_bar_faq.pdf
    </a>
    <a href="https://www.this-is-my-url.org/upload_dir/2014/11/foo_bar.pdf">
        https://www.this-is-my-url.org/upload_dir/2014/11/foo_bar.pdf
    </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

The alert dialog does not appear when clicking on "a"

Attempting to retrieve the ID of the clicked element using jQuery has proven unsuccessful for me. Below is the jQuery code I am working with: <script> $(function(){ $("a.step").click(function(){ var id = $(this).attr('id'); ...

Adjusting the height of the v-footer

I'm having trouble adjusting the height of the v-footer component. According to the documentation, I should be able to use the height prop, but when I try height="100px" or height="50%", the footer doesn't change. What could be causing this issue ...

What is the best way to retrieve the value of the nearest text input using JavaScript?

I am currently working on designing an HTML table that includes a time picker in one column for the start time and another time picker in a separate column for the end time within the same row. My goal is to retrieve both values (start time and end time), ...

Refreshing CSS styles following adding content with jQuery

When I load a DIV with content from a callback that contains multiple HTML elements, each with their own associated classes, the styles are not appearing correctly after using .append. $.ajax({ type: "GET", url: "blah.xml", dataType: "xml", ...

Unspecified origins of Js in Chrome Extension

console.log(chrome.runtime.sendMessage({from:"script2",message:"hello!"})); However, attempting to send the message from a background script to a content script is proving to be unsuccessful. https://i.stack.imgur.com/ERgJB.png ...

Utilizing Jquery to pass two classes along

I am looking for assistance on how to pass multiple classes within if-else statements and jQuery. My goal is to have both divs and divs2 change when the next button is clicked. Can someone please provide guidance on achieving this? --code not mine-- ...

Through the implementation of JavaScript, the element's class is dynamically altered

While working on my project, I encountered an issue with implementing a change in the class of an element using JavaScript. The problem is that when I click on the home page, the last page does not get deselected. Here is my current JavaScript code: const ...

PHP script not refreshing properly on reload causing randomized content to display inaccurately

In an attempt to create a dynamic website that features a random image on each page refresh, I have stored all images in a directory named "/pngs/" and set up a MySQL database with indexed rows for reference: <img src="pngs/000.png"> <img src="pn ...

Is it possible to implement a code that will organize a dropdown list alphabetically?

Could someone assist me in sorting the location items alphabetically in this search form at: I would like the locations to be displayed in alphabetical order. Any help with updating the code below would be greatly appreciated. Here is the current code sn ...

Switch the orientation of a live table moving horizontally to vertically and vice versa

config.previewData = [ { Products:27989, Total Customers:294, Metrics:"MVC", Toner Products:5928, INK Products:22061 }, { Products:56511, Total Customers:376, Metrics:"SMB", ...

The external function in HTML Form's onsubmit attribute is failing to execute

My HTML Form has an onsubmit event that should call a validate() function. Everything works fine when the validate() function is inside a script tag at the end of the body tag. But if the validate() function is in an external JavaScript file, like in thi ...

Using Javascript to iterate through and increase HTML code with values all the way up to 55

I am looking for a way to automatically generate a list of links in an HTML page using JavaScript. I have tried a few scripts, but none have worked for me. This is the current structure of my HTML... <a href="1.html"><img src="images/1.jpg" widt ...

Tips for incorporating a background color into a specific section

Code: .headline { font: 150 50px/0.8 'Pacifico', Helvetica, sans-serif; color: #2b2b2b; text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.1), 7px 7px 0px rgba(0, 0, 0, 0.05); text-align: center; } <link href='http://fonts.googleapis.com ...

Error in Javascript caused by the use of Jquery's "hide on load" feature

Looking for help with creating a menu where the subitems disappear when the page loads. If you require the full HTML code, I can provide it, but essentially it consists of several DIVs, with the items under headers having a class of subitems. I was able ...

Controlling div elements using javascript

Is there a way to help me understand or identify my mistake? I seem to have an issue in my javascript and html code related to gathering user input for a flight. I'm trying to create a counter function to tally all flights (function counter()), but it ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

Utilize JavaScript to compute and implement a deeper shade of background color

To dynamically apply darker shades of background using JavaScript, I have devised the following code. .event-list .bg{ background:#eee; padding:5px; } .grid .event-list:first-child .bg{ background: #2aac97 } .grid .event-list:nth-child(2) .bg{ backgrou ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Displaying various charts in a single view without the need for scrolling in HTML

How can I display the first chart larger and all subsequent charts together in one window without requiring scrolling? This will eventually be viewed on a larger screen where everything will fit perfectly. Any recommendations on how to achieve this? Here ...

Switch the navbar background color from see-through to black

On my website, I have set up a navbar navigation that transitions between a transparent background and a black background as the user scrolls down. However, I would like the black background to be present at all times for the navbar. I built the website us ...