Good day!

There is a container with a width of 100%. I would like to show in this container as many links as fit there without transferring to another line. Because the width of the container is unknown, I want to place there all the links that I have, but to hide those that do not fit completely into the container. That is, to avoid a situation where links will be cut off. To avoid a situation like:

Similar articles - how to cook cabbage, how to cook zucchini, how to cook beef

If the link to the beef does not fit, then do not show it, and not cut.

Is it possible to somehow implement it on css?

thank

  • Add an example of your code in which is cut off - Grundy
  • as an option - the text is cut off and dots are added to the end of the line - jsfiddle.net/soledar10/ynsdpv1z - soledar10

1 answer 1

content = document.querySelector('.content'); button = document.querySelectorAll('button'); button[0].onclick = function() {content.style = 'width: 430px';}; button[1].onclick = function() {content.style = 'width: 600px';}; 
 .content { width: 600px; background: #F0F4F8; padding: 4px; border-radius: 2px; height: 16px; overflow: hidden; } span a { text-decoration: none; color: #000; } span a:hover { text-decoration: underline; color: #61aceb; } 
 <div class='content'> <span>ΠŸΠΎΡ…ΠΎΠΆΠΈΠ΅ ΡΡ‚Π°Ρ‚ΡŒΠΈ: </span> <span><a href='#'>ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ капусту</a>,</span> <span><a href='#'>ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠ°Π±Π°Ρ‡ΠΊΠΈ</a>,</span> <span><a href='#'>ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ говядину</a></span> </div> <button>430 px</button> <button>600 px</button> 

Or by the example provided in the comment.

The key role is played by overflow: hidden and height: 16px in the main block and the specified display: inline-block parameter for the link

 div { overflow: hidden; background: #F0F4F8; padding: 4px; height: 16px; border-radius: 3px; margin-top: 64px; } div a { margin-left: 6px; text-decoration: none; color: #000; display: inline-block; } div a:hover { text-decoration: underline; color: #2c86d2; } 
 <div> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ капусту</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠ°Π±Π°Ρ‡ΠΊΠΈ</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠ°ΠΊΠ°Ρ…ΠΈ</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ Π±Π»ΠΈΠ½Ρ‹</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΏΠ»ΠΎΠ²</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ сырники</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ осьминога</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ Grundy</a> <a href="#">ΠΊΠ°ΠΊ Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ кСксики</a> </div> 

  • Well, thank you! About overflow: hidden; I understand, but why explicitly set height: 16px? - anacol
  • @anacol, if you remove hidden then you will see where the text goes, which did not fit. A height of 16px is equal to the height of one line - Mr. Black
  • why is the height of one line exactly 16? - anacol
  • @anacol, if div a has a font-size: 24px , then the block height will have to be adjusted to 24px. By default, the font size is set to 16px. Or adapt to the line-height - Mr. Black