Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to truncate a URL so that it fits within a layout

Tags:

html

css

What is the best way to truncate a URL when displaying it within a web page? I don't mean a link but literally displaying the URL as a value to the user, assuming that the text might be in a container of fixed width and you don't want to wrap or run outside of the container?

Is it better to truncate from the end, favouring the early part of the url:

eg. http/really.long/urlthaticantf...ere.html

Or place the '...' in the middle to favour the start and end of the link as the most value in terms of giving context:

eg. http/really.long/ur...aticantfithere.html

Also, what is a good rule of thumb when choosing how long to make the truncated URL? Should you be pessimistic and pick a likely wide-character such as capital 'M' and see how many of these fit in the layout? This tends to give really short URLs in general as most characters are much narrower than 'M'.

Or should you be optimistic and use a truncation that generally gives a good length but risk overrunning when the URL contains many large characters?

like image 616
Tom Martin Avatar asked Oct 15 '08 23:10

Tom Martin


People also ask

How do you handle a long URL?

There are several things that you can do to avoid URLs that are too long: If using dynamic URLs with URL parameters, use server-side URL rewrites to convert them into static, human-readable URLs. Try to minimize the number of parameters in the URL whenever possible.

How do you break a link in HTML?

The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems. The <br> tag is an empty tag which means that it has no end tag.

How do you write URL?

URLs should not use underscores, spaces, or any other characters to separate words. Use lowercase letters. In some cases, uppercase letters can cause issues with duplicate pages. For example, moz.com/Blog and moz.com/blog might be seen as two distinct URLs, which might create issues with duplicate content.


2 Answers

My preference is to display the most critical components of the URL. This is the file being requested and the domain of the request are what I consider critical, the intermediate path and the query string are things I consider non-critical.

So if you had http://www.Example.com/archives/2005/08/09/something.html, I would truncate it as www.Example.com/.../something.html

Of course, there are scenarios where this wont work. Take the URL of this page:

What's the best way to truncate a URL so that it fits within a layout

In this case, I would truncate the last portion of the URL to a reasonable number of characters (preferably breaking on a non-alpha), such as:

stackoverflow.com/.../whats-the-best...

like image 104
coderGeek Avatar answered Sep 20 '22 15:09

coderGeek


Truncate the middle, for the reasons you gave.

like image 35
Corey Trager Avatar answered Sep 19 '22 15:09

Corey Trager