Forum Discussion
HOW TO: Hide web part when it's empty
smithme There's usually an option to hide the web part if there's nothing to show. It's definitely on 'News', although maybe not on every web part.
- Fredrik_FjllstrmJan 22, 2024Copper ContributorI would add a CSS class of hidden that only has display:none and add that class to whatever element I want to hide if the webpart is empty.
- mcadsFeb 15, 2024Copper Contributor
Fredrik_Fjllstrm Can you post an example of how to do this? I'm having the same issue.
Thanks so much!
- nogeekleftbehindFeb 26, 2024Copper Contributor
mcads I wondered that exact question myself and asked Microsoft Copilot. Here's what it said:
Certainly! To conditionally hide an element within a SharePoint Online page using CSS, you can create a custom CSS class with the property display: none. Then, apply this class to the desired element based on specific conditions (such as whether a web part is empty). Here’s how you can achieve this:
Create a Custom CSS Class:
- You can define a CSS class that sets the display property to none. This class will be used to hide the element.
- For example, let’s create a class called .custom-hidden:
.custom-hidden { display: none; }
Identify the Element to Hide:
- Determine which element you want to hide. It could be a specific div, section, or any other HTML element within your SharePoint page.
Apply the Custom Class Conditionally:
- Depending on your scenario (e.g., if a web part is empty), you can add the .custom-hidden class to the element.
- You can do this dynamically using JavaScript or directly in your HTML markup.
Example Usage:
- Let’s say you want to hide a specific div with the ID myElement when a web part is empty:
<div id="myElement" class="custom-hidden"> <!-- Content of the element --> </div>
- If the web part is empty, the myElement div will be hidden due to the applied .custom-hidden class.
- Let’s say you want to hide a specific div with the ID myElement when a web part is empty:
Content Editor Web Part (Classic):
- If you’re working with classic pages, you can use a Content Editor Web Part to inject your custom CSS directly into the page.
- Add the CSS snippet within the Content Editor Web Part to hide the desired element.
Modern Pages:
- In modern pages, you can use the Script Editor Web Part (if available) or the SPFx (SharePoint Framework) to inject custom CSS.
- Alternatively, you can link an external CSS file using the File Viewer Web Part or SPFx Application Customizer.
Remember to adjust the class name (custom-hidden) and the element ID (myElement) according to your specific use case. Additionally, consider using the appropriate method based on whether you’re working with classic or modern SharePoint pages. 🌐:artist_palette:
References:
- https://www.c-sharpcorner.com/article/hide-sharepoint-online-page-elements-using-css/https://www.c-sharpcorner.com/article/hide-sharepoint-online-page-elements-using-css/.
- https://sharepoint.stackexchange.com/questions/253550/hide-element-using-css-for-a-modern-pagehttps://sharepoint.stackexchange.com/questions/253550/hide-element-using-css-for-a-modern-page.
- https://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynonehttps://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone.