css - 如何根据屏幕分辨率截断文本

标签 css

我想根据屏幕分辨率截断文本

目前我的 tpl 文件中的代码如下:

            {if $category->description}
            <div class="cat_desc">
            {if strlen($category->description) > 190}
                <p id="category_description_short">{$category->description|truncate:190}</p>
                <p id="category_description_full" style="display:none">{$category->description}</p>
                <a href="#" onclick="$('#category_description_short').hide(); $('#category_description_full').show(); $(this).hide(); return false;" class="lnk_more">{l s='More'}</a>
            {else}
                <p>{$category->description}</p>
            {/if}
            </div>
        {/if}

无论屏幕分辨率如何,此类代码都允许显示截断为 190 个字符的文本。

我想做以下事情:

  • 在大屏幕上显示 300 个字符
  • 在手机屏幕上显示 100 个字符

你知道如何实现这个目标吗。

我试图找到像这里描述的那样的 css 方式: https://css-tricks.com/flexbox-truncated-text/

但是使用上面的方法,文本总是被限制在一行。在大屏幕上,我希望文本分几行。

预先感谢您的阅读和任何帮助。

帕特里克

最佳答案

Im CSS 您可以使用例如:

// large screen
.textbox { 
    max-width: 600px; 
    word-wrap:break-word; 
}

// override when smaller screen
@media only screen and (max-width: 500px) {
    .textbox { 
        max-width: 300px; 
        word-wrap:break-word; 
    }
}

HTML

<p class="textbox">Text and text and text</p>

计算字符数,需要JS解决方案

关于css - 如何根据屏幕分辨率截断文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50720787/

相关文章:

css - 在某些缩放级别,下拉菜单在 IE7 中有透明间隙

html - 如何转义内联样式中的引号?

html - 为什么 Bootstrap 的 clearfix 类在这里不起作用?

javascript - 突出显示事件菜单项作为页面滚动 div(侧边栏滚动菜单)

css - IE7 中的表 inline-block/inline-table

css - div 仅在一侧溢出?

css - HTML5 Boilerplate 在网站底部留下一个空白

html - Bootstrap 4 中容器内的全宽图像

css - div 背景仅向左重复

css - React - 基于 View 更改组件中文本的颜色