how can i add custom css file for
Hello @uir347gmai8039
To add a custom CSS file in Magento 2.4, you can follow these steps:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <css src="css/custom.css" /> </head> </page>
php bin/magento cache:clean
With these steps, you have successfully added a custom CSS file in Magento 2.4, and it will be loaded on your storefront, allowing you to apply custom styles to your theme.
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
Please watch this video How to add the JS and CSS files into a custom module in Magento 2 https://www.youtube.com/watch?v=JGli227W1ks, it will help you to complete your requirements.
Hi @uir347gmai8039,
In Magento 2, you can add a custom CSS file by following these steps:
Create a Custom Theme (if not already created):
If you don't have a custom theme, you can create one. Create a new theme directory in the app/design/frontend directory.
For example, if your theme's name is "CustomTheme," your theme directory structure should look like:
app/design/frontend/Vendor/CustomTheme/
Create or Identify the default_head_blocks.xml File: In your custom theme directory, check if there is a default_head_blocks.xml file. If it doesn't exist, create one. This file is used to declare assets that should be included in the head section of the HTML document.
The path to the file should be:
app/design/frontend/Vendor/CustomTheme/Magento_Theme/layout/default_head_blocks.xml
Declare the CSS File in default_head_blocks.xml: Add the following content to your default_head_blocks.xml file to declare the custom CSS file:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <css src="css/custom-styles.css" /> </head> </page>
In this example, css/custom-styles.css is the path to your custom CSS file relative to your theme directory.
Create the Custom CSS File: In your custom theme directory, create a web/css directory. Inside this directory, create your custom CSS file. For example:
app/design/frontend/Vendor/CustomTheme/web/css/custom-styles.css
Add your custom styles to this file.
Deploy the Static Content: After creating the custom CSS file, you need to deploy the static content. Run the following command from your Magento root directory:
bin/magento setup:static-content:deploy
This command will deploy the static content, including your custom CSS file.
Clear Cache: Finally, clear the Magento cache to apply the changes:
bin/magento cache:clean
Now, your custom CSS file should be included in the head of your Magento store pages. Make sure to customize the paths and file names according to your theme structure and preferences.