I have a list in SharePoint that has numbers of columns with very long headers and I’d like to wrap these long headers so that I can have more columns shown on the screen. By default, I couldn’t. But with a little nice trick, I can.
The reason why I couldn’t by default because the default CSS style sheet the list uses has white-space: nowrap in place that prevents anything from wrapping. If we want the header wrapped we need to find a way to overwrite the white-space style with normal instead.
So here is how I did it, inspired by Ryan here.
Go to my list, select Site Actions and choose Edit Page.
Click Add a web part, then select Media and Content and Content Editor, and click Add to add the selected web part to the current page.
Select the newly added web part, and select HTML → Edit HTML Source from the ribbon.
And paste in the following style codes.
<style type="text/css"> .ms-vh, .ms-vh2-nofilter, .ms-vh2-nograd, .ms-vh2, .ms-vb, .ms-vb2 { white-space: normal; text-align:center; vertical-align:text-bottom; color:red; } </style>
Click OK to save the change. Refresh the page and done.
It’s a very nice and useful trick in SharePoint because, with this added style web part, I can actually customize the style sheet to have the list displaying in a way I really wanted.
You can also accomplish the same with SharePoint Designer if you have one handy.
This code applies to the column contents as well. I just want to wrap and center my column titles, not the data content.
great – this fixed a massive problem for me – thanks!!
It doesn’t work with SP2013
I used this however it causes my ribbon with “Items” & “List” to disappear. Why is that so?
Yes, that’s because you added the web part for CSS style on the page. I believe you can click inside the list to have the items & lists menu to show up again.
Known issue… When you add a web part to a list page SP doesn’t display those tabs anymore…
There are permanent fixes out there e.g.: http://www.pentalogic.net/sharepoint-products/free-stuff/view-rescue
A quick-fix way to see these ‘lost’ tabs I found is to…
– click in the List/library search dialog (not directly on the magnifying glass though) or
– select any of the list/library items,
– click to the right of the list (assuming the columns don’t fill the width of the screen)
the tabs should now appear, but this is not a permanent fix, you’ll have to do this every time the list reloads.
Hello I would like for it to wrap smaller than it did, how would I do that?
Maybe, set the text-size to a smaller value, i.e. 9pt?
Content Editor Web Part was added above the Allitems.aspx or SP list items for my site. I added the script above to the html source which worked beautifully but after I applied the changes. The Allitems.aspx items (list items containing 730 records) are no longer showing. Can you help please?
Good post for on-prem, but was looking for something for SPO. Came across this post and it works well using JSON, try out https://code2care.org/q/how-to-wrap-column-text-in-sharepoint-online-modern-list-grid-view-using-json-formatting