Home > Shopify Development > Apply a class to your templates

How to apply a class to your templates in Shopify

Sam Nguyen
Sam Updated: March 20, 2024

Share:

Drive 20-40% of your revenue with Avada
avada email marketing

Sometimes, you might not want every image on your Shopify store to open in a lightbox, especially if they’re hyperlinked or already the desired size. There are two ways to customize this behavior: applying a class to your templates or adding jQuery code. This article will focus on the first method, guiding you through applying a class to specific templates for more control over image display.

How to apply a class to your templates in Shopify

Step 1:

Go to Online Store > Themes from your Shopify Admin panel. apply class to templates 1

Step 2:

Choose the theme you want to edit and click Actions > Edit code. apply class to templates 2

Step 3:

Under the Templates folder, tap page.liquid, article.liquid, and blog.liquid to let them opened in the online code editor: apply class to templates 3

  • page.liquid is employed for all specialized page content.
  • article.liquid is employed for every individual blog post page.
  • blog.liquid is employed for the blog landing page.

Step 4:

Locate the div element which wraps your content, and insert rte to its class attribute. In case the class attribute does not exist, please add it.

- In blog.liquid and article.liquid:


  <div class="rte">
    {{ article.content }}
  </div>

- In page.liquid:


  <div class="rte">
    {{ page.content }}
  </div>

Step 5:

Tap Save to save all of your changes to every template file.

Tips for applying a class to your Shopify templates

Here are some valuable tips to keep in mind when applying classes to your Shopify templates using Liquid:

Planning and Organization:

  • Clear Naming Convention: Use descriptive and consistent class names that reflect the purpose or styling of the elements they target. This makes your code more readable and maintainable.
  • Modular Approach: Break down your CSS styles into smaller, reusable classes to promote code efficiency and avoid redundancy.
  • Plan Ahead: Before adding classes, map out the specific elements you want to target and the styles or functionality you want to apply. This will help you create a structured and organized approach.

Liquid Code Implementation:

  • Conditional Logic: Use Liquid’s if, else, and unless statements to apply classes conditionally based on specific criteria like template type, product tags, or customer group.
  • The contains Operator: Use the contains operator to check if a template name or handle includes a specific string, allowing you to target a group of templates with similar characteristics.
  • The handle Variable: Utilize the handle variable to dynamically add classes based on the current page’s handle, providing more targeted styling options.

CSS Styling and Customization:

  • Specificity: Understand CSS specificity rules to ensure your custom styles override any default theme styles for the targeted elements.
  • Responsiveness: Consider using media queries within your CSS to create responsive designs that adapt to different screen sizes and devices.
  • Testing: Thoroughly test your class implementations and CSS styles across different browsers and devices to ensure consistency and avoid any unexpected behavior.

Additional Tips:

  • Theme Documentation: Consult your theme’s documentation for any specific instructions or best practices for adding custom classes.
  • Shopify Experts: If you’re not comfortable editing Liquid code or CSS, consider hiring a Shopify expert to assist you with the implementation.
  • Backup Your Theme: Always create a backup of your theme files before making any code changes, allowing you to revert to the previous version if needed.

By following these tips and best practices, you can effectively apply classes to your Shopify templates and unlock a world of customization possibilities for your online store.

FAQs

Applying a class to your Shopify templates allows you to target specific elements within those templates using CSS. This enables you to style those elements uniquely, add functionality with JavaScript, or even hide/show content based on the class.

You can commonly apply classes to `page.liquid`, `article.liquid`, and `blog.liquid` templates. These control the layout and content of your pages, blog posts, and articles, respectively.

Yes, you can use Liquid conditional statements to apply different classes based on the template type. For example, you could add a class specific to blog posts using .

Applying a class allows you to reuse the same CSS styles across multiple templates or elements, promoting code maintainability. Adding custom CSS directly within the template file is less flexible and can lead to code duplication if you need to apply the same styles elsewhere.

Conclusion

By applying CSS classes to your Shopify templates using Liquid, you gain greater control over styling and functionality, enabling a more tailored and polished storefront experience. Have you explored other creative ways to customize your Shopify templates with Liquid? Share your insights and techniques in the comments below!

Related post:


Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.

Stay in the know

Get special offers on the latest news from AVADA.