How do I add custom scripts and style only for specific pages in Shopify?

hello community i want to clarify is it possible to add custom scripts and style only for specific pages?

Yes, @nixe, is it possible to add custom scripts and styles only for specific pages using the below code

{% if template == 'product' %}
  
{% endif %}

{% if template == 'collection' %}
  
{% endif %}

If you need further assistance, please let me know. If you found my help useful, consider liking this message and marking it as the Accepted Solution.
Best regards
K.K

Hey @nixe ,

Yes, it’s possible to add custom scripts and styles only for specific pages on your Shopify store. You can do this by targeting specific pages using conditional logic within your theme’s Liquid files or by adding the styles/scripts directly to specific templates.

follow these steps:

  1. Adding Custom Styles or Scripts in Specific Pages:

You can add your custom styles and scripts to specific pages using Liquid’s conditional logic:

For Custom Styles:

In your theme’s theme.liquid file (or the specific template file, like product.liquid or page.liquid), wrap the styles inside a conditional block to target a specific page:

{% if template == 'page.about-us' %}
  
{% endif %}

In the example above, the CSS will only apply to the About Us page (assuming the page template is named “about-us”).

For Custom Scripts:

Similarly, for JavaScript, you can add it conditionally:

{% if template == 'product' %}
  
{% endif %}

This script will only run on product pages.

Page-Specific Templates:

If you are using different templates for different pages (e.g., a custom-page.liquid template), you can insert scripts or styles directly into that template fi

{% if template == 'page.custom-page' %}
  
  
{% endif %}

If I was able to help you, please don’t forget to Like and mark it as the Solution!
Best Regard,
Rajat Sharma

  1. Create a new .liquid section

  2. Add a html type input setting in that section

  3. Add that section in the page you want and add the custom scripts and styles for those pages.

  4. (Optional) Contact me if you didn’t understand anything.