How to provide a good experience in the page editor
The page editor provides an inline editing experience to help content authors create and edit their content in a what you see is what you get (WYSIWYG) fashion. To accomplish this, the editors include an iframe with a preview of the webpage that includes both code from modules and templates as well as HubSpot application CSS/JS. Sometimes CSS/JS from a template or module causes problems in the editors. This article is a guide on best practices to avoid those problems.
It’s important to test your developed assets in HubSpot’s content editors before delivering them. Testing in the editor is a good way to spot styling conflicts with the editor and to create a seamless experience for your end user.
The following are a sampling of tests that can be run in the editor:
- Format text via Style dropdown options (i.e. font, text size) and toolbar options (i.e. alignment, colors)
- Click to insert options from Rich Text toolbar (i.e. embed, link, image, personalization tokens)
- Click on the rich text elements plugins (i.e. embed, link, image, personalization tokens) in the inline editors and make sure that the UI works correctly and appears as expected.
This function will run when there’s a click on the body element. Because the code calls
event.stopPropagation(), the event will not bubble up to the
window. If there are event listeners on those elements, the code in those listeners will not run. The issue with the above code is that the click handler will run on every click, which causes problems in the inline editor because it adds click handlers to the window element via React. Instead of listening for every click, it would be more appropriate to only add the click handler when needed — for example, after a user has clicked a button to open a side menu — and then removing the handler once the click has fired.
Problems can also occur in CSS when using selectors that are generic like
label. When possible it is preferred to use selectors that are specific to a portion of the webpage such as
Avoid using !important tags
!important tags are used to make styling rules take precedence over others. Use of !important tags on bare element selectors like
input[type="text"] are not best practice when building web content, but they are often used when a style rule is being overridden by something more specific than the selected rule. Using !important will help the selected rule rise to the surface and be the winning style rule on a given asset, even if it’s less specific.
Instead, favor specificity in your selectors so that you don’t have to use
!important. This will allow you to pinpoint the elements you’re looking to style without putting blanket rules over things beyond the element you’re targeting. You can also take advantage of our boilerplate CSS file to have a better sense of selectors that should be used to avoid CSS bleed issues.
!important is used in CSS for the
<label> element, with the intent to ensure that all
<label> elements in a form are white:
Despite the intent, using an element as a selector for the style rule is too broad, making all
<label> tags in the content editor white. This style rule doesn’t exclude the
<label> elements used in our insert link plugins, resulting in the illusion of missing labels for fields on our plugin.
Instead, you can use more specific selectors, as we did in the style rule below, to target the labels for the form module you’re seeking to add the white color to so this will not affect the labels elsewhere in the content editor. This is a great resource for developers looking to get a better sense of what selectors can be used when styling a form.
The editor uses an iframe to load a preview of the content into HubSpot’s content editor. To this iframe,
.hs-inline-edit is a class that is added to the
<html> element and
window.hsInEditor is defined to `true`. When the inline editor is initialized, these items give developers the opportunity to determine if the content is within the HubSpot content editor.
Developers can use the
window.hsInEditor variable to disable portions of their code when the code is run inside the editor.
Alternatively, you can take advantage of the HubSpot specific class.
For example, if you have a fixed header in your page and are seeing that the fixed rich text toolbar in the editor is getting hidden underneath the fixed header, you're probably pushing your website's UI layers to the highest z-index. Generally it's not good practice to do this (unless you absolutely have to). Within the editor it becomes particularly problematic as your content can render on top of the inline controls. We recommend keeping your websites' UI layers beneath [X] z-index for best results, for which you can use the selector
.hs-inline-edit, followed by the selector for your fixed header (i.e.
header), and employ the z-index value of 2147483599:
window.hsInEditor to determine whether or not to do something would be helpful. Consider the one pasted below that takes advantage of the jQuery document ready handler:
The jQuery document ready part will only work if your HubSpot site uses jQuery.
Thank you for your feedback, it means a lot to us.