底漆 CSS 截断自定义最大宽度
在Web开发项目中,开发者会遇到由于客户需求、整体外观、资源有限等多种原因,需要在指定的空间内显示文本的情况,truncate属性是CSS 中的一个有效功能可以解决这个问题。
它使开发人员能够显示单行文本并用省略号截断溢出的文本。但是,根据具体情况,可能需要自定义截断文本的最大宽度。在本文中,我们将讨论如何使用 Primer CSS(一个由 GitHub 设计系统设计的流行开源 CSS 框架)来自定义最大宽度。
截断是什么意思?
在网页设计中,truncate是CSS的text-overflow属性的值之一。在处理文本时,经常会遇到容器不足以容纳文本的情况。这种文本称为溢出文本。它使我们能够显示一行文本,然后用省略号截断其余部分。
在CSS中,为了使用“截断”,你必须执行以下步骤 -
将“white-space”属性设置为nowrap
将溢出属性设置为隐藏
将text-overflow属性设置为省略号
示例
<html> <head> <style> div { width: 77%; height: 30px; border: 1px solid black; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } </style> </head> <body> <div> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </div> </body> </html>
为了避免这么多行代码,您可以使用 Primer CSS 来代替。 Primer CSS 内置了一个截断组件。它具有相同的预定义类。
在使用 Primer CSS 中的任何类之前,我们必须从 npm 安装它 -
npm install --save @primer/css
或在 HTML 代码中使用 CDN 链接 -
<link href= "https://unpkg.com/@primer/css@^20.2.4/dist/primer.css" rel= "stylesheet" />
自定义截断文本的最大宽度
为了自定义截断文本的最大宽度,Primer CSS 提供了预定义的类,用于截断网站中溢出的文本。
示例
在此示例中,我们使用预定义的框类将 div 元素转换为可调整大小的框。这里,p-1是一个类简写,用于在框的所有边上添加 4px (0.25 rem) 的填充。
接下来,我们有 style 属性,用于将所需的样式添加到框中。我们将resize属性的值设置为horizontal,以便用户只需从右角拖动它即可水平调整框的大小。为了向元素添加水平滚动条,我们使用了“overflow:scroll”属性。使用水平滚动条将使用户能够在水平滚动文本时看到隐藏的内容。
然后,我们使用 Primer CSS 中的预定义类显示具有不同最大宽度的不同截断文本。
<html> <head> <link rel="stylesheet" href="https://unpkg.com/@primer/css@^20.2.4/dist/primer.css" /> </head> <body> <h1 style="margin: 10px"> Primer CSS Truncate Custom Max Width </h1> <p style="margin: 10px"> Following we have different truncated text with customized maximum widths. </p> <br> <div class="Box p-1" style="resize: horizontal; overflow: scroll; margin: 10px"> <div class="Truncate"> <span class="Truncate-text Truncate-text--expandable" style="max-width: 460px;"> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </span> </div> <br> <div class="Truncate"> <span class="Truncate-text Truncate-text--expandable" style="max-width: 340px;"> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </span> </div> <br> <div class="Truncate"> <span class="Truncate-text Truncate-text--expandable" style="max-width: 280px;"> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </span> </div> <br> <div class="Truncate"> <span class="Truncate-text Truncate-text--expandable" style="max-width: 180px;"> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </span> </div> <br> <div class="Truncate"> <span class="Truncate-text Truncate-text--expandable" style="max-width: 80px;"> This is the text. Apple mango banana watermelon orange kiwi pomegranate muskmelon pineapple grapes papaya guava strawberries raspberry avocado pear. </span> </div> </div> </body> </html>
结论
在内容溢出的情况下自定义显示文本的最大宽度是一种很好的做法,可以帮助开发人员控制网站上有限空间内的文本显示。按照本文讨论的方法,您将能够创建具有视觉吸引力的网站。我们在卡片设计中也可以使用截断方法。这使您的内容更具可读性和用户友好性,同时您必须显示长标题、标题、卡片描述等,
以上是底漆 CSS 截断自定义最大宽度的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Autoprefixer是一个根据目标浏览器范围自动为CSS属性添加厂商前缀的工具。1.它解决了手动维护前缀易出错的问题;2.通过PostCSS插件形式工作,解析CSS、分析需加前缀的属性、依配置生成代码;3.使用步骤包括安装插件、设置browserslist、在构建流程中启用;4.注意事项有不手动加前缀、保持配置更新、非所有属性都加前缀、建议配合预处理器使用。

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

创建CSS加载旋转器的方法有三种:1.使用边框的基本旋转器,通过HTML和CSS实现简单动画;2.使用多个点的自定义旋转器,通过不同延迟时间实现跳动效果;3.在按钮中添加旋转器,通过JavaScript切换类来显示加载状态。每种方法都强调了设计细节如颜色、大小、可访问性和性能优化的重要性,以提升用户体验。

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

要创建内在响应式网格布局,核心方法是使用CSSGrid的repeat(auto-fit,minmax())模式;1.设置grid-template-columns:repeat(auto-fit,minmax(200px,1fr))让浏览器自动调整列数并限制每列最小和最大宽度;2.使用gap控制格子间距;3.容器应设为相对单位如width:100%、配合box-sizing:border-box避免宽度计算错误并用margin:auto居中;4.可选设置行高与内容对齐方式提升视觉一致性,如row

要让整个网格布局在视口中居中显示,可通过以下方法实现:1.使用margin:0auto实现水平居中,需设定容器固定宽度,适用于固定布局;2.利用Flexbox在外层容器设置justify-content和align-items属性,结合min-height:100vh可实现垂直和水平居中,适合全屏展示场景;3.直接使用CSSGrid的place-items属性在父容器上快速居中,简洁且现代浏览器支持良好,同时需确保父容器有足够高度。每种方式均有适用场景和限制,根据实际需求选择合适的方案即可。

prainuredetectionIncsssusissuse@supportScheckSifabRowsEsuppecifortSupecifortEfeatureBeforeApplyingReplyingStyles.1.itusesconditionalcsssssbasssbasedonproperty-valueperty-valuepairs,suessas@supports@supports@supports@supports(display:grid)
