{"id":12520,"date":"2015-08-03T04:28:00","date_gmt":"2015-08-03T04:28:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=12520"},"modified":"2025-07-07T07:38:24","modified_gmt":"2025-07-07T07:38:24","slug":"how-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically\/","title":{"rendered":"How to truncate a long text to fit in a fixed width grid cell dynamically"},"content":{"rendered":"\n<p>When developing a web page, we might have strings which are too long to fit in a fixed width cell of a table or Grid. The problem is more common when working with text in a nested grid. This might also occur when a web page is localized especially when dealing with languages that have longer text.<\/p>\n\n\n\n<p>Typically the requirement is that the text should fit within a single line and should not get wrapped to the next line in the cell (for any language or culture). Because the screen length of the text can vary from language to language, it\u2019s not straightforward to fix the limit of truncation. Below is an explanation of how it can be achieved along with code snippets.<\/p>\n\n\n\n<p>The function Truncate Text takes the following parameters as input:<\/p>\n\n\n\n<p class=\"blog-detail-list\">\u2022 Any text (any language as per the browser culture settings)<br>\u2022 Maximum width of the cell to fit the given text in<br>\u2022 Font name<br>\u2022 Font size<br>\u2022 Boolean value if the font is Bold<\/p>\n\n\n\n<p>Returns a string with the ellipses (\u2026) at the end by truncating the rest of the string.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"493\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate3.webp\" alt=\"\" class=\"wp-image-18462\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate3.webp 626w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate3-300x236.webp 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/figure><\/div>\n\n\n<p>The TruncateText function uses a custom function GetCharWidth which takes in a specific character with the font details as input and returns the width of that character.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"211\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate12.webp\" alt=\"\" class=\"wp-image-18463\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate12.webp 626w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate12-300x101.webp 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/figure><\/div>\n\n\n<p>The width of each character in the text provided as input is measured, summed up and compared to the maximum width allowed in the cell. The index of the character from which the text need to be truncated is captured and the substring of that text is taken till that index.<\/p>\n\n\n\n<p>The computation of the length of the string and the maximum width specified is calculated and the string is truncated to fit the cell width specified.<\/p>\n\n\n\n<p>Now we have the string\/link to fit the grid cell width specified.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing a web page, we might have strings which are too long to fit in a fixed width cell of a table or Grid. The problem is more common when working with text in a nested grid. This might also occur when a web page is localized especially when dealing with languages that have&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/how-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically\/\">Continue reading <span class=\"screen-reader-text\">How to truncate a long text to fit in a fixed width grid cell dynamically<\/span><\/a><\/p>\n","protected":false},"author":31,"featured_media":16772,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-12520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp","author_info":{"display_name":"Sree Navya","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/sreenavya\/"},"views":3787,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/How-to-truncate-a-long-text-to-fit-in-a-fixed-width-grid-cell-dynamically-graphic.webp",800,440,false]},"uagb_author_info":{"display_name":"Sree Navya","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/sreenavya\/"},"uagb_comment_info":0,"uagb_excerpt":"When developing a web page, we might have strings which are too long to fit in a fixed width cell of a table or Grid. The problem is more common when working with text in a nested grid. This might also occur when a web page is localized especially when dealing with languages that have&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=12520"}],"version-history":[{"count":3,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12520\/revisions"}],"predecessor-version":[{"id":23595,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12520\/revisions\/23595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16772"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=12520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=12520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=12520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}