javascript - Shortening a link using HTMLJSCSS - Stack Overflow

I am trying to change the design of the navigation on my site.We have some products with really long na

I am trying to change the design of the navigation on my site.

We have some products with really long names and I want to cut them short and maybe add (...) or something similar at the end.

So something like this should look like abcdefg... instead of abcdefghijklmnopqrstuvwxyz

a{
  width:50px;
  overflow:hidden;
}
<a href="#">abcdefghijklmnopqrstuvwxyz</a>

I am trying to change the design of the navigation on my site.

We have some products with really long names and I want to cut them short and maybe add (...) or something similar at the end.

So something like this should look like abcdefg... instead of abcdefghijklmnopqrstuvwxyz

a{
  width:50px;
  overflow:hidden;
}
<a href="#">abcdefghijklmnopqrstuvwxyz</a>

A JS solution is wele.

I would also like to know why the width isn't being applied?

Share Improve this question edited Apr 17, 2019 at 19:22 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 7, 2019 at 14:48 Paddy HallihanPaddy Hallihan 1,7065 gold badges34 silver badges90 bronze badges 1
  • w3schools./cssref/css3_pr_text-overflow.asp also anchor by default displays inline so add display: block / inline-block if you want overflow property to be used – digital-pollution Commented Mar 7, 2019 at 14:50
Add a ment  | 

2 Answers 2

Reset to default 7

Use white-space bined with overflow & text-overflow. And don't forget to add display: inline-block to the a element, so you can apply width to it.

a {
  display: inline-block;
  width: 50px;
  white-space: nowrap; 
  overflow: hidden;
  text-overflow: ellipsis;
}
<a href="#">abcdefghijklmnopqrstuvwxyz</a>

Anchors are inline elements by default and any width set on an anchor is ignored. Change the display to inline-block:

a {
  width: 50px;
  overflow: hidden;
  display: inline-block;
}
<a href="#">abcdefghijklmnopqrstuvwxyz</a>

As MDN states:

Inline elements are those which only occupy the space bounded by the tags defining the element, instead of breaking the flow of the content.

and...

You can change the visual presentation of an element using the CSS display property. For example, by changing the value of display from "inline" to "block", you can tell the browser to render the inline element in a block box rather than an inline box, and vice versa. Hovewer, doing this will not change the category and the content model of the element. For example, even if the display of the span element is changed to "block", it still would not allow to nest a div element inside it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745397235a4625935.html

相关推荐

  • javascript - Shortening a link using HTMLJSCSS - Stack Overflow

    I am trying to change the design of the navigation on my site.We have some products with really long na

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信