Good day.

There is such code:

<div class="below-title-meta"> <div class="adt"> <img src="...img/date.png" width="12" height="13" alt="Дата"> <span class="date updated">00.00.0000</span> | <span> <img src="...img/category.png" width="14" height="13" alt="Рубрика"> <a href="https://info/" rel="category tag">Название рубрики</a><span style="display:none;" class="vcard author"><span class="fn">456</span></span> </span> <div class="adt-comment"> <img class="link-comments" src="...img/comments.png" width="15" height="14" alt="Комментарии"> <a class="link-comments" href="https://">Нет комментариев</a> </div></div> </div> .below-title-meta{ background: #F7F7F7; padding-bottom:26px; color:#a2a2a2; line-height:1.9; width:106.3%; margin-left:-3.1%; } .below-title-meta a{ color:#666666; text-decoration:none; } .adt{float:left;padding-left:22px;} .adt-comment{float:right;padding-right:16px;} 

https://jsfiddle.net/9n4ebw74/

There are no comments on the right side, and the name of the rubric (rubrics) on the left. Rubber website design. What should be prescribed so that when the title of the rubric (rubrics) approaches No comments , the latter was not transferred to a new line, and the title of the rubric itself was shortened by ellipsis.

If this is depicted in the picture, then like this:

enter image description here

    1 answer 1

    First of all, do not forget that any “rubber” design should still have a minimum width, after which it either ceases to taper or changes its appearance for mobile devices. As a matter of fact, you have two options for how to solve the problem with cutting the text.

    Option 1: Beckend solution

    It's all quite simple. Depending on what language you use, you cut the name of the rubric so that it fits into your field. For example, the code in php:

     // обрежем текст на определённое количество $string = substr($string, 0, 200); // убедимся, что текст не заканчивается знаками препинания $string = rtrim($string, "!,.-"); // находим последний пробел, устраняем его и ставим многоточие $string = substr($string, 0, strrpos($string, ' ')); // выводим echo $string."… "; 

    Option 2: Front End Solution

    There may be several sub-options.

    • use css
    • use jss

    I would suggest discarding the first sub-option, since it will eventually lead to unpredictable consequences in some cases. Anyway, leaving the text simply invisible is not a very good decision. But in general, it is done this way:

     p.clip { white-space: nowrap; /* Запрещаем перенос строк */ overflow: hidden; /* Обрезаем все, что не помещается в область */ background: #fc0; /* Цвет фона */ padding: 5px; /* Поля вокруг текста */ text-overflow: ellipsis; /* Добавляем многоточие */ } 

    As you can see, it is solved quite simply, but there is no check for punctuation, do not remove the last space, and indeed different browsers support css differently.

    The second sub-option is more optimal. On the one hand, it is similar to the first solution, on the backend side, on the other hand, it is more flexible, because it allows you to cut text dynamically, depending on the size of the user's screen and, accordingly, depending on the width of the block where the text should be.

    The easiest option, when you do not have the required length, is:

     var sliced = text.slice(0,10); if (sliced.length < text.length) { sliced += '...'; } 

    Here we cut the text, and if it is longer than the specified length, then add an ellipsis. You can add it with the same treatments as in the example about php, on your own. Well, the most difficult thing is to determine the size of the block under which to tune your text, and then calculate the required number of characters. It all depends a lot on your code, I can only say that two functions of vanilla js will definitely help you:

     // возьмем элемент, ширина которого нас интересует var test = document.getElementById("test"); // узнаем его длину var width = (test.clientWidth) + "px"; 

    Further, I think everything is clear.

    • Thank you for such a comprehensive response. This is a line with categories indicating to which category (categories) an article has been added. Rubber design. Because perfect and optimal way to CSS. The problem is that the CSS construction does not work, and I have already tried it before jsfiddle.net/9n4ebw74/1 . Most likely this happens "No Comments". This is the key problem and I would like to solve it. - UserUser-name
    • "Most likely this is happening" No Comments "" - explain, do not understand - Stanislav Belichenko
    • In general, it is imperative that even rubber designs give a minimum width. I will bring it back, an important point too - Stanislav Belichenko
    • There is a block, on the one hand, of the category, and on the other, “No comments” - this is clearly seen in the live example. And the headlines may and should be limited, but in this case it is clickable category names - they perform the navigation function. It’s better not to limit them, but if the user has opened a page, for example, on a mobile, then there is no way out. - UserUser-name
    • But the layout of the entire page jsfiddle.net/u0t72dgh/4 Now there is a transfer .. but I would like, as in the above picture in the first post - not fit category (categories) ended in ellipsis. - UserUser-name