Add Global CSS and JavaScript with TypoScript
Tested in: TYPO3v13 Categories: Beginner Templating Configuration
Managing your website's core CSS and JavaScript files globally is an efficient way to ensure a consistent look and feel across all pages. TYPO3's TypoScript provides a powerful, centralized method for including these assets sitewide.
This guide will teach you how to link custom CSS and JavaScript files to your site package using TypoScript page configuration.
Learning objective
In this step-by-step guide, you will learn how to create custom CSS and JavaScript files and include them globally on your site using the page.includeCSS and page.includeJS TypoScript properties.
Prerequisites
Tools and technology
- A local TYPO3 installation (v11 or newer).
- A site package extension created for your theme.
- Access to the file system of your TYPO3 installation.
- A code editor.
Knowledge and skills
- Basic understanding of the TYPO3 backend.
- Familiarity with the structure of a site package.
- Basic knowledge of CSS, JavaScript, and HTML.
- Awareness of what TypoScript is and its role in TYPO3.
Create Your Asset Files
First, you need to create the CSS and JavaScript files within your site package's directory structure.
- Navigate to your site package's
Resources/Public/directory. - Create two new sub-directories:
CssandJavaScript. -
Inside the
Cssdirectory, create a new file namedcustom-styles.css. Add a simple CSS rule for testing:body { background-color: #f0f0f0; border: 5px solid #20c997; }Copied! -
Inside the
JavaScriptdirectory, create a new file namedcustom-scripts.js. Add a simple JavaScript alert for testing:alert('Hello from custom-scripts.js!');Copied!Your file structure should now look like this:
└── your_sitepackage/ └── Resources/ └── Public/ ├── Css/ │ └── custom-styles.css ├── JavaScript/ │ └── custom-scripts.js └── ...Copied!
Include Assets Using TypoScript
This method is best for assets that should be loaded on every page of your site.
- Open your site package's main TypoScript setup file. This is typically located at
your_sitepackage/Configuration/TypoScript/setup.typoscript. -
Add the
page.includeCSSandpage.includeJSproperties to your page object configuration. You can assign a key (likestyleorjs) and set the value to the path of your asset file using theEXT:syntax.page { # ... other page configuration includeCSS { style = EXT:your_sitepackage/Resources/Public/Css/custom-styles.css } includeJS { js = EXT:your_sitepackage/Resources/Public/JavaScript/custom-scripts.js } }Copied!Make sure to replace
your_sitepackagewith the actual key of your site package extension.
Clear the Cache and Verify
Finally, clear the TYPO3 caches to ensure your changes are loaded, and then check the frontend of your website.
- Log in to the TYPO3 backend.
- Click the "Clear all caches" lightning bolt icon in the top toolbar.
-
Open your website in a new browser tab.
- You should see the styles from
custom-styles.cssapplied to the page (a light gray background and a green border). - You should see a JavaScript alert box appear with the message "Hello from custom-scripts.js!".
- You should see the styles from
Summary
Congratulations! You have successfully added global CSS and JavaScript files to your TYPO3 site using TypoScript. This centralized approach is excellent for managing your site's core assets.
Next steps
Now that you have experience with including assets globally, you might like to:
- Learn how to conditionally load assets on specific pages using TypoScript conditions.
- Explore how to configure and bundle assets for better performance.