底漆 CSS 截斷自訂最大寬度
在網路開發專案中,開發者會遇到由於客戶需求、整體外觀、資源有限等多種原因,需要在指定的空間內顯示文字的情況,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)
